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

    Choose the best route

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 12   Accepted Submission(s) : 6
    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) q n in this city directed .(Maybe are several ways between two .) s bus that near Kiki’s friend’s home. follow m lines ,each contains three , (0<t<="1000)." from p to station there is way and it will costs t minutes . a line with an integer w(0<w<n), means number of stations Kiki can take at the beginning. Then follows w integers stands for these stations. div <>
     

    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<stdio.h>
     2 #include<string.h>
     3 #define MIN(x,y)(x<y?x:y)
     4 const int MAXN=1010;
     5 const int INF=0x3f3f3f3f;
     6 int n,vis[MAXN],d[MAXN];
     7 int map[MAXN][MAXN];
     8 void initial(){
     9     memset(map,0x3f,sizeof(map));
    10     memset(vis,0,sizeof(vis));
    11     memset(d,INF,sizeof(d));
    12 }
    13 void dijskra(int s){
    14     d[s]=0;
    15     while(true){
    16         int k=-1;
    17         for(int i=1;i<=n;i++)
    18             if(!vis[i]&&(k==-1||d[i]<d[k]))k=i;
    19         if(k==-1)break;
    20         vis[k]=1;
    21         for(int i=1;i<=n;i++)
    22             d[i]=MIN(d[i],d[k]+map[k][i]);
    23     }
    24 }
    25 int main(){
    26     int m,s,p,q,t,a,e,ans;
    27     while(~scanf("%d%d%d",&n,&m,&s)){
    28             initial();
    29         while(m--){
    30             scanf("%d%d%d",&p,&q,&t);
    31             if(map[q][p]>t)map[q][p]=t;
    32         }
    33         scanf("%d",&a);
    34         ans=INF;
    35         dijskra(s);
    36         while(a--){
    37             scanf("%d",&e);
    38             ans=MIN(ans,d[e]);
    39         }
    40         if(ans==INF)puts("-1");
    41         else printf("%d
    ",ans);
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    __attribute__ 总结
    linux文件夹打包命令
    学习ARM的一些基本知识,个人整理
    备忘录之 —— .bashrc(IC工具篇)
    GitHub的基本使用
    02: SocketServer服务
    01: socket模块
    08: python基础练习题
    07: 高阶函数&异常处理
    06: 面向对象
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4737553.html
Copyright © 2011-2022 走看看