zoukankan      html  css  js  c++  java
  • POJ3268 Silver Cow Party(dijkstra+矩阵转置)

    Silver Cow Party
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 15156   Accepted: 6843

    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 ≤ XN). 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: N, M, and X
    Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, 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

    Hint

    Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

    Source

    题意:两次最短路,第一次求x到其余各点的最短路,第二次求各点到x的最短路。前者易于解决,直接应用dijkstra或其他最短路算法即可,后者要先将邻接矩阵转置再执行最短路算法。
    为什么进行矩阵转置?比如u(u != x)到x的最短路为<u,v1>,<v1,v2>,<v2,v3>,...,<vi, x>,这条路径在转置邻接矩阵后变成<x,vi>,...,<v3,v2>,<v2, v1>,<v1,u>.于是乎,在转置邻接矩阵后,执行最短路算法求出x到u的最短 路<x,vi>,...,<v3,v2>,<v2, v1>,<v1,u>即可得到转置前u到x的最短路。
    收获:dijkstra紫书上的模板,矩阵转置操作。
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <algorithm>
     5 #include <ctime>
     6 #include <cmath>
     7 #include <string>
     8 #include <cstring>
     9 #include <stack>
    10 #include <queue>
    11 #include <list>
    12 #include <vector>
    13 #include <map>
    14 #include <set>
    15 using namespace std;
    16 
    17 const int INF=0x3f3f3f3f;
    18 const double eps=1e-10;
    19 const double PI=acos(-1.0);
    20 const int maxn=1000+10;
    21 
    22 int w[maxn][maxn];
    23 int v[maxn], d[maxn], c[maxn];
    24 int n, m, x;
    25 
    26 void dijkstra(int k)
    27 {
    28     memset(v, 0, sizeof(v));
    29     for(int i = 1; i <= n; i++) d[i] = (i==k ? 0 : INF);
    30     for(int i = 1; i <= n; i++){
    31         int x1, m = INF;
    32         for(int y = 1; y <= n; y++) if(!v[y] && d[y]<=m) m = d[x1=y];
    33         v[x1] = 1;
    34         for(int y = 1; y <= n; y++) d[y] = min(d[y], d[x1]+w[x1][y]);
    35     }
    36 }//dijkstra紫书模板
    37 void  tran() {
    38     for(int i = 1; i <= n; i++) {
    39         for(int j = 1; j <= i; j++)
    40             swap(w[i][j], w[j][i]);
    41     }
    42 }//矩阵转置
    43 int main()
    44 {
    45     while(~scanf("%d%d%d", &n, &m, &x)) {
    46         int a, b, t;
    47         memset(w, INF, sizeof(w));
    48         for(int i = 0; i < m; i++) {
    49             scanf("%d%d%d", &a, &b, &t);
    50             w[a][b] = min(w[a][b], t);
    51         }
    52         memset(c, 0, sizeof(c));
    53         dijkstra(x);
    54         for(int i = 1; i <= n; i++) c[i] = d[i];
    55         tran();
    56         dijkstra(x);
    57         int ans = -1;
    58         for(int i = 1; i <= n; i++) {
    59             c[i] += d[i];
    60             ans = max(ans,c[i]);
    61         }
    62        printf("%d
    ", ans);
    63     }
    64 }
  • 相关阅读:
    layui 自定义表单验证的几个实例
    elementUI vue upload完整示例
    视频图片--多线程下载工具
    获取邮箱的DNS和MX 工具类
    10大经典排序算法动图演示
    nginx 转将http跳转到https
    Java 导出 Excel 列号数字与字母互相转换工具
    linux 下查看文件个数及大小
    文字不换行的解决办法
    溢出 省略号 ...
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/4701288.html
Copyright © 2011-2022 走看看