• hdu 2680 Choose the best route Dijkstra 虚拟点


    Choose the best route

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


    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
     
    由于他给了多个起点,却只有一个重点,所以可以设置虚拟的0为起点,0与给出的起点之间的距离为0;
    代码:
    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define inf 100000000
     4 int map[1010][1010];               //注意这道题是单向边
     5 int d[1010];
     6 int s[1010];
     7 int n,m,ss;                //ss是终点
     8 int dij(int v)
     9 {
    10     int i,j,min,pos;
    11     for(i=0;i<=n;i++)
    12     {
    13         s[i]=0;
    14         d[i]=map[v][i];
    15     }
    16        s[v]=1;
    17     d[v]=0;
    18     for(i=1;i<=n;i++)         //处理剩余的n 个点 
    19     {
    20         min=inf;
    21         for(j=1;j<=n;j++)
    22         {
    23            if(!s[j]&&min>d[j])
    24                {
    25                 pos=j;
    26                 min=d[j];
    27               }
    28          }
    29         if(pos==ss||min==inf) break;       
    30         s[pos]=1;
    31         for(j=1;j<=n;j++)
    32         {
    33              if(!s[j]&&d[j]>(d[pos]+map[pos][j]))
    34             d[j]=d[pos]+map[pos][j];
    35         }
    36     }
    37     return d[ss];
    38 }
    39 int main()
    40 {
    41 int i,j,k;
    42 int p,q,t;
    43 int w,qi;          //qi代表起点
    44 while(scanf("%d%d%d",&n,&m,&ss)!=EOF)
    45 {
    46     for(i=0;i<=n;i++)
    47     for(j=0;j<=n;j++)
    48     map[i][j]=inf; 
    49       for(i=1;i<=m;i++)
    50     {
    51         scanf("%d%d%d",&p,&q,&t);
    52         if(map[p][q]>t)             
    53         map[p][q]=t;             /
    54     }
    55     scanf("%d",&w);
    56     for(i=1;i<=w;i++)
    57     {
    58         scanf("%d",&qi);
    59         map[0][qi]=0;            // 设置虚拟的点0 ,O到起点的距离为0,这样就直接一遍dijkstra就行了。
    60     }
    61     k=dij(0);
    62     if(k==inf) printf("-1\n");
    63     else
    64     printf("%d\n",k);
    65  }
    66  return 0;
    67 }
  • 相关阅读:
    [AGC020E] Encoding Subsets
    [Topcoder16346]TwoPerLine
    CF913E Logical Expression
    英语面试
    CRM
    WEB使用第三方支付服务大致流程
    OO语言 prototype 初级用法
    flash设置字体
    air 提示问题
    c#里的BYTE
  • 原文地址:https://www.cnblogs.com/shenshuyang/p/2622705.html
Copyright © 2020-2023  润新知