Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 44207 | Accepted: 16258 |
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 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
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 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.
Source
这道题一开始忘记贴代码了,有点尴尬...题意:FJ在他的农场里发现有虫洞,每个虫洞可以让人从S传送到E,并且时间回退T,现在给出农场中有n个点,m条双向道路和w个单向虫洞连接这些点,询问能否从某个点出发,当他会到原点时,时间退回负值(注意:经过道路的时间是正值,经过虫洞的时间是负值)
题解:先用并查集处理下联通块,因为这不是单源最短路,所以要每个联通块都找一遍,然后用SPFA找负环,把时间设为距离,如果有负环,那么经过这个负环,时间肯定是回退的,设期望值为20,如果某个点进入队列超过这个期望值,就表示图里面有负环,FJ可以回到过去,输出YES,如果没有负环,则输出NO
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
#include<functional>
#include<iostream>
using namespace std;
const int maxn = 505;
const int maxn_edge = 6005;
const int NN = 20;
struct Edge{
int v,cost,nxt;
}edge[maxn_edge];
int head[maxn],tot,d[maxn],p[maxn],cnt[maxn];
bool vis[maxn];
void add(int u,int v,int cost){
edge[tot].v=v;
edge[tot].cost=cost;
edge[tot].nxt=head[u];
head[u]=tot++;
// cout<<u<<' '<<v<<' '<<cost<<' '<<endl;
}
void init(){
memset(d,0x3f,sizeof(d));
memset(vis,0,sizeof(vis));
memset(cnt,0,sizeof(cnt));
}
int find(int x){return p[x]==x?x:(p[x]=find(p[x]));}
int main(){
int T,n,m,w;
freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--){
memset(head,-1,sizeof(head));
tot=0;
scanf("%d%d%d",&n,&m,&w);
for(int i=1;i<=n;i++) p[i]=i;
for(int i=0;i<m;i++){
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
add(u,v,cost);
add(v,u,cost);
int p1=find(u),p2=find(v);
if(p1!=p2) p[p2]=p1;
}
for(int i=0;i<w;i++){
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
add(u,v,-cost);
int p1=find(u),p2=find(v);
if(p1!=p2) p[p2]=p1;
}
bool flag=0;
for(int st=1;st<=n;st++){
if(flag) break;
if(find(st)!=st) continue;
init();
queue<int>q;
q.push(st);
d[st]=0;
while(!q.empty()){
if(flag) break;
int u=q.front();q.pop();
vis[u]=0;
for(int i=head[u];~i;i=edge[i].nxt){
int v=edge[i].v;
if(d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
if(!vis[v]){
vis[v]=1;
cnt[v]++;
q.push(v);
if(cnt[v]>NN) {flag=1;break;}
}
}
}
}
}
// printf("%d\n",d[n]);
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}