Wormholes
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 64467 | Accepted: 24039 |
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
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.
题意:
发现我概括不了题意。。那就通俗一点吧,n个城市,m条正的双向边和w条负的单向边,问你能否找到至少一个环让这个环的值为负数。
做法:
很明显就是找负环,发现学了这么久的图,还是只知道spfa可以判负环但是怎么判有点忘记了。就写个东西小小的总结一下,这里有两种方法,bfs和dfs,这道题来说的话bfs跑的会快一点(虽然我也不知道为什么)。bfs很简单,如果一个点被重复跑了超过n次,那么就可以判定存在负环。为什么一定是超过n次呢,我自己想了一下稍微画了一个图,不怎么好看见谅。
因为你不知道这个图的访问顺序,那么我们就考虑最差的情况,即点3被跑的次数最多的情况。 最差的情况当然是1先访问一次3,再从4访问一次,最后再从2访问一次,所以这个图中同一个点被访问了3次(n-1次),但是图中连环都不存在,我暂时画不出有n次不合法的图,但是次数太少显然是不够的。
还有一种dfs的做法,直接对一个点进行深搜,如果一个点可以更新其他的点但是在之前它已经更新过其他点了,那么也是不合法的。dfs的这种做法的原因我还没理解。。如果有清楚的旁友麻烦私戳我一下哈。。
bfs做法:
219ms
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int maxn=5050;
int m,n,w,head[maxn],now,flag,vis[maxn],dist[maxn],ct[maxn];
struct node{
int to,next,w;
}e[maxn*5];
void add(int u,int v,int w){
e[now].to=v,e[now].next=head[u];
e[now].w=w; head[u]=now++;
}
void init(){
now=0; flag=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(dist,125,sizeof(dist));
memset(ct,0,sizeof(ct));
}
void spfa(int x){
queue<int> q;
dist[x]=0;
vis[x]=1;
q.push(x);
while(!q.empty()){
int u=q.front(); q.pop();
if(++ct[u]>n) {
flag=1;
break;
}
vis[u]=0;
for(int i=head[u];~i;i=e[i].next){
int t=e[i].to,w=e[i].w;
if(dist[t]-w>dist[u]){
dist[t]=w+dist[u];
if(!vis[t]){
vis[t]=1;
q.push(t);
}
}
}
}
}
int main(){
int t,x,y,c;
cin>>t;
while(t--){
init();
scanf("%d%d%d",&n,&m,&w);
for(int i=0;i<m;i++){
scanf("%d%d%d",&x,&y,&c);
add(x,y,c); add(y,x,c);
}
for(int i=0;i<w;i++){
scanf("%d%d%d",&x,&y,&c);
add(x,y,-c);
}
spfa(1);
if(flag) puts("YES");
else puts("NO");
}
return 0;
}
dfs做法:
532ms
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=5050;
int m,n,w,head[maxn],now,flag,vis[maxn],dist[maxn];
struct node{
int to,next,w;
}e[maxn*5];
void add(int u,int v,int w){
e[now].to=v,e[now].next=head[u];
e[now].w=w; head[u]=now++;
}
void init(){
now=0; flag=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(dist,125,sizeof(dist));
}
void dfs_spfa(int x){
if(flag) return ;
vis[x]=1;
for(int i=head[x];~i;i=e[i].next){
int t=e[i].to,w=e[i].w;
if(dist[t]-w>dist[x]){
dist[t]=w+dist[x];
if(vis[t]&&!flag){
flag=1;
break;
}
else {
vis[t]=1;
dfs_spfa(t);
}
}
}
vis[x]=0;
}
int main(){
int t,x,y,c;
cin>>t;
while(t--){
init();
scanf("%d%d%d",&n,&m,&w);
for(int i=0;i<m;i++){
scanf("%d%d%d",&x,&y,&c);
add(x,y,c); add(y,x,c);
}
for(int i=0;i<w;i++){
scanf("%d%d%d",&x,&y,&c);
add(x,y,-c);
}
dist[1]=0;
dfs_spfa(1);
if(flag) puts("YES");
else puts("NO");
}
return 0;
}