SHARE
TWEET

Все равно WA

a guest Sep 22nd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long ans = 1LL << 60;
  5. int n, m, k;
  6.  
  7. vector <int> markets;
  8. vector <bool> mark;
  9. vector <vector <pair <int, long long> > > g;
  10. vector <vector <long long> > dist;
  11. set <pair <long long, int> > que;
  12. vector <int> path;
  13.  
  14. void dijkstra(int ind) {
  15.     dist[ind][markets[ind]] = 0;
  16.     que.insert({0, markets[ind]});
  17.     for (int i = 0; i < n; ++i) {
  18.         if (i != ind) {
  19.             que.insert({1LL << 60, i});
  20.         }
  21.     }
  22.     while (que.size()) {
  23.         for (auto u : g[que.begin() -> second]) {
  24.             if (dist[ind][u.first] > que.begin() -> first + u.second) {
  25.                 que.erase({dist[ind][u.first], u.first});
  26.                 dist[ind][u.first] = que.begin() -> first + u.second;
  27.                 que.insert({dist[ind][u.first], u.first});
  28.             }
  29.         }
  30.         que.erase(que.begin());
  31.     }
  32. }
  33.  
  34. int main() {
  35.     freopen("relocate.in", "r", stdin);
  36.     freopen("relocate.out", "w", stdout);
  37.     ios::sync_with_stdio(0);
  38.     cin.tie(0);
  39.     cin >> n >> m >> k;
  40.     dist.resize(k, vector <long long> (n, 1LL << 60));
  41.     markets.resize(k);
  42.     mark.resize(n);
  43.     g.resize(n);
  44.     for (int i = 0; i < k; ++i) {
  45.         cin >> markets[i];
  46.         --markets[i];
  47.         mark[markets[i]] = 1;
  48.     }
  49.     for (int i = 0; i < m; ++i) {
  50.         int u, v;
  51.         long long len;
  52.         cin >> u >> v >> len;
  53.         --u;
  54.         --v;
  55.         g[u].push_back({v, len});
  56.         g[v].push_back({u, len});
  57.     }
  58.     for (int i = 0; i < k; ++i) {
  59.         dijkstra(i);
  60.     }
  61.     path.resize(k);
  62.     iota(path.begin(), path.end(), 0);
  63.     do {
  64.         long long have = 1LL << 60;
  65.         for (int i = 0; i < n; ++i) {
  66.             if (!mark[i]) {
  67.                 have = min(have, dist[path[0]][i] + dist[path[k - 1]][i]);
  68.             }
  69.         }
  70.         if (have == 1LL << 60) {
  71.             continue;
  72.         }
  73.         for (int i = 1; i < k; ++i) {
  74.             have += dist[path[i]][markets[path[i - 1]]];
  75.         }
  76.         ans = min(ans, have);
  77.     } while (next_permutation(path.begin(), path.end()));
  78.     cout << ans << "\n";
  79.     return 0;
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top