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

    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有w个地方可以坐共交车,问到朋友家的最短路,若不能到输出-1

    题解

    这题就是kiki从w个起点坐到终点的最短路,算是单源最短路,可以每个起点跑一遍SPFA(这样太费时间了)

    假如我们把kiki家看成出发点0,可以到w个距离为0的公交站,这样我们就把多个起点归1了,跑一遍SPFA就行了

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 #define INF 0x3f3f3f3f
     5 typedef pair<int,int> pii;
     6 vector<pii> G[1005];
     7 int Vis[1005],Dist[1005];
     8 void spfa()
     9 {
    10     memset(Vis,0,sizeof(Vis));
    11     memset(Dist,INF,sizeof(Dist));
    12     queue<int> Q;
    13     Q.push(0);
    14     Dist[0]=0;//从家里0出发
    15     while(!Q.empty())
    16     {
    17         int u=Q.front();Q.pop();
    18         Vis[u]=0;
    19         for(int i=0;i<G[u].size();i++)
    20         {
    21             int v=G[u][i].first,w=G[u][i].second;
    22             if(Dist[v]>Dist[u]+w)
    23             {
    24                 Dist[v]=Dist[u]+w;
    25                 if(Vis[v]==0)
    26                 {
    27                     Vis[v]=1;
    28                     Q.push(v);
    29                 }
    30             }
    31         }
    32     }
    33 }
    34 int main()
    35 {
    36     int n,m,s,k,u,v,w;
    37     while(scanf("%d%d%d",&n,&m,&s)!=EOF)
    38     {
    39         for(int i=0;i<m;i++)
    40         {
    41             scanf("%d%d%d",&u,&v,&w);
    42             G[u].push_back(pii(v,w));
    43         }
    44         scanf("%d",&k);
    45         for(int i=0;i<k;i++)
    46         {
    47             scanf("%d",&v);
    48             G[0].push_back(pii(v,0));//从家到公交站距离为0
    49         }
    50         spfa();
    51         printf("%d
    ",Dist[s]==INF?-1:Dist[s]);
    52         for(int i=0;i<n;i++)
    53             G[i].clear();
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    jsonp 监控简陋代码
    Oracle多线程并行使用、关联与指定索引执行
    Oracle计算时间差表达式
    行查列显
    JS时间
    通过string型类名实例化一个类
    sotower1.5-LS_工作流容易出错的地方
    oracle11g ORA-12505
    华项笔记本显示颜色变黄,调整好了重启还是偏黄
    ORA-00031: session marked for kill 处理Oracle中杀不掉的锁
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/8502839.html
Copyright © 2011-2022 走看看