POJ 3259 wormholes

Source: Internet
Author: User

Topic Connection

http://poj.org/problem?id=3259

Wormholesdescription

While exploring he many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it's a one-way path that delivers the IT destination at a time that's before you entered the wormhole! Each of the 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 a avid time-traveling fan, he wants to does the following:start at some field, travel through some paths and worm Holes, and return to the starting field a time before his initial departure. Perhaps he'll be able to meet himself:).

To help FJ find out whether this is possible or not, he'll supply you with complete maps to F (1≤ f ≤ 5) of his farms. No paths'll take longer than seconds to travel and no wormhole can bring FJ back in time by more than-seco Nds.

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. The might is connected by more than one path.
Lines M+2.. M+W+1 of each farm:three space-separated numbers (S, E, T) that D Escribe, respectively:a One, path from S to E . Also moves the traveler back T sec Onds.

Output

Lines 1.. F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (does 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

SPFA judgment negative ring.

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <queue> #include <map>using std::map;using std::min;using std:: Find;using std::p air;using std::queue;using std::vector;using std::multimap; #define PB (E) push_back (e) #define SZ (c) ( int) (c). Size () #define MP (A, b) Make_pair (A, B) #define ALL (c) (c). Begin (), (c). End () #define ITER (c) __typeof ((c). Begin () ) #define CLS (arr, Val) memset (arr, Val, sizeof (arr)) #define Cpresent (C, E) (Find (All (c), (e))! = (c). End ()) #define REP (I, n) for (int i = 0; i < (int.) n; i++) #define TR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) const int n = 510;c onst int INF = 0x3f3f3f3f;struct SPFA {struct Edge {int to, w, next;}    G[n * 10];    BOOL Vis[n];    int tot, inq[n], head[n], dist[n];        inline void init (int n) {tot = 0;            Rep (i, n + 1) {Head[i] =-1;            Inq[i] = vis[i] = 0;        Dist[i] = INF; }   } inline void Add_edge (int u, int v, int w) {G[tot] = (edge) {V, W, Head[u]}; head[u] = tot++;        } inline void built (int m, int w) {int u, V, x;            while (m--) {scanf ("%d%d%d", &u, &v, &x);        Add_edge (U, V, x), Add_edge (V, u, x);            } while (w--) {scanf ("%d%d%d", &u, &v, &x);        Add_edge (U, V,-X);        }} inline bool Spfa (int n, int s = 1) {queue<int> q;        Q.push (s);        Dist[s] = 0, vis[s] = true;            while (!q.empty ()) {int u = q.front (); Q.pop ();            Vis[u] = false;                for (int i = head[u]; ~i; i = g[i].next) {Edge &e = G[i];                    if (dist[e.to] > Dist[u] + e.w) {dist[e.to] = Dist[u] + e.w;                        if (!vis[e.to]) {vis[e.to] = true;                        Q.push (e.to);                    inq[e.to]++;        }        } if (Inq[e.to] > N) return true;    }} return false;        } inline void Solve (int n, int m, int w) {init (n), built (M, W); Puts (SPFA (n)?    "YES": "NO");    }}go;int Main () {#ifdef LOCAL freopen ("In.txt", "R", stdin);    Freopen ("OUT.txt", "w+", stdout); #endif int T, N, M, W;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n, &m, &w);    Go.solve (N, M, W); } return 0;}

Poj 3259 wormholes

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.