Wormholes(最短路 + SPFA + 邻接表 + 队列)

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27163   Accepted: 9777

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..NM (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,  FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively:  NM, and  W 
Lines 2.. M+1 of each farm: Three space-separated numbers ( SET) 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 ( SET) 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.

 

     题意:

     给出 T(1 ~ 5) 个 case,每个 case  首先给出 3 个数,n(1 ~ 500),m(1 ~ 2500),w(1 ~ 200)。代表有 n 个地方,m 条路,w 个虫洞。虫洞的能让时间倒退回 k ,后给出这 m 条路和 w 个虫洞,每一条路都给出 S,E,T,代表 S 和 E 路之间需要花费 T (不超过10000)的时间。结束后给每一个虫洞的S,E,T,代表S 到 E 可以倒退 T (不超过10000)的时间。问能否找到能回到自己的路径,就是找一条能够比刚开始出发的时间还要少的路径。

 

     思路:

     最短路。SPFA即可。读老半天没意味出来是什么意思,能返回自己就是存在负环,因为负环每循环一圈这圈内的每个节点都会减少。要注意 M 是双向边,W 是单向边,添加完M后再添加W即可,所以可能会出现重边的情况,但是也不影响,因为每次松弛都会算出最短的那条。最后判断节点是否入队 超过 n 次即可,如果是的话说明存在负环。还需注意一点就是,因为是双向边,所以边的总数是 2500 * 2 + 200,故有关于边的数组范围要 >= 5200。

 

     AC:

#include <cstdio>
#include <algorithm>
#include <queue>
#include <string.h>
#define MAXN 5500
#define INF 99999999
using namespace std;

int v[MAXN],w[MAXN],fir[505],next[MAXN];
int d[505],vis[505],time[505];
int ind,n;

void add_edge(int a,int b,int val) {
    v[ind] = b;
    w[ind] = val;
    next[ind] = fir[a];
    fir[a] = ind;
    ind++;
}

int SPFA() {
    memset(vis,0,sizeof(vis));
    memset(time,0,sizeof(time));
    for(int i = 1;i <= n;i++) d[i] = INF;
    queue<int> q;
    d[1] = 0;
    q.push(1);
    time[1]++;
    while(!q.empty()) {
        int x = q.front();q.pop();
        vis[x] = 0;
        for(int e = fir[x];e != -1;e = next[e]) {
            if(d[v[e]] > d[x] + w[e]) {
               d[v[e]] = d[x] + w[e];
               if(!vis[v[e]]){   //这里一开始写!vis[x],纯粹手抽
                   q.push(v[e]);
                   vis[v[e]] = 1;
                   time[v[e]]++;
                   if(time[v[e]] >= n)   return 0;
               }
            }
        }
    }
    return 1;
}

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        int m,w;
        ind = 0;
        memset(fir,-1,sizeof(fir));
        scanf("%d%d%d",&n,&m,&w);
        while(m--) {
            int a,b,val;
            scanf("%d%d%d",&a,&b,&val);
            add_edge(a,b,val);
            add_edge(b,a,val);
        }

        while(w--) {
            int a,b,val;
            scanf("%d%d%d",&a,&b,&val);
            add_edge(a,b,-val);
        }

        if(SPFA())  puts("NO");
        else        puts("YES");
    }
    return 0;
}

 

 

 

 

 

 

 

 

你可能感兴趣的:(SPFA)