H - Wormholes

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.

題意:給出普通的點,邊和權值,以及蟲洞的點,邊,權值,問你是否能從某一點出發最後回到出發點,並且回到去的時間要在出發前。

先建邊,普通邊正值,蟲洞邊負值,接着對每一個點跑一次最短路(spfa),如果存在負環,就是成立的情況。

代碼:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
using namespace std;
const int MAXN=505;
const int MAXM=800005;
bool vis[MAXN];
int cnt[MAXN];
int dir[MAXN];
struct Edge
{
    int v;
    double w;
};
vector<Edge> edge[MAXN];
void add(int u,int v,int w)
{
    Edge tmp;
    tmp.v=v;
    tmp.w=w;
    edge[u].push_back(tmp);
}

bool spfa(int from,int n)
{
    memset(vis,false,sizeof(vis));
    memset(dir,0x3f,sizeof(dir));
    memset(cnt,0,sizeof(cnt));
    dir[from]=0;
    vis[from]=true;
    queue<int>que;
    while(!que.empty())
    {
        que.pop();
    }
    que.push(from);
    while(!que.empty())
    {
        int now=que.front();
        que.pop();
        vis[now]=false;
        for(int i=0;i<edge[now].size();i++)
        {
            if(dir[edge[now][i].v]>edge[now][i].w+dir[now])
                {
                    dir[edge[now][i].v]=edge[now][i].w+dir[now];
                    if(!vis[edge[now][i].v])
                    {
                        que.push(edge[now][i].v);
                        vis[edge[now][i].v]=true;
                        if(++cnt[edge[now][i].v]>n)
                            return false;
                    }
                }
        }
    }
    return true;
}
int main (void)
{
    int num;
    cin>>num;
    while(num--)
    {
        int N,M,W;
        int s,e,t;
        for(int i=1;i<=N;i++)
        {
            edge[i].clear();
        }
        scanf("%d %d %d",&N,&M,&W);
        for(int i=1;i<=M;i++)
        {
            scanf("%d %d %d",&s,&e,&t);
            add(s,e,t);
            add(e,s,t);
        }
        for(int i=0;i<W;i++)
        {
            scanf("%d %d %d",&s,&e,&t);
            add(s,e,-t);
        }
        bool re;
        for(int i=1;i<=N;i++)
        {
            re=spfa(i,N);
            if(re==false)
                {
                    break;
                }
        }
        if(re==false)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章