标签:while span continue name main lag tps ace clear
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 305, maxm = 505; 4 const int inf = 0x3f3f3f3f; 5 struct edge { 6 int v, w; 7 }; 8 vector<edge> maps[maxn]; 9 int dis[maxn], n, m; 10 11 bool BellmanFord(int s) { // s为源点 12 fill(dis, dis+n, inf); 13 dis[s] = 0; 14 for (int i = 0; i < n-1; i++) { 15 for (int u = 0; u < n; u++) { 16 for (int j = 0; j < maps[u].size(); j++) { 17 int v = maps[u][j].v; 18 int w = maps[u][j].w; 19 if (dis[u] + w < dis[v]) { 20 dis[v] = dis[u] + w; 21 } 22 } 23 } 24 } 25 for (int u = 0; u < n; u++) { 26 for (int j = 0; j < maps[u].size(); j++) { 27 int v = maps[u][j].v; 28 int w = maps[u][j].w; 29 if (dis[u] + w < dis[v]) 30 return false; 31 } 32 } 33 return true; 34 } 35 int main() { 36 int t; scanf("%d",&t); 37 while (t--) { 38 scanf("%d%d",&n,&m); 39 for (int i = 0; i < n; i++) maps[i].clear(); 40 41 for (int i = 1; i <= m; i++) { 42 int u, v, w; scanf("%d%d%d",&u,&v,&w); 43 maps[u].push_back(edge{v,w}); 44 } 45 for (int i = 1; i <= 6; i++) { 46 int u, v; scanf("%d%d",&u,&v); 47 bool flag = BellmanFord(v); 48 printf("%d\n",-dis[u]); 49 50 //if (flag == false) continue; 51 maps[u].push_back(edge{v,-dis[u]}); 52 } 53 } 54 }
标签:while span continue name main lag tps ace clear
原文地址:https://www.cnblogs.com/wstong/p/11743512.html