The Preliminary Contest for ICPC Asia Nanjing 2019 H. Holy Grail
题目链接:https://nanti.jisuanke.com/t/41305
题目说的很明白。。。只需要反向跑spfa然后输入-dis,然后添-dis的一条边就好了。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #include <map> 7 #include <cmath> 8 #include <iomanip> 9 using namespace std; 10 11 typedef long long LL; 12 #define inf 1e14 13 #define rep(i, j, k) for (int i = (j); i <= (k); i++) 14 #define rep__(i, j, k) for (int i = (j); i < (k); i++) 15 #define per(i, j, k) for (int i = (j); i >= (k); i--) 16 #define per__(i, j, k) for (int i = (j); i > (k); i--) 17 18 const int N = 310; 19 int head[N]; 20 int cnt; 21 bool vis[N]; 22 LL dis[N]; 23 queue<int > que; 24 int n,m; 25 26 struct Edge{ 27 int to; 28 LL w; 29 int next; 30 }e[510]; 31 32 void add(int u,int v,LL w){ 33 e[cnt].to = v; 34 e[cnt].w = w; 35 e[cnt].next = head[u]; 36 head[u] = cnt++; 37 } 38 39 void spfa(int s){ 40 41 while(!que.empty()) que.pop(); 42 rep__(i,0,n){ 43 vis[i] = false; 44 dis[i] = inf; 45 } 46 47 dis[s] = 0; 48 que.push(s); 49 50 int u,v; 51 LL w; 52 while(!que.empty()){ 53 u = que.front(); 54 // cout << "u " << u << endl; 55 que.pop(); 56 vis[u] = false; 57 for(int o = head[u]; ~o; o = e[o].next){ 58 v = e[o].to; 59 w = e[o].w; 60 if(dis[v] > dis[u] + w){ 61 dis[v] = dis[u] + w; 62 if(!vis[v]){ 63 vis[v] = true; 64 que.push(v); 65 } 66 } 67 } 68 } 69 } 70 71 int main(){ 72 73 int T; 74 scanf("%d",&T); 75 76 int u,v; 77 LL w; 78 rep(o,1,T){ 79 scanf("%d%d",&n,&m); 80 81 rep__(i,0,n) head[i] = -1; 82 cnt = 0; 83 84 rep(i,1,m){ 85 scanf("%d%d%lld",&u,&v,&w); 86 add(u,v,w); 87 } 88 89 // rep__(i,o,n){ 90 // cout << " u " << i << endl; 91 92 // for(int o = head[i]; ~o; o = e[o].next){ 93 // cout << e[o].to << " " << e[o].w << endl; 94 // } 95 // } 96 97 rep(i,1,6){ 98 scanf("%d%d",&u,&v); 99 spfa(v); 100 printf("%lld\n",-dis[u]); 101 add(u,v,-dis[u]); 102 } 103 } 104 105 getchar();getchar(); 106 return 0; 107 }
1