zoukankan      html  css  js  c++  java
  • TZOJ 1693 Silver Cow Party(最短路+思维)

    描述

    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?

    输入

    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.

    输出

    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

    样例输出

    10

    提示

    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.
    题意
    给你一个有向图,有N头牛在1-N的位置,问牛i到X再回到i的最大值
    题解
    X到i的最短路很好求,从X点做一次dij
    如何求每个i到X的最短路呢,可以反向建图,然后从X点做一次dij,那么从X到i的最短路就是i到X的最短路
    代码
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn=1005;
     5 vector< pair<int,int> >G[2][maxn];
     6 int d[2][maxn];
     7 int n,m,x;
     8 
     9 void dij(int s,int k)
    10 {
    11     for(int i=1;i<=n;i++)d[k][i]=0x3f3f3f3f;
    12     d[k][s]=0;
    13     queue<int>q;
    14     q.push(s);
    15     while(!q.empty())
    16     {
    17         int u=q.front();q.pop();
    18         for(auto x:G[k][u])
    19         {
    20             int v=x.first;
    21             int w=x.second;
    22             if(d[k][v]>d[k][u]+w)
    23             {
    24                 d[k][v]=d[k][u]+w;
    25                 q.push(v);
    26             }
    27         }
    28     }
    29 }
    30 int main()
    31 {
    32     int u,v,w;
    33     scanf("%d%d%d",&n,&m,&x);
    34     for(int i=1;i<=m;i++)
    35     {
    36         scanf("%d%d%d",&u,&v,&w);
    37         G[0][u].push_back({v,w});
    38         G[1][v].push_back({u,w});
    39     }
    40     dij(x,0),dij(x,1);
    41     int maxx=0;
    42     for(int i=1;i<=n;i++)
    43         maxx=max(maxx,d[0][i]+d[1][i]);
    44     printf("%d
    ",maxx);
    45     return 0;
    46 }
  • 相关阅读:
    设计模式学习之路——Strategy 策略模式
    C# 异步编程 结束异步调用
    设计模式学习之路——Chain Of Responsibility 职责链模式
    delphi笔记之XML操作
    AS3判断XML属性是否存在
    Loader ,URLLoader ,URLStream的使用区别(转)
    delphi笔记之nativeXml
    Delphi的颜色转换
    Delphi利用Bass.dll播放音频
    【转】球坐标旋转
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9746392.html
Copyright © 2011-2022 走看看