【解题报告】【HDOJ2544】【Bellman-Ford最短路】最短路

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define INF 0x3f3f3f3f
 4 struct node{
 5     int u;
 6     int v;
 7     int weight;
 8 };
 9 struct node map[10001];
10 int record[101];
11 int n,m;
12 
13 void Bellman_init()
14 {
15     int i;
16     for(i=0;i<2*m;)
17     {
18         scanf("%d%d%d",&map[i].u,&map[i].v,&map[i].weight);
19         map[i+1].u=map[i].v;
20         map[i+1].v=map[i].u;
21         map[i+1].weight=map[i].weight;
22         i+=2;
23     }
24     memset(record,INF,sizeof(record));
25     record[1]=0;
26 }
27 void Bellman()
28 {
29     int i,j;
30     // for(i=0;i<=n;i++) printf("<%d>\n",record[i]);
31     //for(i=0;i<2*m;i++) printf("<%d,%d,%d>\n",map[i].u,map[i].v,map[i].weight);
32     for(i=0;i<n;i++)
33     {
34         for(j=0;j<2*m;j++)
35         {
36             
37             if(record[map[j].v]>record[map[j].u]+map[j].weight)
38             {
39                 
40                 record[map[j].v]=record[map[j].u]+map[j].weight;
41             }
42         }
43     } 
44 }
45 void Bellman_print()
46 {
47     printf("%d\n",record[n]);
48 }
49 int main()
50 {
51     // freopen("1.txt","r",stdin);
52     while(scanf("%d%d",&n,&m)!=EOF&&(n!=0||m!=0))
53     {
54         Bellman_init();
55         Bellman();
56         Bellman_print();
57     }
58     return 0;
59 }

 

posted on 2012-07-21 18:27  coding封神  阅读(109)  评论(0编辑  收藏  举报

导航