您的位置:首页 > 其它

HDU 3665 Seaside <迪杰斯特拉算法>

2015-08-23 20:17 405 查看

Seaside

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1363 Accepted Submission(s): 978



[align=left]Problem Description[/align]
XiaoY is living in a big city, there are N towns in it and some towns near the sea. All these towns are numbered from 0 to N-1 and XiaoY lives in the town numbered ’0’. There are some directed roads connecting them. It is guaranteed
that you can reach any town from the town numbered ’0’, but not all towns connect to each other by roads directly, and there is no ring in this city. One day, XiaoY want to go to the seaside, he asks you to help him find out the shortest way.

[align=left]Input[/align]
There are several test cases. In each cases the first line contains an integer N (0<=N<=10), indicating the number of the towns. Then followed N blocks of data, in block-i there are two integers, Mi (0<=Mi<=N-1) and Pi, then Mi lines
followed. Mi means there are Mi roads beginning with the i-th town. Pi indicates whether the i-th town is near to the sea, Pi=0 means No, Pi=1 means Yes. In next Mi lines, each line contains two integers SMi and LMi, which means that
the distance between the i-th town and the SMi town is LMi.

[align=left]Output[/align]
Each case takes one line, print the shortest length that XiaoY reach seaside.

[align=left]Sample Input[/align]

5
1 0
1 1
2 0
2 3
3 1
1 1
4 100
0 1
0 1


[align=left]Sample Output[/align]

2


[align=left]Source[/align]
2010 Asia Regional Harbin

题意:

输入一个数字n代表城市的个数,然后输入n 个数据块,每一个数据块的第一行是两个数,第一个数m代表第i个城市相连的有几条路,第二个数p代表第I个城市旁边有没有海;(如果有,则这个数为1,如果没有,则这个数为0),然后输入m行,每一行两个数,第一个数代表第i个城市相连的城市,第二个数代表它们之间的距离。(记住城市的编号是从0——n-1),让你求的是第0个城市(你所在的城市编号为0)到第海边的最短距离!

思路:

这道题很明显是最短路程的题,所以我们可以从求最短路程的方法(迪杰斯特拉,SPFA,弗洛伊德)中选择其一做一下就行了!

我这道题用的是迪杰斯特拉算法!

注意:一般我们求的最短距离都是两个点之间的,而这道题让求的是一对多的,所以我们可以虚拟出来一个终点,然后让那些满足条件的终点与虚拟的终点之间的距离为0,其他的点与虚拟的终点之间的距离为INF,这样就将这个一对多转化成1对1的问题了,但是当你到调用迪杰斯特拉函数的时候,你需要将这个虚拟的点也加上,否则会出现错误!

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int n;
int map[15][15];
int dis[15];
int vis[15];
void init()
{
int a,b,c,d;
memset(map,INF,sizeof(map));
for(int i=0;i<n;i++)//城市的编号从0开始!
{
scanf("%d%d",&a,&b);//a代表有a条路开始于a城!
if(b==1)//如果b==1,则说明第i个城市挨着大海
{
map
[i]=map[i]
=0;
}
else//否则说明不挨大海!
{
map
[i]=map[i]
=INF;
}
for(int j=0;j<a;j++)
{
scanf("%d%d",&c,&d);//c代表c城与i城相连,并且他们之间的距离为d;
map[i][c]=map[c][i]=d;
}
}
}
void dijkstra(int x)//用迪杰斯特拉来求最短的路
{
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
for(int i=0;i<=n;i++)
{
dis[i]=map[x][i];
}
dis[x]=0;
vis[x]=1;
int min,k;
for(int i=0;i<=n;i++)
{
min=INF;
for(int j=0;j<=n;j++)
{
if(!vis[j]&&dis[j]<min)
{
min=dis[j];
k=j;
}
}
if(min==INF)
break;
vis[k]=1;
for(int j=0;j<=n;j++)
{
if(!vis[j]&&dis[j]>dis[k]+map[k][j])
dis[j]=dis[k]+map[k][j];
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)//n代表有n个城市!
{
init();//初始化!
dijkstra(0);
printf("%d\n",dis
);//这里面用到了一个知识点,就是将旁边有海的点都看成是与终点n的
}//距离为0的点,没有海的设成INF,然后求0到n的距离就行了!
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: