码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3259负环判定

时间:2016-07-19 13:37:53      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

题意:有n个顶点,m条边,然后有w个洞,过每个洞的时间为-ti,求是否会时光倒流

分析:就是求是否存在负圈,用Bellman-Floyd判定是否存在负圈即可,注意是无向图,所以路径是双向可达的

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <vector>
 6 #include <algorithm>
 7 #include <set>
 8 #include <map>
 9 #include <bitset>
10 #include <cmath>
11 #include <queue>
12 #include <stack>
13 using namespace std;
14 const int maxn=8000;
15 const int INF=1<<25;
16 struct edge{
17     int from,to,cost;
18 };
19 edge es[maxn];
20 int n,m,w;
21 int d[maxn];
22 
23 bool find_negative_loop(){
24     memset(d,0,sizeof(d));
25     for(int i=0;i<n;i++){
26         for(int j=0;j<m*2+w;j++){
27             edge e=es[j];
28             if(d[e.to]>d[e.from]+e.cost){
29                 d[e.to]=d[e.from]+e.cost;
30 
31                 if(i==n-1)  return true;
32             }
33             //cout<<d[e.to]<<endl;
34         }
35     }
36     return false;
37 }
38 
39 int main()
40 {
41     int t;
42     cin>>t;
43     while(t--)
44     {
45         scanf("%d%d%d",&n,&m,&w);
46         for(int i=0;i<m*2;i+=2)
47         {
48             int s,e,t;
49             scanf("%d%d%d",&s,&e,&t);
50             es[i].from=s,es[i].to=e,es[i].cost=t;
51             es[i+1].from=e,es[i+1].to=s,es[i+1].cost=t;
52         }
53         for(int i=m*2;i<w+m*2;i++)
54         {
55             int s,e,t;
56             scanf("%d%d%d",&s,&e,&t);
57             es[i].from=s,es[i].to=e,es[i].cost=-t;
58         }
59         if(find_negative_loop())  cout<<"YES"<<endl;
60         else  cout<<"NO"<<endl;
61     }
62     return 0;
63 }
View Code

 

POJ3259负环判定

标签:

原文地址:http://www.cnblogs.com/wolf940509/p/5684340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!