HDUOJ 2544 最短路

 

最短路

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11062    Accepted Submission(s): 4709

Problem Description
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

 

 

Input
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
 

 

Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
 

 

Sample Input
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
 

 

Sample Output
3 2
 

 

Source
 


第一次完成最短路径的题目,初探Dijkstra算法的精深。

思路就是通过Dijkstra算法找到1到其余各个路口的最短距离,自然就找到1到n的最短距离。输出即可。

原本我使用的不是正确的Dijkstra算法,但依然通过了很多组数据,后来才发现不对,从而对Dijkstra算法有了更深入的理解。

WA code:

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    const int N=101,max=10000000;
    int n,m,i,j,cost[N],path[N][N],a,b,c;//cost记录各个路口到1的距离,path记录各个路口间的距离
    while(scanf("%d%d",&n,&m) && n>=1)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=i;j++)
            {
                path[i][j]=path[j][i]=max;
            }
            path[i][i]=0;
        }
        for(i=2;i<=n;i++)
        {
            cost[i]=max;
        }
        cost[1]=0;
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            path[a][b]=path[b][a]=c;
            if(a==1) cost[b]=c;
            else if(b==1) cost[a]=c;
        }
	//错误之处:
	//==================================================================
        for(i=2;i<n;i++)
        {
            if(cost[i]<max)
			{
				for(j=2;j<=n;j++)
				{
					if(cost[j]>path[i][1]+path[i][j])
					{
						path[j][1]=path[1][j]=cost[j]=path[i][1]+path[i][j];
					}
				}
			}
        }
	//===================================================================
        printf("%d\n",cost[n]);
    }
    return 0;
}


以上程序不能通过的情况:

5
1 2 4
2 4 1
1 3 1
3 2 2
1 4 5


AC code:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int main()
 5 {
 6     const int N=101,max=0xfffff;//max不宜设得过大,避免后面的运算溢出
 7     //cost记录各个路口到1的距离,path记录各个路口间的距离
 8     int n,m,i,j,k,cost[N],path[N][N],a,b,c,min;
 9     bool S[N];
10     while(scanf("%d%d",&n,&m) && n>=1)
11     {
12         //初始化
13         for(i=1;i<=n;i++)
14         {
15             for(j=1;j<=i;j++)
16             {
17                 path[i][j]=path[j][i]=max;
18             }
19             path[i][i]=0;
20         }
21         for(i=2;i<=n;i++)
22         {
23             S[i]=0;
24             cost[i]=max;
25         }
26         S[1]=1;
27         cost[1]=0;
28         //输入
29         for(i=0;i<m;i++)
30         {
31             scanf("%d%d%d",&a,&b,&c);
32             path[a][b]=path[b][a]=c;
33             if(a==1) cost[b]=c;
34             else if(b==1) cost[a]=c;
35         }
36         //从路口2开始到路口n-1即可
37         for(i=2;i<n;i++)
38         {
39             min=max;
40             //寻找最短路径
41             for(j=2;j<=n;j++)
42             {
43                 if(!S[j] && cost[j]<min)
44                 {
45                     min=cost[j];
46                     k=j;
47                 }
48             }
49             S[k]=1;//把当前离路口1最近的路口纳入集合
50             //更新路径
51             for(j=2;j<=n;j++)
52             {
53                 if(!S[j] && cost[j]>path[k][j]+min)
54                 {
55                     cost[j]=path[k][j]+min;
56                 }
57             }
58         }
59         printf("%d\n",cost[n]);
60     }
61     return 0;
62 }

 

posted on 2012-04-12 18:45  铁树银花  阅读(275)  评论(0编辑  收藏  举报

导航