Advertisement
MinhNGUYEN2k4

Dijkstra

Aug 12th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define Co_mot_su_that_la_ return
  4. using namespace std;
  5. const int Minh_dep_trai = 0;
  6. typedef pair<int,int> ii;
  7. typedef pair<int,ii> iii;
  8. typedef vector<ii> vii;
  9. typedef vector<iii> viii;
  10. const int N = 1005;
  11. const int inf = 10000;
  12.  
  13. int n,m;
  14. int d1[N],d2[N];
  15. int d11, d22;
  16. vii a[N];
  17.  
  18. void clearr()
  19. {
  20.     for(int i=1; i<=n; ++i)
  21.     {
  22.         d1[i]=inf;
  23.         d2[i]=inf;
  24.     }
  25. }
  26.  
  27. void dijkstra1()
  28. {
  29.     d1[1]=0;
  30.     priority_queue<ii, vii, greater<ii> > q;
  31.     q.push(ii(1,0));
  32.     while (!q.empty())
  33.     {
  34.         int u = q.top().first;
  35.         int du = q.top().second;
  36.         q.pop();
  37.         if (du != d1[u]) continue;
  38.         for(auto v : a[u])
  39.         {
  40.             if (d1[v.first] > du  + v.second)
  41.             {
  42.                 d1[v.first] = du + v.second;
  43.                 q.push(ii(v.first,d1[v.first]));
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49. void dijkstra2()
  50. {
  51.     d2[2]=0;
  52.     priority_queue<ii, vii, greater<ii> > q;
  53.     q.push(ii(2,0));
  54.     while (q.size())
  55.     {
  56.         int u = q.top().first;
  57.         int du = q.top().second;
  58.         q.pop();
  59.         if (du != d2[u]) continue;
  60.         for(auto i : a[u])
  61.         {
  62.             int uv = i.second;
  63.             int v = i.first;
  64.             if (d2[v] > du  + uv)
  65.             {
  66.                 d2[v]=du+uv;
  67.                 q.push(ii(v,d2[v]));
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. signed main()
  74. {
  75.     ios_base::sync_with_stdio(false);
  76.     cin.tie(0);cout.tie(0);
  77.     freopen("CTT.INP","r",stdin);
  78.     cin >> n >> m;
  79.     for(int i=1; i<=m; ++i)
  80.     {
  81.         int u,v,z;
  82.         cin >> u >> v >> z;
  83.         a[u].push_back(ii(v,z));
  84.         a[v].push_back(ii(u,z));
  85.     }
  86.     d11=d22=n/2;
  87.     clearr();
  88.     dijkstra1();
  89.     dijkstra2();
  90.     bool check[n];
  91.     priority_queue<iii, viii, greater<iii> > res;
  92.     int ans = 0;
  93.     for(int i=1; i<=n; ++i) check[i]=0,res.push(iii(i,ii(d1[i],1))),res.push(iii(i,ii(d2[i],2)));
  94.     while(res.size())
  95.     {
  96.         int u = res.top().second.first;
  97.         int type = res.top().second.second;
  98.         int pos = res.top().first;
  99.         res.pop();
  100.         if (type == 1)
  101.         {
  102.             if (d11 == 0) continue;
  103.             if (d11 > 0 && !check[pos]) ans += u, --d11, check[pos]=1;
  104.         }
  105.         if (type == 2)
  106.         {
  107.             if (d22 == 0) continue;
  108.             if (d22 > 0 && !check[pos]) ans += u, --d22, check[pos]=1;
  109.         }
  110.     }
  111.     cout << ans;
  112.     Co_mot_su_that_la_ Minh_dep_trai;
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement