HDU 2680-Choose the best route

来源:互联网 发布:mpi编程是什么意思 编辑:程序博客网 时间:2024/06/06 05:07

Choose the best route

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15631    Accepted Submission(s): 5092


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



题意:Kiki想去看望朋友,他要坐公交车去,让找出最快到达朋友家的时间,这里我们只计算公交车的时间,Kiki家附近有公交车,他朋友家附近也有公交车,输入的第一行有三个数 n, m , s ,n 代表公交车站的数量,将其编号从 1 到 n ,编号从 1 到 n ,m 代表的是公交车站的定向交通,s 代表的是Kiki朋友家附近的公交车站的编号,然后紧跟有 m 行,每行有三个数 p,q ,t ,代表 p 号站点到 q 号站点 需要 t 分钟,然后再有一个 w ,w  代表Kiki家附近的公交站数量,然后紧跟 w 个数,给出 Kiki 家附近的公交站点的编号。如果有能找到去朋友家的公交路线就输出用时最少的时间,否则输出 -1.



分析:
本题需注意的是题目中 m stands for the number of directed ways between bus stations .(m代表的是公交车站的定向交通),本题是有向图,存图的时候要注意。
另外说说本题的解法,本题有两种解法,第一种是虚构点 0 是Kiki的家,将 Kiki 家附近的站点到 Kiki 家的距离都设为 0 ,直接求 0 到 s 的最短路。第二种方法已知 Kiki朋友家附近的站点是给定的,将 Kiki 朋友家附近的那个唯一的站点做源点,找距离 Kiki 家附近站点最近的路也行。
夜已深,我还没洗澡,就不给出第二种求解代码,看看第一种解的代码吧!






#include <iostream>#include<string>#include<string.h>#include<stdio.h>using namespace std;#define INF 0x3f3f3f3fint map1[1005][1005];int n,m,s,w;void Dijkstra(){    int vis[1005],bj,dis[1005],min1;    memset(vis,0,sizeof(vis));    for(int i=0;i<=n;i++)        dis[i]=map1[0][i];    vis[0]=1;    for(int i=0;i<=n;i++)    {        min1=INF;        for(int j=0;j<=n;j++)        {            if(!vis[j]&&dis[j]<min1)            {                min1=dis[j];                bj=j;            }        }        if(min1==INF)            break;        vis[bj]=1;        for(int k=0;k<=n;k++)        {            if(!vis[k]&&dis[k]>dis[bj]+map1[bj][k])                dis[k]=dis[bj]+map1[bj][k];        }    }    if(dis[s]<INF)        printf("%d\n",dis[s]);    else        printf("-1\n");}int main(){    int p,q,t,u;    while(~scanf("%d %d %d",&n,&m,&s))    {        for(int i=0;i<=n;i++)        {            for(int j=0;j<=n;j++)            {                if(i==j)                    map1[i][j]=0;                else                    map1[i][j]=INF;            }        }        for(int i=1;i<=m;i++)        {            scanf("%d %d %d",&p,&q,&t);            map1[p][q]=min(map1[p][q],t);        }        scanf("%d",&w);        for(int i=1;i<=w;i++)        {            scanf("%d",&u);            map1[0][u]=0;        }        Dijkstra();    }    return 0;}


 
原创粉丝点击