poj3259

 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 }

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

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

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

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

时间: 2025-01-20 04:41:49

poj3259的相关文章

poj3259(Wormholes)

题目大意: 一个农夫在农场发现了许多奇怪的虫洞,这些虫洞是单向的,已知N个农场,M条正常的路径,W条虫洞,虫洞可以使时间倒流,农主通过正常的路径花费一定的时间,这个路径是双向的, 通过虫洞的路径可以使时间倒流,农主是狂热的旅行者,他的目的是通过正常路径和虫洞,能否回到最初的起点的时刻! 解题思路: bellman_ford算法,看是否有负权回路,有负权回路说明农主到达最初出发的农场"1"的权值小于0,也就意味着农主能通过虫洞的时光倒流回到最初出发农场的那个时刻,注意单向边和双向边,和数

poj3259 Wormholes --- spfa判负环

又写了个bellman模板一直RE求解啊... #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x

POJ3259 Wormholes 【Bellmanford判断是否存在负回路】

很简单的bellmanford题目,这里比较详细:http://blog.csdn.net/lyy289065406/article/details/6645790 直接代码 #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace s

POJ3259——Wormholes(Bellman-Ford+SPFA)

Wormholes DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the worm

POJ3259 Wormholes

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 44261   Accepted: 16285 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that d

poj3259(spfa)

自己的第一道spfa,纪念一下,顺便转载一下spfa的原理.先po代码: #include <iostream> #include <queue> using namespace std; const int MAX = 999999; const int MAXN = 501; int minimum(int a, int b){ return a > b ? b : a; } int main() { int t; cin >> t; while (t--){

poj3259 spfa

spfa判断是否存在负环,path双向,wormhole单向

poj3259 Bellman_Ford算法

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34465   Accepted: 12585 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

poj3259(最短路+负权处理)

题意: N,个点,M条路,W个虫洞,虫洞的边就是负的 如果出现负环代表YES #include <iostream> #include <string.h> #include <queue> #include <vector> #include <utility> #include <cstdio> #include <cstring> #include <algorithm> using namespace