PKU 3259 wormholes-judge the negative weight loop Bellman-Ford

Source: Internet
Author: User

Question:

There are n fields (n <= 500) on the farm, and m paths (M <= 2500) can take t units of time to arrive at J from I. There are also W wormhole holes (W <= 200), which can arrive at the other J from the ground I and timeRegressingT! Note that the path is bidirectional and the wormhole is unidirectional. Now Farmer John wants to know if he can start from a certain place and return to this place so that he can return earlier than the departure time (he can meet himself ^ ).

Analysis:

The disguise of the question is removed. It can be seen that the question requires a directed graph with negative weights.Whether a loop with a negative weight exists.

This problem isBellman-FordThe problem to be solved when finding the shortest path with a negative weight directed graph.

 

  1. /*
  2. Pku3259 wormholes
  3. */
  4. # Include <stdio. h>
  5. # Include <memory. h>
  6. # Define CLR (a) memset (A, 0, sizeof ())
  7. # Define Inf (1 <28)
  8. # Define n 505
  9. # Define M 6005
  10. Int bellman_ford (int e [] [3], int M, int N, int d [])
  11. {
  12. Int K, V, flag, I, J;
  13. For (k = 0; k <n; k ++) d [k] = inf;
  14. // D [0] = 0;
  15. For (k = 0; k <n-1; k ++)
  16. {
  17. Flag = 0;
  18. For (V = 0; v <m; V ++ ){
  19. I = E [v] [0]; j = E [v] [1];
  20. If (d [J]> d [I] + E [v] [2]) {
  21. D [J] = d [I] + E [v] [2];
  22. Flag = 1;
  23. }
  24. }
  25. If (! Flag) return 1;
  26. }
  27. For (v = 0; v <m; v ++)
  28. If (d [e [v] [1]> d [e [v] [0] + e [v] [2])
  29. Return 0;
  30. Return 1;
  31. }
  32. Int n, m, w;
  33. Int e [M] [3], ne;
  34. Int d [N];
  35. Void addEdge (int I, int j, int k ){
  36. E [ne] [0] = I;
  37. E [ne] [1] = j;
  38. E [ne] [2] = k;
  39. Ne ++;
  40. }
  41. Int main ()
  42. {
  43. Int I, j, k, t, T;
  44. Scanf ("% d", & T );
  45. While (T --){
  46. // Input
  47. Ne = 0;
  48. Scanf ("% d", & n, & m, & w );
  49. For (k = 0; k <m + w; k ++ ){
  50. Scanf ("% d", & I, & j, & t );
  51. If (k <m ){
  52. AddEdge (I, j, t );
  53. AddEdge (j, I, t );
  54. }
  55. Else {
  56. AddEdge (I, j,-t );
  57. }
  58. }
  59. // Output
  60. If (Bellman_Ford (e, ne, n, d) puts ("NO ");
  61. Else puts ("YES ");
  62. }
  63. Return 0;
  64. }

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.