題目傳送:poj3259
注意:本題起點為1,spfa做法:一共n個節(jié)點陨界,每個節(jié)點進入隊列的次數(shù)至多為n-1次栏赴,若進入大于等于n次盈咳,說明圖中存在負(fù)權(quán)回路耿眉,滿足“and return to the starting field a time before his initial departure”的條件,田里道路雙向鱼响,蟲洞單向且為負(fù)邊鸣剪。
題目描述:
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
(1N
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
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES
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.
SPFA:
//#include<bits/stdc++.h>
#include<cstring>
#include<cstdio>
#include<queue>
//#include<iostream>
using namespace std;
const int maxn=1e4;
const int inf=1e9;
int e[maxn][maxn];
//int dis[maxn];
bool spfa(int start,int N){
int book[maxn],sum[maxn],dis[maxn];
memset(book,0,sizeof(book));
memset(sum,0,sizeof(sum));
for(int i=1;i<=N;i++)
dis[i]=inf;
dis[start]=0;
queue<int>q;
//q.clear();//C++中的queue自身不支持clear操作,雙端隊列deque支持clear操作
while(!q.empty())q.pop();
q.push(start);
book[start]=1;//注意不是book[1]=1;
++sum[start];
while(!q.empty()){
int x=q.front();
q.pop();
book[x]=0;
for(int i=1;i<=N;i++){
//if(sum[i]>N)return true;
if(e[x][i]<inf&&dis[i]>dis[x]+e[x][i]){//e[x][i]<inf條件不要忘記加丈积,用鄰接矩陣存圖時筐骇,有邊才能松弛
dis[i]=dis[x]+e[x][i];
if(!book[i]){
book[i]=1;
q.push(i);
++sum[i];
if(sum[i]>=N)return true;//是否加=?
}
}
}
}
return false;
}
int main(){
int F,N,M,W,S,E,T;
int st;
scanf("%d",&F);
for(int k=1;k<=F;k++){
scanf("%d%d%d",&N,&M,&W);
for(int i=1;i<=N;i++){//二維e數(shù)組初始化
for(int j=1;j<=N;j++){
if(i==j)e[i][j]=0;
else e[i][j]=inf;
}
}
for(int i=1;i<=M;i++){//田地里的路桶癣,無說明,注意雙向娘锁,讀入正邊
scanf("%d%d%d",&S,&E,&T);//有重復(fù)邊牙寞?暫假設(shè)沒有
if(e[S][E]>T)
e[S][E]=e[E][S]=T;
if(i==1)st=S;
}
for(int i=1;i<=W;i++){//讀入負(fù)邊
scanf("%d%d%d",&S,&E,&T);
e[S][E]=-T;
//e[S][E]=e[E][S]=-T;//蟲洞的負(fù)邊是單向的
}
//cout<<M<<endl;
if(spfa(st,N))printf("YES\n");
else printf("NO\n");
}
return 0;
}
做完這道題有幾點醒悟:一開始時候總是糾結(jié)于題目沒說明起點,該定哪個點為起點呢莫秆,上面的代碼想了一天從wa到ac,后來終于發(fā)現(xiàn)由于習(xí)慣性思維把1當(dāng)做起點间雀,把book[start]=1;寫成了book[1]=1; 不過也正是這個錯誤讓我突然明白這道題和起點是哪個點一點關(guān)系也沒有,只要圖是聯(lián)通的镊屎,可以設(shè)置任何點為起點惹挟,計算其他點到這個規(guī)定的起點的距離來判斷這個圖是否存在負(fù)環(huán)。而我則很蠢的把第一個輸入的點作為起點來松弛缝驳。所以就一句話连锯,判斷有沒有負(fù)環(huán)就完事了归苍,如果圖是聯(lián)通的管他誰是起點。
vector寫法:
spfa:
#include<iostream>
#include<vector>
#include<queue>
const int INF=1e9;
const int maxn=1e3+10;
using namespace std;
struct edge{
int v;
int w;
}edges[maxn*10];
vector<edge>v[maxn];//v可以看出一個二維數(shù)組
bool book[maxn];
int dis[maxn];
int sum[maxn];
int n,m,w,cnt;
void readedge(int e,int s,int t){//讀入邊
cnt++;
edges[cnt].v=s;
edges[cnt].w=t;
v[e].push_back(edges[cnt]);//v數(shù)組下標(biāo)是起點运怖,該數(shù)組記錄了每個起點的所有邊拼弃,v[e].size()就是以e為起點的所有出邊的數(shù)量
}
bool spfa(){
queue<int>q;
book[1]=1;
dis[1]=0;
sum[1]=1;
for(int i=2;i<=n;i++){
book[i]=0;
dis[i]=INF;
sum[i]=0;
}
q.push(1);
while(!q.empty()){
int x=q.front();
q.pop();
book[x]=0;
for(int i=0;i<v[x].size();i++){
if(dis[v[x][i].v]>dis[x]+v[x][i].w){
dis[v[x][i].v]=dis[x]+v[x][i].w;
if(!book[v[x][i].v]){
book[v[x][i].v]=1;
sum[v[x][i].v]++;
if(sum[v[x][i].v]>=n)//某點入隊的次數(shù)大于等于n,該圖必然存在負(fù)環(huán)
return true;
q.push(v[x][i].v);//把能松弛且不在隊列的點入隊
}
}
}
}
return false;
}
int main(){
int f;
cin>>f;
for(int k=1;k<=f;k++){
cin>>n>>m>>w;
cnt=0;
for(int i=1;i<=n;i++)
v[i].clear();
for(int i=0;i<m;i++){//讀入邊
int s,e,t;
cin>>s>>e>>t;
readedge(s,e,t);
readedge(e,s,t);//道路為雙向摇展,所以要反向讀入一次
}
for(int i=0;i<w;i++){
int s,e,t;
cin>>s>>e>>t;
readedge(s,e,-t);//蟲洞數(shù)據(jù)是單向的負(fù)邊
}
if(spfa())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}