POJ-3259-Wormholes

Source: Internet
Author: User

POJ-3259-Wormholes

Http://poj.org/problem? Id = 3259

Bellman_ford determines whether a negative ring exists

# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> using namespace STD; # define Max 0x7fffffffstruct edge {int X, Y, V ;} edge [6000]; int m, n, W; int dis [600]; int bellman_ford (int K) {int I, j, flag; for (I = 1; I <= N; I ++) dis [I] = max; DIS [1] = 0; flag = 0; for (I = 1; I <N; I ++) // relaxation {flag = 0; For (j = 0; j <K; j ++) if (DIS [edge [J]. x]! = Max & dis [edge [J]. y]> dis [edge [J]. x] + edge [J]. v) {dis [edge [J]. y] = dis [edge [J]. x] + edge [J]. v; flag = 1;} If (! Flag) // No change after one update. You do not need to update break again next time;} for (I = 0; I <K; I ++) // adding this edge can shorten the path strength, indicating that there is a negative ring if (DIS [edge [I]. y]> dis [edge [I]. x] + edge [I]. v) return 1; return 0;} int main () {int t; int A, B, C, K; scanf ("% d", & T ); while (t --) {scanf ("% d", & N, & M, & W); k = 0; while (M --) {scanf ("% d", & A, & B, & C); edge [K]. X = A; edge [K]. y = B; edge [K]. V = C; k ++; edge [K]. X = B; edge [K]. y = A; edge [K]. V = C; k ++;} while (w --) {scanf ("% d", & A, & B, & C ); edge [K]. X = A; edge [K]. y = B; edge [K]. V =-C; k ++;} If (bellman_ford (k) printf ("Yes \ n"); else printf ("NO \ n ");} return 0 ;}
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.