Wormholes http://poj.org/problem?id=3259

spfa 2e

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #define mt(a,b) memset(a,b,sizeof(a))
 6 using namespace std;
 7 const int inf=0x3f3f3f3f;
 8 const int M=1024;
 9 class Spfa { ///单源最短路o(2*ME)
10     typedef int typec;///边权的类型
11     static const int ME=1000010;///边的个数
12     static const int MV=1000010;///点的个数
13     struct E {
14         int v,next;
15         typec w;
16     } e[ME];
17     int n,le,head[MV],inque[MV];
18     typec dist[MV];
19     bool used[MV];
20     queue<int> q;
21 public:
22     void init(int tn) { ///传入点的个数
23         n=tn;
24         le=0;
25         mt(head,-1);
26     }
27     void add(int u,int v,typec w) {
28         e[le].v=v;
29         e[le].w=w;
30         e[le].next=head[u];
31         head[u]=le++;
32     }
33     bool solve(int s) { ///传入起点,下标0开始,存在负环返回false
34         for(int i=0; i<n; i++) {
35             dist[i]=inf;
36             used[i]=true;
37             inque[i]=0;
38         }
39         used[s]=false;
40         dist[s]=0;
41         inque[s]++;
42         while(!q.empty()) q.pop();
43         q.push(s);
44         while(!q.empty()) {
45             int u=q.front();
46             q.pop();
47             used[u]=true;
48             for(int i=head[u]; ~i; i=e[i].next) {
49                 int v=e[i].v;
50                 if(dist[v]>dist[u]+e[i].w) {
51                     dist[v]=dist[u]+e[i].w;
52                     if(used[v]) {
53                         used[v]=false;
54                         q.push(v);
55                         inque[v]++;
56                         if(inque[v]>n) return false;
57                     }
58                 }
59             }
60         }
61         return true;
62     }
63     typec getdist(int id) {
64         return dist[id];
65     }
66 } gx;
67 
68 int main() {
69     int t,n,m,w,x,y,z;
70     while(~scanf("%d",&t)) {
71         while(t--){
72             scanf("%d%d%d",&n,&m,&w);
73             gx.init(n);
74             while(m--) {
75                 scanf("%d%d%d",&x,&y,&z);
76                 x--;
77                 y--;
78                 gx.add(x,y,z);
79                 gx.add(y,x,z);
80             }
81             while(w--){
82                 scanf("%d%d%d",&x,&y,&z);
83                 x--;
84                 y--;
85                 gx.add(x,y,-z);
86             }
87             if(!gx.solve(0)) puts("YES");
88             else puts("NO");
89         }
90     }
91     return 0;
92 }
View Code

相关文章:

  • 2022-12-23
  • 2021-07-23
  • 2022-12-23
  • 2021-09-22
  • 2022-01-12
  • 2022-12-23
猜你喜欢
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
  • 2021-11-14
  • 2021-06-27
相关资源
相似解决方案