jainbot27

usacoshortcuts

Jul 25th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target("sse4")
  3. #include <bits/stdc++.h>
  4. #define f first
  5. #define s second
  6. #define pb push_back
  7. #define mp make_pair
  8. #define ts to_string
  9. #define ub upper_bound
  10. #define lb lower_bound
  11. const char nl = '\n';
  12. using namespace std;
  13. using ll = long long;
  14. using vi = vector<ll>;
  15. using vl = vector<ll>;
  16. using pii = pair<ll, ll>;
  17. using pll = pair<ll, ll>;
  18. using str = string;
  19. //The following debugging code was taken from Benjamin Qi's template :)
  20. str ts(char c) { return str(1,c); }
  21. str ts(bool b) { return b ? "true" : "false"; }
  22. str ts(const char* s) { return (str)s; }
  23. str ts(str s) { return s; }
  24. template<class A> str ts(complex<A> c) {
  25.     stringstream ss; ss << c; return ss.str(); }
  26. str ts(vector<bool> v) {
  27.     str res = "{"; for(ll i = 0;i < (ll)v.size(); i++) res += char('0'+v[i]);
  28.     res += "}"; return res; }
  29. template<size_t SZ> str ts(bitset<SZ> b) {
  30.     str res = ""; for(ll i = 0; i < b.size(); i++) res += char('0'+b[i]);
  31.     return res; }
  32. template<class A, class B> str ts(pair<A,B> p);
  33. template<class T> str ts(T v) {
  34.     bool fst = 1; str res = "{";
  35.     for (const auto& x: v) {
  36.         if (!fst) res += ", ";
  37.         fst = 0; res += ts(x);
  38.     }
  39.     res += "}"; return res;
  40. }
  41. template<class A, class B> str ts(pair<A,B> p) {
  42.     return "("+ts(p.f)+", "+ts(p.s)+")"; }
  43. void DBG() { cerr << "]" << endl; }
  44. template<class H, class... T> void DBG(H h, T... t) {
  45.     cerr << ts(h); if (sizeof...(t)) cerr << ", ";
  46.     DBG(t...); }
  47.  
  48. #ifdef LOCAL
  49. #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
  50. #else
  51. #define dbg(...) 69
  52. //freopen(".in", "r", stdin);
  53. //freopen(".out", "w", stdout);
  54. #endif
  55. //CODE STARTS HERE-----------------------------------------------------------------------------------------//
  56. const ll mxN = 1e4 + 10;
  57. ll n, m, t, c[mxN]; ll d[mxN]; ll p[mxN], amt[mxN];
  58. vector<pii> adj[mxN];
  59. priority_queue<pii, vector<pii>, greater<pii>> pq;
  60. ll dist = 0;
  61. signed main(){
  62.     cin.tie(nullptr);
  63.     ios_base::sync_with_stdio(false);
  64.     // ifstream cin("shortcut.in");
  65.     // ofstream cout("shortcut.out");
  66.     cin >> n >> m >> t;
  67.     for(ll i = 0; i < n; i++)
  68.         cin >> c[i];
  69.     for(ll i = 0; i < m; i++){
  70.         ll a, b, e;
  71.         cin >> a >> b >> e;
  72.         a--,b--;
  73.         adj[a].pb(mp(b,e));
  74.         adj[b].pb(mp(a,e));
  75.     }
  76.     pq.push({0,0});
  77.     memset(d,0x3f,sizeof(d));
  78.     d[0] = 0;
  79.     p[0] = 0;
  80.     while(!pq.empty()){
  81.         ll v=pq.top().s; ll dv = pq.top().f; pq.pop();
  82.         // dbg(v, dv);
  83.         if(dv > d[v]) continue;
  84.         for(pii u : adj[v]){
  85.             if(d[u.f] > dv + u.s || (d[u.f] == dv + u.s && v < p[u.f])){
  86.                 d[u.f] = dv + u.s;
  87.                 p[u.f] = v;
  88.                 pq.push({d[u.f], u.f});
  89.             }
  90.         }
  91.     }
  92.     for(ll i =0 ; i < n; i++){
  93.         ll u = i;
  94.         while(u != 0){
  95.             amt[u] += c[i];
  96.             u = p[u];
  97.         }
  98.         amt[0] += c[i];
  99.     }
  100.     for(ll i = 0; i< n; i++){
  101.         dist += c[i]  * d[i];
  102.     }
  103.     ll ans = 0;
  104.     for(ll i=0 ; i < n; i++){
  105.         ll curdist = dist- (d[i] * amt[i]) + (amt[i]  * t);
  106.         ans = max(ans, dist - curdist);
  107.     }
  108.     cout << ans << nl;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment