• HDU 2680 (多起点一个终点最短路)


    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到达目的地的最短路,如果不能到达,就输出-1;
    思路:对于多起点一个终点的问题,可以把起点简化成一个起点,即:将Kiki家视为真正的起点,然后把他家到达每一个能到达的起点的距离看成是0,这样就能缩短时间复杂度。对于多起点多终点的情况,可以将多个起点连在同一个假想的起点上,并且把距离置为0,终点也一样。
    注意:本题标明交通路线是directed,即:有向图

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <string>
     6 #include <cmath>
     7 #include <cstdlib>
     8 using namespace std;
     9 
    10 const int INF=0x3f3f3f3f;
    11 const int N=1010;
    12 int mp[N][N];
    13 int dis[N];
    14 int vis[N];
    15 int m;
    16 int n;
    17 
    18 int dijstra()
    19 {
    20     memset(dis,0x3f,sizeof(dis));
    21     memset(vis,0,sizeof(vis));
    22     dis[0]=0;
    23     for(int i=1;i<=n;i++)
    24     {
    25         int k=0;
    26         int mini=INF;
    27         for(int j=1;j<=n;j++)
    28         {
    29             if(!vis[j]&&mini>dis[j])
    30                 mini=dis[k=j];
    31         }
    32         vis[k]=1;
    33         if(k==m) return dis[m];
    34         for(int j=1;j<=n;j++)
    35         {
    36             if(vis[j]||mp[k][j]==INF) continue;
    37             dis[j]=min(dis[j],dis[k]+mp[k][j]);
    38         }
    39     }
    40     return dis[m];
    41 }
    42 
    43 int main()
    44 {
    45     int s;         //已修好的路有几条
    46     while(~scanf("%d%d%d",&n,&s,&m))    //终点是m,最远的点是n
    47     {
    48         memset(mp,INF,sizeof(mp));
    49         while(s--)
    50         {
    51             int a,b,c;
    52             scanf("%d%d%d",&a,&b,&c);
    53             if(mp[a][b]>c)
    54                 mp[a][b]=c;
    55         }
    56         int d;      //起点个数
    57         scanf("%d",&d);
    58         while(d--)
    59         {
    60             int x;      //起点
    61             scanf("%d",&x);
    62             mp[0][x]=0;       //将能做起点的均与‘0’相连,并且都赋值0,不影响后面的操作,保证只有一个起点'0'
    63         }
    64         int k=dijstra();
    65         if(k==INF) printf("-1
    ");
    66         else printf("%d
    ",dijstra());
    67     }
    68     return 0;
    69 }
    
    
     
  • 相关阅读:
    css3之box-shadow
    css3之圆角
    KOA 学习(九)koa-static
    KOA 学习(八) koa-bodyparser
    KOA 学习(七) 路由koa-router
    videojs使用的常见问题
    KOA 学习(六)superAgent
    KOA 学习(四)
    Ng第五课:Octave 教程(Octave Tutorial)
    Ng第四课:多变量线性回归(Linear Regression with Multiple Variables)
  • 原文地址:https://www.cnblogs.com/Amidgece/p/5745786.html
Copyright © 2020-2023  润新知