zoukankan      html  css  js  c++  java
  • hdoj 2680 choose the best route

    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
     
    dijkstra代码:
     1 #include<stdio.h>
     2 #define INF 0x3f3f3f3f
     3 #define N 1010
     4 int vis[N], dis[N], cost[N][N];
     5 int n, m, s, w, p, q, t;
     6 int min(int x, int y)
     7 {
     8     
     9     return x < y ? x : y;
    10 }  
    11 void dijkstra(int beg)
    12 {
    13     int u, v;
    14     for(u = 1; u <= n; u++)
    15     {
    16         vis[u] = 0;
    17         dis[u] = INF;
    18     }
    19     dis[beg] = 0;
    20     while(true)
    21     {
    22         v = -1;
    23         for(u = 1; u <= n; u++)
    24             if(!vis[u] && (v == -1 || dis[u] < dis[v]))
    25                 v = u;
    26         if(v == -1)
    27             break;
    28         vis[v] = 1;
    29         for(u = 1; u <= n; u++)
    30             dis[u] = min(dis[u], dis[v] + cost[v][u]);
    31     }
    32 }
    33 int main()
    34 {
    35     int i , j;
    36     while(~scanf("%d%d%d", &n, &m, &s))
    37     {
    38         for(i = 1; i <= n; i++)
    39             for(j = i; j <= n; j++)
    40                 cost[i][j] = cost[j][i] = INF;
    41         while(m--)
    42         {
    43             scanf("%d%d%d", &p, &q, &t);
    44             if(cost[q][p] > t)
    45                 cost[q][p] = t;
    46         }
    47         scanf("%d", &w);
    48         int sum = INF, b;
    49         dijkstra(s);
    50         for(i = 1; i <= w; i++)
    51         {
    52             scanf("%d", &b);
    53             if(sum > dis[b])
    54                 sum = dis[b];
    55         }
    56         
    57         if(sum == INF)
    58             printf("-1
    ");
    59         else
    60             printf("%d
    ", sum);
    61     }
    62     return 0;
    63 } 

    spfa代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <queue>
     4 #define N 10000000
     5 #define M 1010
     6 #define INF 0x3f3f3f3f
     7 using namespace std;
     8 int n, m, s, cnt;
     9 int vis[M], head[M], time[M];
    10 queue<int>q;
    11 struct node
    12 {
    13     int from, to, cost, next;
    14 }road[N];
    15 void add(int x, int y, int z)
    16 {
    17     node e = {x, y, z, head[x]};
    18     road[cnt] = e;
    19     head[x] = cnt++;
    20 }
    21 void spfa()
    22 {
    23     while(!q.empty())
    24     {
    25         int u = q.front();
    26         q.pop();
    27         vis[u] = 0;
    28         for(int i = head[u]; i != -1; i = road[i].next)
    29         {
    30             int v = road[i].to;
    31             if(time[v] > time[u] + road[i].cost)
    32             {
    33                 time[v] = time[u] + road[i].cost;
    34                 if(!vis[v])
    35                 {
    36                     vis[v] = 1;
    37                     q.push(v);
    38                 }
    39             }
    40         }
    41     }
    42 }
    43 int main()
    44 {
    45     while(~scanf("%d%d%d", &n, &m, &s))
    46     {
    47         
    48         memset(head, -1, sizeof(head));
    49         memset(vis, 0, sizeof(vis));
    50         memset(time, INF, sizeof(time));
    51         while(m--)
    52         {
    53             int p, q, t;
    54             scanf("%d%d%d", &p, &q, &t);
    55             add(p, q, t);
    56             //add(q, p, t);
    57         }
    58         int w;
    59         scanf("%d", &w);
    60         while(w--)
    61         {
    62             int posi;
    63             scanf("%d", &posi);
    64             q.push(posi);
    65             time[posi] = 0;
    66             vis[posi] = 1;
    67         } 
    68         spfa();
    69         if(time[s] == INF)
    70             printf("-1
    ");
    71         else
    72             printf("%d
    ", time[s]);
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    同一WpfApplication下简单的页面转换
    触发器
    程序包
    函数
    存储过程
    游标
    我的第一个SolidWorks图
    张量系列(tensor02)
    张量系列-Tensor(01)
    Python与矩阵论——特征值与特征向量
  • 原文地址:https://www.cnblogs.com/digulove/p/4738452.html
Copyright © 2011-2022 走看看