zoukankan      html  css  js  c++  java
  • Choose the best route(最短路)dijk

    http://acm.hdu.edu.cn/showproblem.php?pid=2680

    Choose the best route

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


    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
     

      题解:最短路 注意有重边,这里介绍一种用链表存 的时候可以不用考虑重边,方法就是将所有的边都标记为未访问,然后将其他边的值标记成INF,将开始那条边的值标记成0 ,然后加入n边,每次更新的时候就不用考虑重边了。

    这个题因为数据量特别的大,dijk的算法本身是O (n^2)的,查询的时候调用n次dijk所以总共是O(n^3),所以最后会超时,可以逆向思维,因为终点是已知的所以从终点开始dijk一次后找到所有的已知起点中距离最小的点就可以了。

    注意这个题中是有向边。

    下面是代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 #define N 2000
     7 #define M 400000
     8 #define INF 0x1fffffff
     9 struct Edge{
    10     int to ;
    11     int v;
    12     int next;
    13 }edge[M];
    14 int head[N];
    15 int Ecnt;
    16 void init()
    17 {
    18     Ecnt = 0;
    19     memset(head,-1,sizeof(head));
    20 }
    21 void add(int from , int to ,int v)
    22 {
    23     edge[Ecnt].to = to;
    24     edge[Ecnt].v = v;
    25     edge[Ecnt].next = head[from];
    26     head[from] = Ecnt++;
    27 }
    28 int dist[N];
    29 bool p[N];
    30 void dijk(int s, int n)
    31 {
    32     int i , j , k ;
    33     for(i = 0 ;i <= n ;i++)
    34     {
    35         p[i] = false;
    36         dist[i] = INF;
    37     }
    38     //p[s] = true;
    39     dist[s] = 0;
    40 
    41     for( i = 0; i < n ; i++)
    42     {
    43         int Min = INF ;
    44         int  k = 0 ;
    45         for( j = 1 ; j <= n ; j++)
    46         {
    47             if(!p[j]&&dist[j]<Min)
    48             {
    49                 Min = dist[j];
    50                 k = j;
    51             }
    52         }
    53         if(Min == INF ) return ;
    54         p[k] = true;
    55         for(j = head[k]; j != -1 ; j = edge[j].next)
    56         {
    57             Edge e = edge[j];
    58             if(!p[e.to]&&dist[e.to]>dist[k]+e.v)
    59                 dist[e.to] = dist[k]+e.v;
    60         }
    61     }
    62 }
    63 int main()
    64 {
    65     int n , m , s ;
    66     while(~scanf("%d%d%d",&n,&m,&s))
    67     {
    68         init();
    69         for(int i = 0 ;i < m ; i++)
    70         {
    71             int p , q , t ;
    72             scanf("%d%d%d",&p,&q,&t);
    73             add(q,p,t);//逆向扫描,所以逆向加边
    74         }
    75         dijk(s,n);
    76         int ss;
    77         scanf("%d",&ss);
    78         int ans = INF;
    79         for(int i = 0 ;i < ss; i++)
    80         {
    81             int w ;
    82             scanf("%d",&w);
    83             ans = min(ans,dist[w]);
    84         }
    85         if(ans==INF) printf("-1
    ");
    86         else printf("%d
    ",ans);
    87     }
    88     return 0;
    89 }
  • 相关阅读:
    代码: 仿淘宝商品详情页左上,图片鼠标浮上去,图片部分区域放大 (页面布局:图片列表)
    !代码:页面布局
    代码: 返回页面顶部 jquery
    !!学习笔记:CSS3动画
    代码: 两列图片瀑布流(一次后台取数据,图片懒加载。下拉后分批显示图片。图片高度未知,当图片onload后才显示容器)
    !学习笔记:前端测试 、前端调试、console 等
    学习笔记:SASS
    !代码:伪类
    代码:css小图标
    学习笔记:ECharts
  • 原文地址:https://www.cnblogs.com/shanyr/p/4681147.html
Copyright © 2011-2022 走看看