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

poj3259

时间:2015-10-27 21:36:40      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 1 #include<iostream>
 2 #include<string.h>
 3 using namespace std;
 4 const int MAX_N=500+5;
 5 const int MAX_M=5200+5;
 6 typedef struct edge
 7 {
 8     int from;
 9     int to;
10     int time;
11 };
12 edge es[MAX_M];
13 int n,m,w;
14 int d[MAX_N];
15 
16 bool find_negative_loop(int countt)
17 {
18     memset(d,1<<26,sizeof(d));
19     for (int i=0;i<n-1;i++)
20     {
21         bool update=false;
22         for (int j=0;j<countt;j++)
23         {
24             edge e=es[j];
25             if (d[e.to]>d[e.from]+e.time)
26             {
27                 d[e.to]=d[e.from]+e.time;
28                 update=true;
29             }
30         }
31         if (!update)    
32             break;    
33     }
34     for (int i=0;i<countt;i++)
35     {
36         edge e=es[i];
37         if (d[e.to]>d[e.from]+e.time)
38         {
39             return true;
40         }
41     }
42     return false;
43 }
44 
45 int main()
46 {
47     //freopen("input.txt","r",stdin);
48     int num;
49     int count;
50     cin>>num;
51     while(num--)
52     {
53         cin>>n>>m>>w;
54         int i;
55         count=0;
56         for (i=0;i<m;i++)
57         {
58             cin>>es[count].from>>es[count].to>>es[count].time;
59             es[count+1].from=es[count].to;
60             es[count+1].to=es[count].from;
61             es[count+1].time=es[count].time;
62             count+=2;
63         }
64         for (;i<m+w;i++)
65         {
66             cin>>es[count].from>>es[count].to>>es[count].time;
67             es[count].time=-es[count].time;
68             count++;
69         }
70         if(find_negative_loop(count))
71             cout<<"YES"<<endl;
72         else
73             cout<<"NO"<<endl;
74     }
75     return 0;
76 }
View Code

照着书里面用了Bellman-Ford算法,测试数据一下子就通过,可是还有这两个问题没有注意到:

1.应该把d数组里元素的值初始化为大数

2.普通路之间可以来回,虫洞不可以

还有啊,每次都忘了把freopen函数注释就提交了!!!!!

poj3259

标签:

原文地址:http://www.cnblogs.com/acmray/p/4915410.html

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