• POJ 3268 Silver Cow Party (双向dijkstra)


    题目链接:http://poj.org/problem?id=3268

    Silver Cow Party
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 19211   Accepted: 8765

    Description

    One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

    Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

    Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

    Input

    Line 1: Three space-separated integers, respectively: NM, and X 
    Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

    Output

    Line 1: One integer: the maximum of time any one cow must walk.

    Sample Input

    4 8 2
    1 2 4
    1 3 2
    1 4 7
    2 1 1
    2 3 5
    3 1 2
    3 4 4
    4 2 3

    Sample Output

    10
    题目大意:(单向路)有n头牛,他们商量好去x家举行party,需要你帮他们计算出其他人去到x家party结束后回到自己家所需要的最短路程,然后输出最长路程。(每头牛去的路程可能和回家的路程不同)
    解题思路:用两遍dijkstra 分别计算出其他牛到x家的最短路径,然后再计算出他们从x家返回到自己家的最短路程,最后找出最长路径
      1 #include <stdio.h>
      2 #include <string.h>
      3 #define inf 9999999999
      4 int p1[1010][1010];
      5 int p2[1010][1010];
      6 int vis1[1010];
      7 int vis2[1010];
      8 int dis1[1010];
      9 int dis2[1010];
     10 int n,m,x;
     11 void dijkstra_go()  //计算从自己家到x家所需要的最短路径(注意用的p2数组)反向思维
     12 {
     13     int i,j,pos = 0,minn;
     14     for (i = 1; i <= n; i ++)
     15     {
     16         vis1[i] = 0;
     17         dis1[i] = p2[x][i];
     18     }
     19     vis1[x] = 1;
     20     dis1[x] = 0;
     21 
     22     for (i = 1; i <= n; i ++)
     23     {
     24         minn = inf;
     25         for (j = 1; j <= n; j ++)
     26         {
     27             if (!vis1[j] && dis1[j] < minn)
     28             {
     29                 minn = dis1[j];
     30                 pos = j;
     31             }
     32         }
     33         vis1[pos] = 1;
     34         for (j = 1; j <= n; j ++)
     35         {
     36             if (!vis1[j] && dis1[j] > dis1[pos]+p2[pos][j])
     37                 dis1[j] = dis1[pos]+p2[pos][j];
     38         }
     39     }
     40 }
     41 void dijkstra_back()  //计算从x家回到自己家所需要的最短路径
     42 {
     43     int i,j,pos = 0,minn;
     44     for (i = 1; i <= n; i ++)
     45     {
     46         vis2[i] = 0;
     47         dis2[i] = p1[x][i];
     48     }
     49     vis2[x] = 1;
     50     dis2[x] = 0;
     51     for (i = 1; i <= n; i ++)
     52     {
     53         minn = inf;
     54         for (j = 1; j <= n; j ++)
     55         {
     56             if (!vis2[j] && dis2[j] < minn)
     57             {
     58                 minn = dis2[j];
     59                 pos = j;
     60             }
     61         }
     62         vis2[pos] = 1;
     63         for (j = 1; j <= n; j ++)
     64         {
     65             if (!vis2[j] && dis2[j] > dis2[pos]+p1[pos][j])
     66                 dis2[j] = dis2[pos]+p1[pos][j];
     67         }
     68     }
     69 }
     70 int main ()
     71 {
     72     int a,b,t;
     73     int i,j;
     74     int sum[1010];
     75     while (~scanf("%d%d%d",&n,&m,&x))
     76     {
     77         for (i = 1; i <= n; i ++)
     78         {
     79             for (j = 1; j <= n; j ++)
     80             {
     81                 p1[i][j] = inf;
     82                 p2[i][j] = inf;
     83             }
     84         }
     85 
     86         for (i = 0; i < m; i ++)
     87         {
     88             scanf("%d%d%d",&a,&b,&t);
     89             p1[a][b] = t;
     90             p2[b][a] = t;
     91         }
     92         dijkstra_go();
     93         dijkstra_back();
     94         int maxx = 0;
     95         for (i = 1; i <= n; i ++)
     96         {
     97             if (i == x)
     98                 continue;
     99             sum[i] = dis1[i]+dis2[i];
    100             if (maxx < sum[i])
    101                 maxx = sum[i];
    102         }
    103         printf("%d
    ",maxx);
    104     }
    105     return 0;
    106 }
    View Code


  • 相关阅读:
    SpringMVC学习笔记----
    各种语言学习
    七步精通Python机器学习 转
    开源GIS有用
    sql 语句
    调试windows服务最简单的方法之一
    sqlserver2008 win7下安装 windows installer错误
    高德地图AmapSDKDemo运行
    google mapsDemo示例运行
    android开发环境搭建遇到的问题
  • 原文地址:https://www.cnblogs.com/yoke/p/5866151.html
Copyright © 2020-2023  润新知