# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1075498 |
2024-08-26T07:05:36 Z |
vjudge1 |
Cities (BOI16_cities) |
C++17 |
|
1549 ms |
47332 KB |
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define el cout << '\n'
#define f1(i,n) for(ll i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 2e5+5, inf=1e18;
ll n,k,m,dist[6][6], im[6], d[maxn],dp[(1 << 5)][maxn];
vector<pair<ll,ll>> G[maxn];
priority_queue<pll, vector<pll>, greater<pll>> pq;
bool vis[maxn];
void dijkstra(ll s){
f1(i,n) d[i] = inf;
d[s] = 0ll;
pq.push({d[s], s});
while(!pq.empty()){
pair<ll,ll> item = pq.top();
pq.pop();
ll dist = item.first, u = item.second;
if(dist != d[u]) continue;
vis[u] = true;
for(pair<ll,ll> e: G[u]){
ll c = e.first, w = e.second;
if(d[u] + w < d[c]){
d[c] = d[u] + w;
pq.push({d[c], c});
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(__file_name ".inp", "r")){
freopen(__file_name ".inp","r",stdin);
freopen(__file_name ".out","w",stdout);
}
// code here
cin >> n >> k >> m;
for(ll i=0;i<(1 << k);i++){
f1(u, n) dp[i][u] = inf;
}
f1(i,k) {
cin >> im[i];
dp[1 << (i-1)][im[i]] = 0;
}
f1(i,m){
ll u, v, w; cin >> u >> v >> w;
G[u].push_back({v, w});
G[v].push_back({u, w});
}
for(ll mask=1;mask<(1 << k);mask++){
// if(__builtin_popcount(mask) == 1) continue;
for(ll submask=mask&(mask-1);submask;submask=mask&(submask-1)){
f1(u, n){
dp[mask][u] = min(dp[mask][u], dp[submask][u] + dp[mask ^ submask][u]);
}
}
f1(u,n) pq.push({dp[mask][u], u});
while(!pq.empty()){
pair<ll,ll> item = pq.top();
pq.pop();
ll dist = item.first, u = item.second;
// cout << dist << ' ' << u << ' ' << mask << ' ' << dp[mask][u];el;
if(dist > dp[mask][u]) continue;
// vis[u] = true;
for(pair<ll,ll> e: G[u]){
ll c = e.first, w = e.second;
if(dp[mask][u] + w < dp[mask][c]){
dp[mask][c] = dp[mask][u] + w;
pq.push({dp[mask][c], c});
}
}
}
}
cout << *min_element(dp[(1 << k) - 1]+1, dp[(1 << k) - 1] + n + 1);
return 0;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen(__file_name ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen(__file_name ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
406 ms |
28616 KB |
Output is correct |
2 |
Correct |
434 ms |
27460 KB |
Output is correct |
3 |
Correct |
246 ms |
19940 KB |
Output is correct |
4 |
Correct |
51 ms |
16208 KB |
Output is correct |
5 |
Correct |
210 ms |
25308 KB |
Output is correct |
6 |
Correct |
39 ms |
16208 KB |
Output is correct |
7 |
Correct |
5 ms |
5464 KB |
Output is correct |
8 |
Correct |
4 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5596 KB |
Output is correct |
2 |
Correct |
5 ms |
5468 KB |
Output is correct |
3 |
Correct |
4 ms |
5468 KB |
Output is correct |
4 |
Correct |
4 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
669 ms |
34764 KB |
Output is correct |
2 |
Correct |
668 ms |
33752 KB |
Output is correct |
3 |
Correct |
461 ms |
26312 KB |
Output is correct |
4 |
Correct |
389 ms |
26564 KB |
Output is correct |
5 |
Correct |
122 ms |
18728 KB |
Output is correct |
6 |
Correct |
46 ms |
18772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1425 ms |
47332 KB |
Output is correct |
2 |
Correct |
1549 ms |
47212 KB |
Output is correct |
3 |
Correct |
1455 ms |
46444 KB |
Output is correct |
4 |
Correct |
896 ms |
39076 KB |
Output is correct |
5 |
Correct |
740 ms |
32716 KB |
Output is correct |
6 |
Correct |
171 ms |
20144 KB |
Output is correct |
7 |
Correct |
57 ms |
18756 KB |
Output is correct |