Advertisement
willy108

ZOJ 4136

Mar 27th, 2021
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. //escape plan
  2. //https://zoj.pintia.cn/problem-sets/91827364500/problems/91827370531
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <cstdlib>
  7. #include <string>
  8. #include <utility>
  9. #include <cmath>
  10. #include <cassert>
  11. #include <algorithm>
  12. #include <vector>
  13. #include <random>
  14. #include <chrono>
  15. #include <queue>
  16. #define cont continue
  17.  
  18. #define pow2(n) (1 << (n))
  19. #define ll long long
  20. #define pii pair<int, int>
  21. #define pb push_back
  22. #define mp make_pair
  23. #define ins insert
  24. #define lsb(n) ((n)&(-(n)))
  25. #define LC(n) (((n) << 1) + 1)
  26. #define RC(n) (((n) << 1) + 2)
  27. #define add(a, b) (((a)%mod + (b)%mod)%mod)
  28. #define mul(a, b) (((a)%mod * (b)%mod)%mod)
  29. #define init(arr, val, sz) memset(arr, val, (sz + 2) * sizeof(arr[0]))
  30. #define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
  31. #define tern(a, b, c) ((a) ? (b) : (c))
  32. //the following ones give off usaco vibes
  33. #define pbenq priority_queue
  34. #define moo printf
  35. #define oom scanf
  36. #define mool puts("")
  37. #define loom getline
  38.  
  39. const ll mod = 1e9 + 7;
  40. const int MX = 1e5 +10, int_max = 0x3f3f3f3f;
  41.  
  42. using namespace std;
  43. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  44. void setIO(const string& file_name){
  45.     freopen((file_name+".in").c_str(), "r", stdin);
  46.     freopen((file_name+".out").c_str(), "w+", stdout);
  47. }
  48.  
  49.  
  50. struct node{
  51.   int u, w;
  52.   node(){}
  53.   node(int a, int b){ u = a, w = b; }
  54.   bool operator < (const node& b) const{
  55.     return w > b.w;
  56.   }
  57. };
  58.  
  59. vector<pii> adj[MX];
  60. int dist[MX], en[MX], blo[MX], vis[MX], fin[MX];
  61. int n, m, k;
  62.  
  63. pbenq<node> pq; //OTZ OTZ OTZ
  64.  
  65. int dijkstra(){
  66.   pq = pbenq<node>();
  67.   init(dist, 0x3f, n);
  68.   init(vis, 0, n);
  69.   for(int i = 1; i<=n; i++){
  70.     if(en[i]){
  71.       pq.push(node(i, 0));
  72.       dist[i] = 0;
  73.     }
  74.   }
  75.   while(!pq.empty()){
  76.     node cur = pq.top(); pq.pop();
  77.     //moo("%d %d\n", cur.u, cur.w);
  78.     int u = cur.u, d = cur.w;
  79.     if(blo[u] && !en[u]){
  80.       blo[u]--; cont;
  81.     }
  82.     if(u == 1) return d;
  83.     if(vis[u]) cont;
  84.     vis[u] = 1;
  85.     for(const auto& e : adj[u]){
  86.       int v = e.first, t = e.second + d;
  87.       //if(dist[v] > t){
  88.         //dist[v] = t;
  89.         pq.push(node(v, t));
  90.       //}
  91.     }
  92.   }
  93.   return -1;
  94. }
  95.  
  96.  
  97. int main(){
  98.   cin.tie(0) -> sync_with_stdio(0);
  99.   int T; cin >> T;
  100.   while(T--){
  101.     cin >> n >> m >> k;
  102.     for(int i = 0; i<=n; i++) adj[i].clear();
  103.     init(en, 0, n);
  104.     init(blo, 0, n);
  105.     for(int i = 0; i<k; i++){
  106.       int a; cin >> a;
  107.       en[a]++;
  108.     }
  109.     for(int i = 1; i<=n; i++) cin >> blo[i];
  110.     for(int i = 0; i<m; i++){
  111.       int a, b, c; cin >> a >> b >> c;
  112.       adj[a].pb(mp(b, c));
  113.       adj[b].pb(mp(a, c));
  114.     }
  115.     moo("%d\n", dijkstra());
  116.   }
  117.     return 0;
  118. }
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement