F - Wormholes POJ - 3259

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.. MW+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.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)        for(ll  i=a;i<b;i++)
#define dec(i,a,b)        for(ll  i=a;i>=b;i--)
#define pb              push_back
#define mp              make_pair
using namespace std;
int dir[5][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } ,{ 0,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int N = 2500 * 2 + 200 + 10;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}

int n, m, w;
struct edge
{
    int u, v;
    int t;
};

edge e[N];

bool bf()
{
    vector<int> d(510, 10000);
    d[1] = 0;

    for (int i = 1; i < n; i++)
    {
        int fg = 1;
        for (int j = 0; j < 2 * m + w; j++)
        {
            int u = e[j].u;
            int v = e[j].v;
            int t = e[j].t;

            if (d[v] > d[u] + t)
            {
                d[v] = d[u] + t;
                fg = 0;
            }
        }
        if (fg)
            return false;
    }
    for(int i=0;i<2 * m + w; i++)
    {
        if (d[e[i].v] > d[e[i].u] + e[i].t)
            return true;
    }
    return false;
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> m >> w;
        int u, v, t;
        int tmp = 0;
        for (int i = 1; i <= m; i++)
        {
            cin >> u >> v >> t;
            e[tmp].u = u;
            e[tmp].v = v;
            e[tmp++].t = t;

            e[tmp].u = v;
            e[tmp].v = u;
            e[tmp++].t = t;
        }
        for (int i = 1; i <= w; i++)
        {
            cin >> u >> v >> t;
            e[tmp].u = u;
            e[tmp].v = v;        
            e[tmp++].t = -t;
        }
        if (bf())
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

 

 

posted @ 2020-04-13 22:14  DeaL57  阅读(111)  评论(0)    收藏  举报