zoukankan      html  css  js  c++  java
  • <poj

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

      题目大意:牛们要去聚会,输入N = 顶点数(牛场);M = 边(路)的数目; X = 终点 (聚会点)。问题:求来回时间的最大值。

    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.
    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
     
     解题思路:因为本题是单向的,所以在做的时候可以做一个正向图和一个反向图,分别求解来回的时间,然后找和的最大值。

    这里是我的代码:
     1 #include <cstring>
     2 #include <iostream>
     3 #define INF 9999999
     4 using namespace std;
     5 
     6 bool used[100005];
     7 int V, E;
     8 const int maxn = 1005;
     9 
    10 void dijkstra (int s, int cost[][1005], int d[]) {
    11     fill (d, d + V + 1, INF);
    12     fill (used,used + V + 1,false);
    13     d[s] = 0;
    14     while (true) {
    15         int v = -1;
    16         for (int u = 1; u <= V; u++) {
    17             if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
    18         }
    19         if (v == -1) break;
    20         used[v] = true;
    21         for (int u = 1; u <= V; ++u) {
    22             if (d[u] >  d[v] + cost[v][u]) {
    23                 d[u] = d[v] + cost[v][u];
    24             }
    25         }
    26     }
    27 }
    28 
    29 int cost[maxn][maxn];
    30 int rcost[maxn][maxn];
    31 
    32 int main () {
    33     int d[maxn];
    34     int rd[maxn];
    35     int x, y, w;
    36     int sum[maxn];
    37     int S;
    38     cin >> V >> E >> S;
    39     
    40     for (int i = 1;i <= V; ++i)
    41         for (int j = 1; j <= V; ++j)
    42             rcost[i][j] = cost[i][j] = INF;
    43     
    44     for (int i = 0; i < E; ++i) {
    45         cin >> x >> y >> w;
    46         rcost[y][x] = cost[x][y] = w;
    47     }
    48     
    49     dijkstra(S, cost, d);//分别计算最短路径
    50     dijkstra(S, rcost, rd);
    51     
    52     for (int i = 1; i <= V; ++i)//求和
    53         sum[i] = d[i] + rd[i];
    54     
    55     int maxnum = sum[1];
    56     for (int i = 1; i <= V; ++i)///找最大值
    57         if (sum[i] > maxnum)
    58             maxnum = sum[i];
    59     
    60     cout << maxnum << endl;
    61     
    62     return 0;
    63 }
    View Code

        欢迎码友评论,一起成长。

  • 相关阅读:
    基于模糊Choquet积分的目标检测算法
    Android开发5:布局管理器2(表格布局TableLayout)
    JAVA WEB开发环境搭建教程
    linux下自助获取帮助
    dsp下基于双循环缓冲队列的视频采集和显示记录
    找工作笔试面试那些事儿(11)---数据库知识总结(2)范式
    【Todo】Zookeeper系列文章
    VC2010对Excel的操作
    hdu2647解题报告
    premake 在64位Ubuntu系统下编译32位GCC程序
  • 原文地址:https://www.cnblogs.com/Ddlm2wxm/p/5714724.html
Copyright © 2011-2022 走看看