poj3259 Wormholes,poj3259wormholes
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 delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
Line 1: A single integer,
F.
F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively:
N,
M, and
W
Lines 2..
M+1 of each farm: Three space-separated numbers (
S,
E,
T) that describe, respectively: a bidirectional path between
S and
E that requires
T seconds to traverse. Two fields might be connected by more than one path.
Lines
M+2..
M+
W+1 of each farm: Three space-separated numbers (
S,
E,
T) that describe, respectively: A one way path from
S to
E that also moves the traveler back
T seconds.
Output
Lines 1..
F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).
Sample Input
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
Sample Output
NOYES
Hint
For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
題意:N塊地,M條路,W個蟲洞。判斷有沒有可以是時間倒流的路徑。注意把W次輸入的w[i]改成-w[i]。
SPFA:判斷有沒有入隊次數超過N的點。
特別提醒:不要相信給出的資料範圍,這是個大坑!!測試資料絕對有超過很多的,我開了20000的數組。
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<queue>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x3f // 一個大數#define eps 1e-6using namespace std;#define maxn 20000 //數組開大些!#define Maxn 20000int n,m,W;int first[maxn];int next[maxn];int u[Maxn];int v[Maxn];int w[Maxn];int dis[maxn];int go[maxn];int vist[maxn];int conut[maxn];int ttt;queue<int> Q;int spfa(int x){ vist[x]=1; memset(dis,INF,sizeof(dis)); dis[x]=0; Q.push(x); conut[x]++; while(!Q.empty()) { int t=Q.front(); Q.pop(); vist[t]=0; conut[t]++; for(int i=first[t];i!=-1;i=next[i]) { if(dis[t]+w[i]<dis[v[i]]) { dis[v[i]]=dis[t]+w[i]; if(!vist[v[i]]) { if(conut[t]>=n) //如果某點入隊超過n次,則可判定存在負環。。。。 return 1; Q.push(v[i]); ++conut[v[i]]; } } } } return 0;}int main(){ int T; cin>>T; while(T--) { memset(vist,0,sizeof(vist)); memset(conut,0,sizeof(conut)); while(!Q.empty()) Q.pop(); // cout<<"二貨@"<<endl; scanf("%d%d%d",&n,&m,&W); for(int i=1;i<=n;i++) first[i]=-1; for(int i=1;i<=m;i++) { scanf("%d%d%d",&u[i],&v[i],&w[i]); u[i+m]=v[i]; v[i+m]=u[i]; w[i+m]=w[i]; } for(int i=m*2+1;i<=2*m+W;i++) { scanf("%d%d%d",&u[i],&v[i],&w[i]); w[i]=-w[i]; } for(int i=1;i<=2*m+W;i++) { next[i]=first[u[i]]; first[u[i]]=i; } int qq=spfa(1); if(qq) puts("YES"); else puts("NO"); } return 0;}
poj3259 bellman 演算法
看看My Code吧
#include<iostream>
using namespace std;
typedef struct Node
{
int u,v,t;
}Node;
Node e[25000];
int n,m,w,en;
bool bellmanford()
{
bool flag;
int dis[1001];
for(int i=0;i<n-1;i++)
{
flag=false;
for(int j=0;j<en;j++)
if(dis[e[j].v]>dis[e[j].u]+e[j].t)
{
dis[e[j].v]=dis[e[j].u]+e[j].t;
flag=true;
}
if(!flag)
break;
}
for(int i=0;i<en;i++)
if( dis[e[i].v]>dis[e[i].u]+e[i].t)
return true;
return false;
}
int main()
{
int Case ,u,v,t;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&Case);
while(Case--)
{
scanf("%d%d%d",&n,&m,&w);
en=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&t);
e[en].u=u;
e[en].v=v;
e[en++].t=t;
e[en].u=v;
e[en].v=u;
e[en++].t=t;
}
for(int i=1;i<=w;i++)
{
scanf("%d%d%d",&u,&v,&t);
e[en].u=u;
e[en].v=v;
e[en++].t=-t;
}
if(bellmanford())
puts("YES");
else
puts("NO");
}
return 0;
}...餘下全文>>