Choose the best route(选择最佳路线)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680
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.
(有一天,琪琪想拜访她的一位朋友。由于她容易晕车,她想尽快到达朋友家。现在给您一张城市交通路线图,以及琪琪家附近的车站,以便她乘坐。您可能认为Kiki可以在任何车站换公交车。请找出Kiki需要花费的最少时间。为方便起见,如果城市有n个公交车站,则车站将被表示为整数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.
(有几个测试用例。
每种情况均以三个整数n,m和s开头(n <1000,m <20000,1 = <s <= n)n代表该城市的公交车站数量,m代表之间的有向车道数量公交车站。(也许在两个公交车站之间有几种方式。)s代表在琪琪朋友家附近的公交车站。
然后跟随m行,每行包含三个整数p,q和t(0 <t <= 1000)。意思是从车站p到车站q有一种办法,它将花费t分钟。
然后,一行具有整数w(0 <w <n)的行表示Kiki可以在开始时使用的站点数。然后,w个整数代表这些站。)
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”.
(对于每个数据集,输出包含一行:Kiki需要花费的最少时间,如果找不到这样的路线,则只需输出“ -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
思路:将其点定为0(琪琪家),将琪琪家到可达车站的时间设为0。最后套用Dijkstra算法模板
正确代码:
#include<iostream>
#include<stdio.h>
using namespace std;
int n,m,sl;//n:数量,m:有向车道数量公交车站,sl:朋友家附近公交站
int k;//琪琪开始可用站点数
int MAXN=2<<20;
int s[1005][1005];
int vis[1005];
int ans[1005];
void print(){
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
cout<<s[i][j]<<" ";
}
cout<<endl;
}
}
void init(){
for(int i=0;i<=n;i++){
vis[i]=0;
for(int j=0;j<=n;j++){
s[i][j]=i==j?0:MAXN;
s[j][i]=s[i][j];
}
}
}
void dijkstra(int x){
vis[x]=1;
for(int i=0;i<=n;i++){
ans[i]=s[x][i];
}
while(true){
int f=-1;
int minn=MAXN;
for(int i=0;i<=n;i++){
if(vis[i]==0&&ans[i]<minn){
f=i;
minn=ans[i];
}
}
if(f==-1){
break;
}
vis[f]=1;
for(int i=0;i<=n;i++){
if(vis[i]==0&&ans[i]>ans[f]+s[f][i]){
ans[i]=ans[f]+s[f][i];
}
}
}
}
int main(){
ios::sync_with_stdio(false);
while(cin>>n>>m>>sl){
init();
for(int i=0;i<m;i++){
int p,q,t;
cin>>p>>q>>t;
if(s[p][q]>t){
s[p][q]=t;
}
}
cin>>k;
for(int i=0;i<k;i++){
int a;
cin>>a;
if(s[0][a]>0){
s[0][a]=0;
}
}
//print();
dijkstra(0);
//print();
if(ans[sl]==MAXN){
cout<<-1<<endl;
}
else{
cout<<ans[sl]<<endl;
}
}
return 0;
}

本文介绍了一个基于Dijkstra算法实现寻找从起点到终点最短时间路径的问题。通过设定公交车站及行车时间,解决了一位角色尽快到达目的地的问题。
582

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



