Dang_Quan_10_Tin

Untitled

Jun 1st, 2020
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <vector>
  6. #define task "SHORTCUT"
  7. #define ll long long
  8. using namespace std;
  9.  
  10. struct Tadj{
  11.     int v;
  12.     ll w;
  13.     Tadj () {}
  14.     Tadj(int u, ll t){
  15.         v = u;
  16.         w = t;
  17.     }
  18.     bool operator < (const Tadj &a) const{
  19.         return v < a.v
  20.             || (v == a.v && w < a.w);
  21.     }
  22. };
  23.  
  24. const int N = 1e5 + 2;
  25. int n, m;
  26. ll a[N];
  27. ll t;
  28. ll dis[N];
  29. vector<Tadj> adj[N];
  30. int par[N];
  31. const ll Inf = 1e18;
  32.  
  33. void Read(){
  34.     cin >> n >> m >> t;
  35.     for(int i = 1; i <= n; ++i)
  36.         cin >> a[i];
  37.     for(int i = 1; i <= m; ++i){
  38.         int a, b, c;
  39.         cin >> a >> b >> c;
  40.         adj[a].push_back(Tadj(b, c));
  41.         adj[b].push_back(Tadj(a, c));
  42.     }
  43. //  for(int i = 1; i <= n; ++i)
  44. //      sort(adj[i].rbegin(), adj[i].rend());
  45. }
  46.  
  47. struct Tque{
  48.     int v;
  49.     ll w;
  50.     Tque() {}
  51.     Tque(int u, ll t){
  52.         v = u;
  53.         w = t;
  54.     }
  55.     bool operator < (const Tque& a) const{
  56.         return w > a.w;
  57.     }
  58.     bool Valid(){
  59.         return w == dis[v];
  60.     }
  61. };
  62.  
  63. void Init(){
  64.     fill_n(dis, N, Inf);
  65.     dis[1] = 0;
  66.     par[1] = 1;
  67. }
  68.  
  69. bool Relax(int c, int i, ll w){
  70.     ll res = dis[c] + w;
  71.     if(dis[i] > res){
  72.         dis[i] = res;
  73.         return true;
  74.     }
  75.     if(dis[i] == res)
  76.         par[i] = min(par[i], c);
  77.     return false;
  78. }
  79.  
  80. void Dijkstra(){
  81.     Init();
  82.     priority_queue<Tque, vector<Tque> > s;
  83.     s.push(Tque(1, 0));
  84.     while(s.size()){
  85.         Tque c = s.top();
  86.         s.pop();
  87.         if(!c.Valid())
  88.             continue;
  89.         for(Tadj i : adj[c.v])
  90.             if(Relax(c.v, i.v, i.w)){
  91.                 s.push(Tque(i.v, dis[i.v]));
  92.                 par[i.v] = c.v;
  93.             }
  94.     }
  95. }
  96.  
  97. void dfs(int i){
  98.     for(Tadj j : adj[i])
  99.         if(par[j.v] == i){
  100.             dfs(j.v);
  101.             a[i] += a[j.v];
  102.         }
  103. }
  104.  
  105. void trace(int i){
  106.     if(i == 1){
  107.         cerr << 1;
  108.         return;
  109.     }
  110.     cerr << i << " ";
  111.     trace(par[i]);
  112. }
  113.  
  114. void Solve(){
  115.     Dijkstra();
  116.     //trace(n);cerr << '\n';
  117.     dfs(1);
  118.     ll res = 0;
  119.     for(int i = 1; i <= n; ++i){
  120.         if(dis[i] != Inf)
  121.             res = max(res, a[i] * (dis[i] - t));
  122.     }
  123.     cout << res;
  124. }
  125.  
  126. main(){
  127.     ios::sync_with_stdio(0);
  128.     cin.tie(0);
  129.     cout.tie(0); if(fopen(task".INP", "r"))
  130.     freopen(task".INP", "r", stdin),
  131.     freopen(task".OUT", "w", stdout);
  132.     if(fopen("shortcut.in", "r"))
  133.     freopen("shortcut.in", "r", stdin),
  134.     freopen("shortcut.out", "w", stdout);
  135.     Read();
  136.     Solve();
  137. }
Advertisement
Add Comment
Please, Sign In to add comment