zoukankan      html  css  js  c++  java
  • HDU 2680 Choose the best route(dijkstra+优先队列优化)

    Choose the best route




    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
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #include<vector>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 struct edge
     9 {
    10     int u,v,w;
    11     edge(int u,int v,int w):u(u),v(v),w(w){};
    12 };
    13 
    14 struct heapnode
    15 {
    16     int u,d;
    17     bool operator < (const heapnode& temp)const
    18     {
    19         return d>temp.d;
    20     }
    21 };
    22 
    23 vector<int>G[1005];
    24 vector<edge>edges;
    25 const int inf=0x3f3f3f3f;
    26 int vis[1005];
    27 int d[1005];
    28 int ed;
    29 int m,n;
    30 
    31 void init()
    32 {
    33     for(int i=1;i<=m;i++)
    34         G[i].clear();
    35     edges.clear();
    36 }
    37 
    38 void addedge(int u,int v,int w)
    39 {
    40     edges.push_back(edge(u,v,w));
    41     int m=edges.size();
    42     G[u].push_back(m-1);
    43 }
    44 
    45 void dijkstra(int ed)
    46 {
    47     memset(vis,0,sizeof(vis));
    48     priority_queue<heapnode>q;
    49     q.push((heapnode){ed,0});
    50     for(int i=1;i<=m;i++)
    51         d[i]=inf;
    52     d[ed]=0;
    53     while(!q.empty())
    54     {
    55         int u=q.top().u;
    56         q.pop();
    57         if(vis[u])
    58             continue;
    59         vis[u]=1;
    60         for(int i=0;i<G[u].size();i++)
    61         {
    62             edge& e=edges[G[u][i]];
    63             if(d[e.v]>d[u]+e.w)
    64             {
    65                  d[e.v]=d[u]+e.w;
    66                  q.push((heapnode){e.v,d[e.v]});
    67             }
    68         }
    69     }
    70 }
    71 
    72 int main()
    73 {
    74     int st,a,b,c,num;
    75     while(scanf("%d%d%d",&m,&n,&ed)!=EOF)
    76     {
    77         init();
    78         for(int i=0;i<n;i++)
    79         {
    80             scanf("%d%d%d",&a,&b,&c);
    81             addedge(b,a,c);
    82         }
    83         dijkstra(ed);
    84         scanf("%d",&num);
    85         int ans=inf;
    86         while(num--)
    87         {
    88             scanf("%d",&st);
    89             ans=min(ans,d[st]);
    90         }
    91         if(ans==inf)
    92             printf("-1
    ");
    93         else
    94             printf("%d
    ",ans);
    95     }
    96     return 0;
    97 }
  • 相关阅读:
    Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
    ptconfigdiff的使用
    freebsd上安装sudo
    vm9.02的序列号
    pttablechecksum
    "Makefile", line 3: Need an operator
    nc的使用
    vs2005自带的水晶报表破解方法
    [vs2008环境]绑定水晶报表的两种方式(Pull和Push)
    .NET环境下水晶报表使用总结
  • 原文地址:https://www.cnblogs.com/homura/p/4725590.html
Copyright © 2011-2022 走看看