# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1075439 |
2024-08-26T06:04:04 Z |
vjudge1 |
Cities (BOI16_cities) |
C++17 |
|
2016 ms |
46656 KB |
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define all(x) x.begin(), x.end()
mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
const int NM = 1e5 + 5;
const int inf = 1e18 + 5;
vector<pair<int, int>> g[NM];
int n, k, m, dp[NM][32], imp[5];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
void solve()
{
cin >> n >> k >> m;
for (int i = 0; i < k; i++)
cin >> imp[i];
while (m--)
{
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
for (int msk = 1; msk < (1 << k); msk++)
{
for (int i = 1; i <= n; i++)
dp[i][msk] = inf;
if (__builtin_popcount(msk) == 1)
dp[imp[__lg(msk)]][msk] = 0;
for (int u = 1; u <= n; u++)
for (int smsk = (msk - 1) & msk; smsk; smsk = (smsk - 1) & msk)
setmin(dp[u][msk], dp[u][smsk] + dp[u][msk ^ smsk]);
for (int u = 1; u <= n; u++)
pq.push({dp[u][msk], u});
while (!pq.empty())
{
auto t = pq.top();
pq.pop();
if (dp[t.se][msk] != t.fi)
continue;
for (auto v : g[t.se])
if (dp[v.fi][msk] > t.fi + v.se)
{
dp[v.fi][msk] = t.fi + v.se;
pq.push({dp[v.fi][msk], v.fi});
}
}
}
int res = inf;
for (int i = 1; i <= n; i++)
setmin(res, dp[i][(1 << k) - 1]);
cout << res;
}
signed main()
{
fastIO
if (fopen("in.txt", "r"))
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
int tc = 1;
// cin >> tc;
// pre();
while (tc--)
solve();
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen("in.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
cities.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen("out.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
3 ms |
2820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
498 ms |
46536 KB |
Output is correct |
2 |
Correct |
472 ms |
45752 KB |
Output is correct |
3 |
Correct |
321 ms |
37320 KB |
Output is correct |
4 |
Correct |
50 ms |
15552 KB |
Output is correct |
5 |
Correct |
272 ms |
46656 KB |
Output is correct |
6 |
Correct |
45 ms |
15324 KB |
Output is correct |
7 |
Correct |
4 ms |
3160 KB |
Output is correct |
8 |
Correct |
3 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3160 KB |
Output is correct |
2 |
Correct |
5 ms |
3272 KB |
Output is correct |
3 |
Correct |
4 ms |
3160 KB |
Output is correct |
4 |
Correct |
4 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
978 ms |
46580 KB |
Output is correct |
2 |
Correct |
937 ms |
45628 KB |
Output is correct |
3 |
Correct |
611 ms |
37316 KB |
Output is correct |
4 |
Correct |
476 ms |
32088 KB |
Output is correct |
5 |
Correct |
119 ms |
19124 KB |
Output is correct |
6 |
Correct |
51 ms |
18000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1995 ms |
46424 KB |
Output is correct |
2 |
Correct |
2016 ms |
46536 KB |
Output is correct |
3 |
Correct |
1899 ms |
45496 KB |
Output is correct |
4 |
Correct |
1252 ms |
37320 KB |
Output is correct |
5 |
Correct |
901 ms |
32196 KB |
Output is correct |
6 |
Correct |
210 ms |
19120 KB |
Output is correct |
7 |
Correct |
60 ms |
17744 KB |
Output is correct |