Implementation of the shortest Bellman-Ford HDU2544

Source: Internet
Author: User

Implementation of the shortest Bellman-Ford HDU2544
Problem Description

In each year's competition, all the finalists will get a very beautiful t-shirt. However, every time our staff moved hundreds of pieces of clothing from the store back to the stadium, they were very tired! So now they want to find the shortest route from the store to the stadium. Can you help them?

Input

The input includes multiple groups of data. The first row of each group of data is two integers, N and M (N <= 100, M <= 10000). N indicates several intersections on the streets of Chengdu, the intersection marked as 1 is the location of the store, the intersection marked as N is the location of the stadium, and M represents several roads in Chengdu. N = M = 0 indicates that the input is complete. In the next M row, each row contains three integers, A, B, and C (1 <= A, B <= N, 1 <= C <= 1000 ), it means there is A road between Intersection A and intersection B. Our staff need to walk this road in C minutes.
Enter a route to ensure there is at least one store.

Output

Output a line for each group of inputs, indicating the shortest time for a staff member to walk from the store to the stadium

Sample Input

2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0

Sample Output

3
2

Solutions

In an undirected graph, Bellman-Ford can expand edges to 2 * e and convert them to a "Directed Graph" to solve the problem.

Code
# Include
  
   
Const int max_v = 110; const int max_e = 10010; const int INF = 1000000000; int e, v; struct edge {int from, to, cost ;}; int d [max_v]; edge eg [max_e * 2]; int main () {while (scanf ("% d", & v, & e) & v) {// the edge is extended to the range of 1-2E for (int I = 0; I <max_v; I ++) d [I] = INF; for (int I = 1; I <= e; I ++) {scanf ("% d", & eg [I]. from, & eg [I]. to, & eg [I]. cost); eg [I + e]. from = eg [I]. to; eg [I + e]. to = eg [I]. from; eg [I + e]. cost = eg [ I]. cost;} d [1] = 0; while (true) {bool flag = false; for (int I = 1; I <= 2 * e; I ++) {if (d [eg [I]. from]! = INF & d [eg [I]. from] + eg [I]. cost <d [eg [I]. to]) {d [eg [I]. to] = d [eg [I]. from] + eg [I]. cost; flag = true ;}} if (! Flag) break;} printf ("% d \ n", d [v]);} 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.