这道题是有向图,这点要搞清楚,不然会WA的,Orz、、
解法有两种思路
1:用反向图,就是用终点当起点,求到各个点的最短路,从而得到从各起点到终点的距离,则选出最小的即可。
2:加一个超级原点(连通各个起点,并且距离为0)
Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4643 Accepted Submission(s): 1466
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.
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
反向图:
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int M = 1005;
#define inf 0x7f7f7f
int dis[M],mp[M][M];
bool visit[M];
int n,m,start,end;
int a,b,c;
int s[M];
void Dijkstra( int st )
{
for( int i=1 ; i<= n ; i++ ){
visit[i] = false;
dis[i] = mp[st][i];
}
dis[st] = 0;
visit[st] = true;
int tmp , k;
for( int j=1 ; j<=n ; j++ ){
tmp = inf;
for( int j=1 ; j<=n ; j++ )
if( !visit[j] && tmp > dis[j] )
tmp = dis[ k = j ];
if( tmp == inf )
break;
visit[k] = true;
for( int j=1 ; j<=n ; j++ )
if( !visit[j] && dis[j] > mp[k][j] + dis[k] )
dis[j] = mp[k][j] + dis[k];
}
}
int main()
{
while( scanf("%d%d%d",&n,&m,&end) != EOF ){
for( int i=1 ; i<=n ; i++ )
for( int j=1 ; j<=n ; j++ ){
if( i==j )
mp[i][j] = 0;
else
mp[i][j] = inf;
}
for( int i=0 ; i<m ; i++ ){
scanf("%d%d%d",&a,&b,&c);
if( mp[b][a] > c )
mp[b][a] = c;
}
int ans;
ans = inf;
scanf("%d",&start);
for( int i=0 ; i<start ; i++ )
scanf("%d",&s[i]);
Dijkstra( end );
for( int i=0 ; i<start ; i++ ){
if( ans > dis[ s[i] ] )
ans = dis[ s[i] ];
}
if( ans == inf )
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}
超级原点:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 0x3f3f3f3f
#define N 1010
int num, road, destination;
int map[N][N], dis[N], oppo[N];
bool visit[N];
void Dijkstra(int start)
{
int temp, k;
memset(visit, 0, sizeof(visit));
for(int i = 1; i <= num; ++i)
dis[i] = map[start][i];
dis[start] = 0;
visit[start] = 1;
for(int i = 1; i <= num; ++i)
{
temp = MAX;
for(int j = 1; j <= num; ++j)
if(!visit[j] && temp > dis[j])
temp = dis[k = j];
if(temp == MAX) break;
visit[k] = 1;
for(int j = 1; j <= num; ++j)
if(!visit[j] && dis[j] > dis[k] + map[k][j])
dis[j] = dis[k] + map[k][j];
}
}
int main()
{
int x, y, cost, exp, minn;
while(scanf("%d%d%d", &num, &road, &destination) != EOF)
{
memset(map, MAX, sizeof(map));
for(int i = 1; i <= road; ++i)
{
scanf("%d%d%d", &x, &y, &cost);
if(cost < map[y][x]) //巧妙之处:反向图
map[y][x] = cost;
}
scanf("%d", &exp);
for(int i = 1; i <= exp; ++i)
scanf("%d", &oppo[i]);
Dijkstra(destination);
minn = MAX;
for(int i = 1; i <= exp; ++i) //起点找到终点最小值
if(dis[oppo[i]] < minn)
minn = dis[oppo[i]];
if(minn == MAX) printf("-1\n");
else printf("%d\n", minn);
}
return 0;
}

本文介绍了解决公交路线规划问题的两种有效算法:一种是利用反向图来寻找从多个起点到单一终点的最短路径;另一种则是通过引入一个超级原点连接所有起点来解决问题。这两种方法均适用于有向图,并详细展示了如何使用Dijkstra算法进行最短路径计算。
643

被折叠的 条评论
为什么被折叠?



