加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

TOJ 1693:Silver Cow Party(最短路+思维)

发布时间:2020-12-14 04:20:36 所属栏目:大数据 来源:网络整理
导读:描述 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

描述

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:?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.

输出

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("%dn",maxx);
45     return 0;
46 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读