题目传送:poj3259
注意:本题起点为1,spfa做法:一共n个节点,每个节点进入队列的次数至多为n-1次,若进入大于等于n次,说明图中存在负权回路,满足“and return to the starting field a time before his initial departure”的条件,田里道路双向,虫洞单向且为负边。
题目描述:
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
(1N500)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
#include
#include
#include
//#include
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;
queueq;
//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]dis[x]+e[x][i]){//e[x][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数组初始化
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);//有重复边?暂假设没有
if(e[S][E]>T)
e[S][E]=e[E][S]=T;
if(i==1)st=S;
}
for(int i=1;i<=W;i++){//读入负边
scanf("%d%d%d",&S,&E,&T);
e[S][E]=-T;
//e[S][E]=e[E][S]=-T;//虫洞的负边是单向的
}
//cout<
做完这道题有几点醒悟:一开始时候总是纠结于题目没说明起点,该定哪个点为起点呢,上面的代码想了一天从wa到ac,后来终于发现由于习惯性思维把1当做起点,把book[start]=1;写成了book[1]=1; 不过也正是这个错误让我突然明白这道题和起点是哪个点一点关系也没有,只要图是联通的,可以设置任何点为起点,计算其他点到这个规定的起点的距离来判断这个图是否存在负环。而我则很蠢的把第一个输入的点作为起点来松弛。所以就一句话,判断有没有负环就完事了,如果图是联通的管他谁是起点。
vector写法:
spfa:
#include
#include
#include
const int INF=1e9;
const int maxn=1e3+10;
using namespace std;
struct edge{
int v;
int w;
}edges[maxn*10];
vectorv[maxn];//v可以看出一个二维数组
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数组下标是起点,该数组记录了每个起点的所有边,v[e].size()就是以e为起点的所有出边的数量
}
bool spfa(){
queueq;
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;idis[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)//某点入队的次数大于等于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>s>>e>>t;
readedge(s,e,t);
readedge(e,s,t);//道路为双向,所以要反向读入一次
}
for(int i=0;i>s>>e>>t;
readedge(s,e,-t);//虫洞数据是单向的负边
}
if(spfa())
cout<<"YES"<