Poj 3259 wormholes judge negative weight Loop

Source: Internet
Author: User
Wormholes

Time limit:2000 MSMemory limit:65536 KB

64-bit integer Io format:% I64d, % i64uJava class name:Main

[Submit] [Status] [discuss]

Description

While processing his own 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 comprisesN(1 ≤N≤ 500) fields conveniently numbered 1 ..N,M(1 ≤M≤ 2500) paths, andW(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 mapsF(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.

Inputline 1: A single integer, F. FFarm descriptions follow. line 1 of each farm: three space-separated integers respectively: N, M, And WLines 2 .. M+ 1 of each farm: three space-separated numbers ( S, E, T) That describe, respectively: a bidirectional path SAnd EThat requires TSeconds 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 STo EThat also moves the traveler back TSeconds. outputlines 1 .. F: For each farm, output "yes" if FJ can achieve his goal, otherwise output "no" (do not include the quotes). sample input
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
Sample output
NOYES
Hintfor farm 1, FJ cannot travel back in time. for Farm 2, FJ cocould travel back in time by the cycle 1-> 2-> 3-> 1, arriving back at his starting location 1 second before he leaves. he cocould start from anywhere on the cycle to accomplish this.
// Memory time // 308 K 204 MS # include <iostream> # include <string. h> using namespace STD; int dis [1001]; // source point to each point weight const int max_w = 10001; // infinite distance struct weight {int s; int e; int t;} edge [5200]; int n, m, w_h; // n (1 ≤ n ≤ 500) fields vertex count // M (1 ≤ m ≤ 2500) paths positive weight bidirectional edge // w_h (1 ≤ W ≤ 200) wormholes wormhole (backtracking), negative weight unidirectional edge int all_e; // edge set (total number of edges) bool Bellman () {bool flag;/* Relax */For (INT I = 0; I <N-1; I ++) // dis relaxation count {flag = false; for (Int J = 0; j <All_e; j ++) // All edge sets if (DIS [edge [J]. e]> dis [edge [J]. s] + edge [J]. t) // you can relax and update {dis [edge [J]. e] = dis [edge [J]. s] + edge [J]. t; flag = true; // relax updates the path} If (! Flag) break; // as long as a relax is not updated, it indicates that the shortest path has been searched, or some points are not reachable, you can jump out of relax} // All edge sets have been updated and are the shortest path between two points/* search negative circle */For (int K = 0; k <all_e; k ++) // traverse all edge sets. If the edge can be updated to the shortest again, the negative weight loop appears if (DIS [edge [K]. e]> dis [edge [K]. s] + edge [K]. t) return true; return false;} int main (void) {int U, V, W; int F; CIN> F; while (f --) {memset (DIS, max_w, sizeof (DIS); // The initial value from the source point to each point is infinite, that is, the default value is not connected to CIN> N> m> w_h; all_e = 0; // initialize the pointer/* re Ad in positive paths */For (INT I = 1; I <= m; I ++) {CIN> U> V> W; edge [all_e]. S = edge [all_e + 1]. E = u; edge [all_e]. E = edge [all_e + 1]. S = V; edge [all_e ++]. T = W; edge [all_e ++]. T = W; // because of the bidirectional nature of paths, the weights in both directions are equal. Pay attention to pointer movement}/* read in negative wormholds */For (Int J = 1; j <= w_h; j ++) {CIN> U> V> W; edge [all_e]. S = u; edge [all_e]. E = V; edge [all_e ++]. T =-W; // note that the weight is negative}/* Bellman-Ford algorithm */If (bellman () cout <"yes" <Endl; else C Out <"no" <Endl;} return 0;} // memory time // 308 K 204 MS # include <iostream> # include <string. h> using namespace STD; int dis [1001]; // source point to each point weight const int max_w = 10001; // infinite distance struct weight {int s; int e; int t;} edge [5200]; int n, m, w_h; // n (1 ≤ n ≤ 500) fields vertex count // M (1 ≤ m ≤ 2500) paths positive weight bidirectional edge // w_h (1 ≤ W ≤ 200) wormholes wormhole (backtracking), negative weight unidirectional edge int all_e; // edge set (total number of edges) bool Bellman () {bool flag;/* Relax */For (INT I = 0; I <N-1; I ++) // dis relaxed Count {flag = false; For (Int J = 0; j <all_e; j ++) // All edge sets if (DIS [edge [J]. e]> dis [edge [J]. s] + edge [J]. t) // you can relax and update {dis [edge [J]. e] = dis [edge [J]. s] + edge [J]. t; flag = true; // relax updates the path} If (! Flag) break; // as long as a relax is not updated, it indicates that the shortest path has been searched, or some points are not reachable, you can jump out of relax} // All edge sets have been updated and are the shortest path between two points/* search negative circle */For (int K = 0; k <all_e; k ++) // traverse all edge sets. If the edge can be updated to the shortest again, the negative weight loop appears if (DIS [edge [K]. e]> dis [edge [K]. s] + edge [K]. t) return true; return false;} int main (void) {int U, V, W; int F; CIN> F; while (f --) {memset (DIS, max_w, sizeof (DIS); // The initial value from the source point to each point is infinite, that is, the default value is not connected to CIN> N> m> w_h; all_e = 0; // initialize the pointer/* read in positive paths */For (INT I = 1; I <= m; I ++) {CIN> U> V> W; edge [all_e]. S = edge [all_e + 1]. E = u; edge [all_e]. E = edge [all_e + 1]. S = V; edge [all_e ++]. T = W; edge [all_e ++]. T = W; // because of the bidirectional nature of paths, the weights in both directions are equal. Pay attention to pointer movement}/* read in negative wormholds */For (Int J = 1; j <= w_h; j ++) {CIN> U> V> W; edge [all_e]. S = u; edge [all_e]. E = V; edge [all_e ++]. T =-W; // note that the weight is negative}/* Bellman-Ford algorithm */If (bellman () cout <"yes" <Endl; else cout <"no" <Endl;} return 0 ;}

 

Poj 3259 wormholes judge negative weight Loop

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.