Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
题意:给定一个有向图,多个起点,一个终点,求起点到终点的最短路。
注意:图是有向图,两个车站之间可能有多条道路。开始时我一直当做无向图做,结果WA了几次之后,看Discuss里说是有向图。郁闷啊。。。
分析:
思路一:因为有多个起点可以选择,但只有一个终点,所以自己可以再加上一个点作为起点,编号为0,这个点和题中给的那些起点之间的距离为0。这样题目就转化为了求单源最短路径问题。
思路二:反向图。把终点作为反向图的起点,题中的起点作为要到达的终点,每条路的起点和终点交换。然后用Dijkstra求单源最短路,记录最小值即可。
思路一代码:
#include<stdio.h>
#include<string.h>
#define INF 1<<26
const int N = 1e3 + 10;
int w[N][N], d[N];
void Dijkstra(int n, int u)
{
int vis[N], i;
memset(vis, 0, sizeof(vis));
for(i = 0; i <= n; i++)
d[i] = INF;
d[u] = 0;
for(i = 0; i <= n; i++)
{
int x = u, temp = INF;
for(int y = 0; y <= n; y++)
if(!vis[y] && d[y] < temp)
temp = d[x = y];
if(temp == INF) break;
vis[x] = 1;
for(int y = 0; y <= n; y++)
if(d[y] > d[x] + w[x][y])
d[y] = d[x]+ w[x][y];
}
}
int main()
{
int n, m, des, i, j;
while(~scanf("%d%d%d",&n, &m, &des))
{
for(i = 0; i <= n; i++)
for(j = 0; j <= n; j++)
{
if(i == j)
w[i][j] = 0;
else
w[i][j] = INF;
}
int a, b, c;
for(i = 0; i < m; i++)
{
scanf("%d%d%d",&a, &b, &c);
if(c < w[a][b])
w[a][b] = c;
//两个车站之间如果有多条路,选择最短的一条
}
int num, p;
scanf("%d",&num);
for(i = 0; i < num; i++)
{
scanf("%d",&p);
w[0][p] = 0;
//自己加一个起点,把图更新
}
Dijkstra(n, 0);
if(d[des] < INF)
printf("%d\n",d[des]);
else
printf("-1\n");
}
return 0;
}
思路二代码:
#include<stdio.h>
#include<string.h>
#define INF 1<<26
const int N = 1e3 + 10;
int w[N][N], d[N];
void Dijkstra(int n, int u)
{
int vis[N], i;
memset(vis, 0, sizeof(vis));
for(i = 1; i <= n; i++)
d[i] = INF;
d[u] = 0;
for(i = 1; i <= n; i++)
{
int x = u, temp = INF;
for(int y = 1; y <= n; y++)
if(!vis[y] && d[y] < temp)
temp = d[x = y];
if(temp == INF) break;
vis[x] = 1;
for(int y = 1; y <= n; y++)
if(d[y] > d[x] + w[x][y])
d[y] = d[x] + w[x][y];
}
}
int main()
{
int n, m, des, i, j;
while(~scanf("%d%d%d",&n, &m, &des))
{
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
{
if(i == j)
w[i][j] = 0;
else
w[i][j] = INF;
}
int a, b, c;
for(i = 0; i < m; i++)
{
scanf("%d%d%d",&a, &b,&c);
if(c < w[b][a])
w[b][a] = c;
//路也反向
}
Dijkstra(n, des); //终点作为起点
int num, Min_path = INF, p;
scanf("%d",&num);
for(i = 0; i < num; i++)
{
scanf("%d",&p);
if(d[p] < Min_path)
Min_path = d[p]; //记录最小值
}
if(Min_path < INF)
printf("%d\n",Min_path);
else
printf("-1\n");
}
return 0;
}