Submission #75743

#TimeUsernameProblemLanguageResultExecution timeMemory
75743polyfishCities (BOI16_cities)C++14
100 / 100
4326 ms81396 KiB
//pantyhose(black) + glasses = infinity #include <bits/stdc++.h> using namespace std; #define debug(x) cerr << #x << " = " << x << '\n'; #define BP() cerr << "OK!\n"; #define PR(A, n) {cerr << #A << " = "; for (int _=1; _<=n; ++_) cerr << A[_] << ' '; cerr << '\n';} #define PR0(A, n) {cerr << #A << " = "; for (int _=0; _<n; ++_) cerr << A[_] << ' '; cerr << '\n';} #define FILE_NAME "data" typedef pair<int64_t, int> pairInts; const int MAX_N = 100002; const int MAX_K = 5; const int64_t INF = 1e18; int n, m, k, important_city[MAX_K]; int64_t f[1<<MAX_K][MAX_N]; vector<pairInts> g[MAX_N]; priority_queue<pairInts, vector<pairInts>, greater<pairInts> > pq; void enter() { cin >> n >> k >> m; for (int i=0; i<k; ++i) { cin >> important_city[i]; } for (int i=1; i<=m; ++i) { int u, v, w; cin >> u >> v >> w; g[u].push_back(make_pair(w, v)); g[v].push_back(make_pair(w, u)); } } void dijkstra(int x) { for (int i=1; i<=n; ++i) pq.push(make_pair(f[x][i], i)); while (pq.size()) { int u = pq.top().second; int64_t du = pq.top().first; pq.pop(); if (du!=f[x][u]) continue; for (int i=0; i<g[u].size(); ++i) { int v = g[u][i].second, w = g[u][i].first; if (f[x][v]>f[x][u]+w) { pq.push(make_pair(f[x][v] = f[x][u] + w, v)); } } } } void dp() { for (int x=0; x<(1<<k); ++x) for (int i=1; i<=n; ++i) f[x][i] = INF; for (int i=0; i<k; ++i) f[1<<i][important_city[i]] = 0; for (int x=0; x<(1<<k); ++x) { for (int u=1; u<=n; ++u) { for (int i=0; i<g[u].size(); ++i) { int v = g[u][i].second, w = g[u][i].first; for (int x2 = x; x2>0; x2 = (x2 - 1) & x) { f[x][u] = min(f[x][u], f[x2][v] + f[x^x2][u] + w); } } } dijkstra(x); } int64_t res = INF; for (int i=1; i<=n; ++i) res = min(res, f[(1<<k)-1][i]); cout << res; } int main() { #ifdef OFFLINE_JUDGE freopen(FILE_NAME".inp", "r", stdin); freopen(FILE_NAME".out", "w", stdout); #endif ios::sync_with_stdio(0); cin.tie(0); enter(); dp(); }

Compilation message (stderr)

cities.cpp: In function 'void dijkstra(int)':
cities.cpp:45:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i=0; i<g[u].size(); ++i) {
                       ~^~~~~~~~~~~~
cities.cpp: In function 'void dp()':
cities.cpp:62:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int i=0; i<g[u].size(); ++i) {
                           ~^~~~~~~~~~~~
#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...