PKU 3259 wormholes (bellman Ford judges the negative weight edge)

Source: Internet
Author: User

Question link:
Pku3259 wormholes

 

I read Dijkstra and floyed in the data structure book (I have never written it myself)

 

However, if there is a negative weight edge, it will never

 

So I found a specific question to see how to write it.

 

It seems that the key steps for bellmanford and Dijkstra to solve the problem are similar.

Are constantly relaxed

 

But the order of bellmanford is different.

Dijkstra is used to relax each vertex.

 

Bellmanford is the relaxation of each edge (actually a vertex of a directed line segment)

 

// PKU 3259 wormholes // http://poj.org/problem? Id = 3259 // number of points nε [1,500], number of edges wε [], the number of wormhole w−0, 1,200 // The value range of each edge is [-runtime, 10000] # include <stdio. h> # define INF 10001 # define M 3000 struct {int front, end, num;} edge [M * 2 + 10]; // bidirectional graph int Dist [m] = {0}, pre [m], I, j; bool bellman_ford (INT N, int S, int e) // point N start point s edge number {// call bellman_ford (n, 1, E) for (I = 1; I <= N; I ++) // initialize {Dist [I] = inf; // pre [I] =-1;} Dist [s] = 0; // The shortest path of the origin is initialized as 0for (I = 1; I <= n-1; I ++) // the start point of the cyclic Division N-1 j = 1; j <= E; j ++) // perform the relaxation operation on each edge {If (Dist [edge [J]. end]> Dist [edge [J]. front] + edge [J]. num) {Dist [edge [J]. end] = DIST [edge [J]. front] + edge [J]. num; // pre [edge [J]. end] = edge [J]. front; // pre record the beginning of each node} for (I = 1; I <= E; I ++) {If (Dist [edge [I]. end]> Dist [edge [I]. front] + edge [I]. num) // if it is true, the negative ring return false;} return true;} int main () {int F, n, m, W, I, e, x, y, z; scanf ("% d", & F); While (f --) {E = 1; scanf ("% d", & N, & M, & W ); // points n edge number M wormhole number W for (I = 1; I <= m; I ++) {scanf ("% d", & X, & Y, & Z); edge [e]. front = x; // because they are all unidirectional edge [e]. end = y; edge [E ++]. num = z; edge [e]. front = y; // you need to reverse the next edge [e]. end = x; edge [E ++]. num = z ;}for (I = 1; I <= W; I ++) {scanf ("% d", & X, & Y, & Z); edge [e]. front = x; // then the previous e value edge [e]. end = y; edge [E ++]. num = z *-1; // negative edge} If (! Bellman_ford (n, 1, E) // if false printf ("Yes \ n"); // there is a negative ring elseprintf ("NO \ n "); // for (I = 1; I <= N; I ++) // printf ("Dist [% d] = % d", I, DIST [I]); // printf ("\ n"); // for (I = 1; I <= E-1; I ++) // printf ("% d: dist [% d] = % d> Dist [% d] = % d + cost = % d? \ N ", I, edge [I]. end, DIST [edge [I]. end], edge [I]. front, DIST [edge [I]. front], edge [I]. num); // printf ("\ n");} return 0 ;}

 

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.