POJ 3259 wormholes

Source: Internet
Author: User

POJ 3259 wormholes[★★☆☆☆] graph theory Shortest way bellman
    • Main topic:

      The essence is to find out whether the graph has a negative ring. That is, how to find out whether a graph contains a negative ring.
      It is important to note that the path in the input is bidirectional and wormhole is one-way

    • Sample Example

      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

      Output:
      NO
      YES

    • Problem Solving Ideas:

      This problem is good, my optimization of a condition led to WA, or too young.
      But I feel this problem is wrong ... Oh, my God--
      There is my code at the end of the set of samples, a lot of online code is wrong, but to turn to AC, fans.

    • Code
#include <iostream>#include <algorithm>using namespace STD;typedef Long LongllConst intINF =1e18+7;intN, M, W;structEdge {intStart, end; ll time;}; Edge e[5500];intCte;ll d[ -];BOOLBellmanints) {if(D[s]! = INF)return 0;//Optimization     for(inti =1; I <= N;    i++) {D[i] = INF; } D[s] =0;intI for(i =1; I <= N; i++) {BOOLUpdate =0; for(intj =0; J < CTE; J + +) {Edge e = e[j];if(D[e.start]! = INF && d[e.end] > D[e.start] + e.time) {D[e.end] = D[e.start] + e.time;//if (D[e.end] < 0) return 1; The wrong optimization. Update =1; }        }if(!update) Break; } for(intj =0; J < CTE; J + +) {Edge e = e[j];if(D[e.start]! = INF && d[e.end] > D[e.start] + e.time) {return 1; }    }//if (i = = n+1) return 1;    return 0;}intMain () {intFCin>> F; while(f--) {CTE =0;Cin>> N >> M >> W; for(inti =1; I <= N;        i++) {D[i] = INF; } for(inti =0; i < M; i++) {intS, E, t;Cin>> s >> e >> t;            Edge Te; Te.start = s; Te.end = e;            Te.time = t;            e[cte++] = te; Te.start = e;            Te.end = s;        e[cte++] = te; } for(inti =0; i < W; i++) {intS, E, t;Cin>> s >> e >> t;            Edge Te; Te.start = s; Te.end = e;            Te.time =-T;        e[cte++] = te; }BOOLFL =0; for(inti =1; I <= N; i++) {fl = Bellman (i);if(FL) Break; }//FL = Bellman (1);        if(FL)cout<<"yes\n";Else cout<<"no\n"; }return 0;}/*15 3 3 103 4 104 5 3 21*/

POJ 3259 wormholes

Related Keywords:
Related Article

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.