Submission #37329

#TimeUsernameProblemLanguageResultExecution timeMemory
37329szawinisCities (BOI16_cities)C++14
74 / 100
4000 ms41820 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, int> pli; const int N = 1e5+1; const ll INF = 1e17+1; int n, k, m; vector<int> s; vector<pair<int,int> > g[N]; ll ans = INF, dp[1 << 5][N]; priority_queue<pli, vector<pli>, greater<pli> > pq; int main() { scanf("%d %d %d", &n, &k, &m); for(int i = 0, v; i < k; i++) { scanf("%d", &v); s.push_back(v-1); } for(int i = 0, u, v, w; i < m; i++) { scanf("%d %d %d", &u, &v, &w); g[u-1].emplace_back(v-1, w); g[v-1].emplace_back(u-1, w); } for(int mask = 1; mask < 1 << k; mask++) { for(int v = 0; v < n; v++) dp[mask][v] = INF; } for(int i = 0; i < k; i++) dp[1 << i][s[i]] = 0; for(int mask = 1; mask < 1 << k; mask++) { for(int prev_mask1 = 0; prev_mask1 < mask; prev_mask1++) { int prev_mask2 = prev_mask1 ^ mask; if((prev_mask1 | mask) != mask || prev_mask2 > prev_mask1) continue; for(int v = 0; v < n; v++) dp[mask][v] = min(dp[prev_mask1][v] + dp[prev_mask2][v], dp[mask][v]); } for(int v = 0; v < n; v++) if(dp[mask][v] < INF) pq.emplace(dp[mask][v], v); while(!pq.empty()) { int u = pq.top().second; pq.pop(); for(auto p: g[u]) if(dp[mask][u] + p.second < dp[mask][p.first]) { dp[mask][p.first] = dp[mask][u] + p.second; pq.emplace(dp[mask][p.first], p.first); } } } ll ans = INF; for(int v = 0; v < n; v++) ans = min(dp[(1 << k)-1][v], ans); printf("%lld", ans); }

Compilation message (stderr)

cities.cpp: In function 'int main()':
cities.cpp:14:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &n, &k, &m);
                               ^
cities.cpp:16:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &v);
                  ^
cities.cpp:21:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &u, &v, &w);
                                ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...