Hduoj2680【dijksta】

/*Choose the best route 
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia 
Font Size: ← →
Problem Description
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as 
possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose 
Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,
the stations will been expressed as an integer 1,2,3…n. 
Input
There are several test cases. 
Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands 
for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station 
that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will 
costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.

Output
The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”. 
Sample Input
5 8 5
1 2 2
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3
4 3 4
1 2 3
1 3 4
2 3 2
1
1

Sample Output
1
-1

Author
dandelion 
Source
2009浙江大学计算机研考复试(机试部分)——全真模拟 */
#include<stdio.h>
#include<string.h>
#define MAX 10000000
int n, m, end, dis[1010][1010], start[1010], max;
int min(int x, int y)
{
	return x<y?x:y;
}
void dij(int x)
{
	int i, j, mi , vis[1010] = {0}, d[1010];
	vis[x] = 1;
	for(i = 1; i <= n; ++i)
	d[i] = dis[i][x];//起点到各点的距离 
	for(int k = 1; k < n; ++k)
	{
		mi = MAX;
		j = 0; //当无法到达终点时用来判断
		for(i = 1; i <= n; ++i)
		{
			if(d[i] < mi && !vis[i])//寻找未访问过且到起点距离最小的点 
			{
				mi = d[i];
				j = i;
			}
		}
		if(j == 0)
		break;
		vis[j] = 1;//标记已经访问 
		if(start[j])//若走到终点 
		{
			if(d[j] < max)//记录最短距离 
			max = d[j];
		}
		for(i = 1; i <= n; ++i)//更新未访问过的点到起点的距离 
		if(!vis[i])
		d[i] = min(d[i], d[j] + dis[i][j]);
	}
}
int main()
{
	int i, j, k;
	while(scanf("%d%d%d", &n, &m, &end) != EOF)
	{
		for(i = 1; i <= n; ++i)//初始化 
		for(j = 1; j <= n; ++j)
		{
			if(i!=j)
			dis[i][j] = MAX;
			else
			dis[i][j] = 0;
		}
		for(i = 0; i < m; ++i)//记录初始路径 
		{
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			if(c < dis[a][b])//a到b 
			dis[a][b] = c;
		}
		int num1, num2;
		scanf("%d", &num1);//输入终点个数 
		memset(start, 0, sizeof(start));
		for(i = 0; i < num1; ++i)
		{
			scanf("%d", &num2);
			start[num2] = 1;//标记终点 
		}
		max = MAX;
		dij(end);
		if(max != MAX)
		printf("%d\n", max);
		else
		printf("-1\n");
	}
	return 0;
}


题意:给出n个汽车站和m个车站之间的距离和终点,车站编号从1到n,现在给出w个起点,求起点到终点的最小距离。

思路:这题可以看出是最短路径的算法,关键是有几个技巧,即将终点看成是起点,起点看成是多个终点,可以优化复杂度,其次就是要处理无法到达终点的情况。

你可能感兴趣的:(Hduoj2680【dijksta】)