Shortest Path, bellman-ford Algorithm

Source: Internet
Author: User

The Problem description is given with n vertices and a directed graph of m edges (some edge weights may be negative, but there is no negative ring ). Calculate the shortest path (from vertex 1 to vertex n) from vertex 1 to other ). The first line of the input format is two integers n, m. In the next m row, each row has three integers u, v, l, indicating that u to v has an edge with a length of l. There are n-1 lines in the output format. line I indicates the shortest path from Point 1 to point I + 1. Sample input 3 31 2-12 3-13 1 2 sample output-1-2 data size and conventions for 10% of data, n = 2, m = 2. For 30% of the data, n <= 5, m <= 10. For 100% of data, 1 <= n <= 20000,1 <= m <= 200000,-10000 <= l <= 10000, to ensure that all other vertices can be reached from any vertex.

1 package job525; 2 3 import java. util. values; 4 5 class Eage {6 int from; 7 int to; 8 int cost; 9 10 public int getFrom () {11 return from; 12} 13 public void setFrom (int from) {14 this. from = from; 15} 16 public int getTo () {17 return to; 18} 19 public void setTo (int to) {20 this. to = to; 21} 22 public int getCost () {23 return cost; 24} 25 public void setCost (int cost) {26 this. cost = cost; 27} 28 publ Ic Eage (int from, int to, int cost) 29 {30 this. from = from; 31 this. to = to; 32 this. cost = cost; 33} 34} 35 36 public class the shortest path of bellman_ford {37 38 static int m, n, inf = 100000; // n: n vertices; m: m side 39 static int [] d = new int [20000]; 40 static Eage [] eg = new Eage [200000]; 41 42 public static void main (String [] args) {43 expect SC = new expect (System. in); 44 n = SC. nextInt (); m = SC. nextInt (); 45 int I = 0; 46 while (I <m) 47 {48 Int u, v, l; 49 u = SC. nextInt (); 50 v = SC. nextInt (); 51 l = SC. nextInt (); 52 eg [I ++] = new Eage (u, v, l); 53} 54 // test Storage Limit 55/* 56 for (I = 0; I <m; I ++) 57 {58 System. out. println ("(" + eg [I]. getFrom () + "," + eg [I]. getTo () + "):" + eg [I]. getCost (); 59} 60 */61 // test ended -------------------------------------------------------------------------------------- 62 Bellman_ford (); 63 for (I = 2; I <= n; I ++) 64 {65 System. out. println (d [I]); 66} 67} 68 public static void bellman_ford () 69 {70 init (); 71 while (true) 72 {73 boolean flag = false; 74 for (int I = 0; I <m; I ++) 75 {76 Eage e = eg [I]; 77 if (d [e. getFrom ()]! = Inf & d [e. getTo ()]> d [e. getFrom ()] + e. getCost () 78 {79 d [e. getTo ()] = d [e. getFrom ()] + e. getCost (); 80 // System. out. println ("(" + eg [I]. getFrom () + "," + eg [I]. getTo () + "):" + eg [I]. getCost () + d [e. getTo ()]); // test 81 flag = true; 82} 83} 84 if (! Flag) 85 break; 86} 87} 88 public static void init () 89 {90 for (int I = 1; I <= n; I ++) 91 d [I] = inf; 92 d [1] = 0; 93} 94}View Code

 

  

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.