zoukankan      html  css  js  c++  java
  • HDU 2680 Choose the best route(dijkstra)

    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
     
    把w个火车站当成出发点0,s为最终要到的地方,最短路径,跟2066一个人的旅行一样
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 const int inf=1<<30;
     5 using namespace std;
     6 int dis[1010],vis[1010];
     7 int n,m,s,p,t,q,w,Map[1010][1010];
     8 int e[1010];
     9 void Dijkstra(){//模板 
    10     int minn,mark;
    11     memset(vis,0,sizeof(vis));
    12     vis[0]=1;
    13     for(int i=0;i<=n;i++)
    14         dis[i]=Map[0][i];
    15     for(int i=1;i<=n;i++){
    16         minn=inf,mark=-1;
    17         for(int j=1;j<=n;j++){
    18             if(dis[j]<minn&&!vis[j]){
    19                 minn=dis[j];
    20                 mark=j;
    21             }
    22         }
    23         if(mark==-1)
    24             break;
    25         vis[mark]=1;
    26         for(int j=1;j<=n;j++){
    27             if(dis[j]>dis[mark]+Map[mark][j]&&!vis[j])
    28                 dis[j]=dis[mark]+Map[mark][j]; 
    29         }
    30     }
    31 }
    32 
    33 
    34 int main(){
    35     while(scanf("%d%d%d",&n,&m,&s)!=EOF){
    36         for(int i=0;i<=n;i++){
    37             for(int j=0;j<=n;j++){
    38                 Map[i][j]=inf;
    39             }
    40             Map[i][i]=0;
    41         }
    42         for(int i=0;i<m;i++){
    43             scanf("%d%d%d",&p,&q,&t);
    44             if(t<Map[p][q])
    45                 Map[p][q]=t; 
    46         }
    47         scanf("%d",&w);
    48         for(int i=0;i<w;i++){
    49             scanf("%d",&e[i]);//开始的出发点 
    50             Map[0][e[i]]=0;
    51         }
    52         Dijkstra();
    53         if(dis[s]==inf){ //s为最终要到的地方 
    54             printf("-1
    ");
    55         }
    56         else
    57             printf("%d
    ",dis[s]);
    58     }
    59     return 0;
    60 } 
  • 相关阅读:
    Aboat join
    ajax 弹出层
    如何使用 BindingSource 绑定 ListBox,同时解决绑定 List<T> 后修改数据源不能同时刷新界面显示的问题
    Javascript 弹出层
    vs 2008 90 天 试用 破解
    c#的as类型转换详细理解,欢迎园友拍砖。
    Linux下定时切割Mongodb数据库日志并删除指定天数前的日志记录
    纠结了一天多的问题arithmetic overflow error converting expression to data type datetime
    关于开发Office2003AddIn问题
    为什么Scrum不行。。
  • 原文地址:https://www.cnblogs.com/cake-lover-77/p/10223979.html
Copyright © 2011-2022 走看看