zoukankan      html  css  js  c++  java
  • POJ 3268 (dijkstra算法)

     

    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

    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.
     
     
    思路:这里要求每个点的每只奶牛去终点来回的最短路径,然后找出最长的路径长度,考虑到每个点到终点的最短路径不好求,可以反向建边直接dijkstra解决,因此要用两次dijkstra算法......
     
     
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<cstdio>
     5 #include<algorithm>
     6 #include<map>
     7 #include<set>
     8 #include<vector>
     9 #include<queue>
    10 using namespace std;
    11 #define ll long long 
    12 const int mod=1e9+7;
    13 const int inf=1e9+7;
    14 const int maxn=1e3+10;
    15 const int maxm=1e5+10;
    16 int u[maxm];//计算正反两次迪杰斯特拉 
    17 int v[maxm];//需要把边存起来 
    18 int w[maxm];
    19 int n,m;
    20 int endd;
    21 int dis[maxn];
    22 int ans[maxn];
    23 int e[maxn][maxn];
    24 int book[maxn];
    25 void init()
    26 {
    27     for(int i=1;i<=n;i++)
    28         for(int j=1;j<=n;j++)
    29             i==j?e[i][j]=0:e[i][j]=inf;
    30  } 
    31 void dijkstra()
    32 {
    33     for(int i=1;i<=n;i++)
    34         dis[i]=e[endd][i];
    35     memset(book,0,sizeof(book));
    36     book[endd]=1;
    37     for(int k=1;k<=n-1;k++)
    38     {
    39         int minn=inf,temp;
    40         for(int i=1;i<=n;i++)
    41         {
    42             if(book[i]==0&&dis[i]<minn)
    43                 minn=dis[temp=i];
    44         }
    45         book[temp]=1;
    46         for(int j=1;j<=n;j++)
    47         {
    48             if(book[j]==0&&e[temp][j]<inf)
    49             {
    50                 if(dis[j]>dis[temp]+e[temp][j])
    51                     dis[j]=dis[temp]+e[temp][j];
    52             }
    53         }
    54     }
    55 }
    56 int main()
    57 {
    58     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    59     cin>>n>>m>>endd;
    60     init();
    61     for(int i=1;i<=m;i++)
    62     {
    63         cin>>u[i]>>v[i]>>w[i];
    64         e[u[i]][v[i]]=w[i];//正向边 
    65     }
    66     dijkstra();
    67     for(int i=1;i<=n;i++)
    68         ans[i]=dis[i];//保存前一次的结果 
    69     init();
    70     for(int i=1;i<=m;i++)
    71         e[v[i]][u[i]]=w[i];//反向建边,所有奶牛往终点走反向之后等价于终点的单源最短路 
    72     dijkstra();
    73     int maxx=-1;
    74     for(int i=1;i<=n;i++)
    75     {
    76         if(ans[i]+dis[i]>maxx)
    77             maxx=ans[i]+dis[i];//找最大值 
    78     }
    79     cout<<maxx<<endl;    
    80     return 0;
    81 }
    大佬见笑,,
  • 相关阅读:
    索引结构
    云时代基础设置自动化管理利器: Chef
    软件Scrum
    选择置换+败者树搞定外部排序
    selenium webdriver (python)2
    [置顶] javascript-基于对象or面向对象?
    4.7 阻止对某几列插入
    mysql数据损坏修复方法
    阿里云挂载数据盘
    Delphi 使用双缓冲解决图片切换时的闪烁问题 good
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/10969807.html
Copyright © 2011-2022 走看看