zoukankan      html  css  js  c++  java
  • Choose the best route

    杭电2680

    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
    View Code
     1 //杭电2680
     2 #include<stdio.h>
     3 #include<stdlib.h>
     4 #include<string.h>
     5 #define M 999999999
     6 #define N 1010
     7 int n,m,s,w,x,y,z;
     8 int a[N],b[N],map[N][N];
     9 int f[N];
    10 int MIN(int a,int b)
    11 {
    12     return a<b?a:b;
    13 }
    14 int main()
    15 {
    16     int i,j,k,min,t;
    17     int p,flag;
    18     while(scanf("%d%d%d",&n,&m,&s)!=-1)
    19     {
    20         memset(f,0,sizeof(f));
    21         for(i=1;i<=n;i++)
    22         {
    23             for(j=1;j<=n;j++)
    24             {
    25                 map[i][j]=M;
    26             }
    27         }
    28         for(i=1;i<=m;i++)
    29         {
    30             scanf("%d%d%d",&x,&y,&z);
    31             if(map[y][x]>z)
    32                 map[y][x]=z;//本题为有向输入
    33         }
    34         for(i=1;i<=n;i++)
    35         {
    36             a[i]=M;
    37             b[i]=0;
    38         }
    39         scanf("%d",&w);
    40         
    41         for(j=1;j<=w;j++)
    42         {
    43             scanf("%d",&p);
    44             f[p]=1;//标记起始的车站
    45         }
    46         
    47         int num=1;
    48         t=k=s;
    49         a[s]=0;
    50         b[s]=1;
    51         flag=0;
    52         while(num<n)
    53         {
    54             min=M;
    55             for(i=1;i<=n;i++)
    56             {
    57                 if(b[i]==0)
    58                 {
    59                     a[i]=MIN(a[i],a[t]+map[t][i]);
    60                     if(a[i]<min)
    61                     {
    62                         min=a[i];
    63                         k=i;
    64                     }
    65                 }
    66             }
    67             b[k]=1;
    68             t=k;
    69             if(f[t]==1)
    70             {
    71                 flag=1;
    72                 break;
    73             }
    74             num++;
    75         }
    76         
    77         if(flag==1)
    78             printf("%d\n",a[t]);
    79         else
    80             printf("-1\n");
    81         
    82     }
    83     return 0;
    84 }
    View Code
     1 //杭电2680
     2 #include<stdio.h>
     3 #include<stdlib.h>
     4 #include<string.h>
     5 #define M 999999999
     6 #define N 1010
     7 int n,m,s,w,x,y,z;
     8 int a[N],b[N],map[N][N];
     9 int f[N];
    10 void dijksta()
    11 {
    12     int i,j;
    13     for(i=0;i<=n;i++)
    14     {
    15         a[i]=map[0][i];
    16         b[i]=i==0?1:0;
    17     }
    18     for(i=0;i<=n-1;i++)
    19     {
    20         int t=0;
    21         a[t]=M;
    22         for(j=0;j<=n;j++)
    23           if(b[j]==0&&a[j]<a[t])
    24               t=j;
    25           b[t]=1;
    26           for(j=0;j<=n;j++)
    27               if(b[j]==0&&map[t][j]<M&&a[j]>map[t][j]+a[t])
    28                   a[j]=map[t][j]+a[t];
    29     }
    30 }
    31 int main()
    32 {
    33     int i,j,k,min,t;
    34     int p,flag;
    35     while(scanf("%d%d%d",&n,&m,&s)!=-1)
    36     {
    37         memset(f,0,sizeof(f));
    38         for(i=0;i<=n;i++)
    39         {
    40             for(j=0;j<=n;j++)
    41             {
    42                 map[i][j]=M;
    43             }
    44         }
    45         for(i=1;i<=m;i++)
    46         {
    47             scanf("%d%d%d",&x,&y,&z);
    48             if(map[x][y]>z)
    49                 map[x][y]=z;
    50         }
    51         scanf("%d",&w);
    52         for(j=1;j<=w;j++)
    53         {
    54             scanf("%d",&p);
    55             map[0][p]=0;
    56         }
    57         
    58       dijksta();
    59         if(a[s]!=M)
    60             printf("%d\n",a[s]);
    61         else
    62             printf("-1\n");
    63         
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    elasticsearch painless脚本评分
    elasticsearch relevance score相关性评分的计算
    java 多线程间通信(二)
    java 多线程间通信(一)
    java CountDownLatch、CyclicBarrier和 Semaphore用法
    java 深入剖析ThreadLocal
    java中String、StringBuffer、StringBuilder的区别
    Leetcode: LRU Cache
    Leetcode: Anagrams(颠倒字母而成的字)
    Leetcode: First Missing Positive
  • 原文地址:https://www.cnblogs.com/zlyblog/p/2610139.html
Copyright © 2011-2022 走看看