[USACO07FEB]银牛派对Silver Cow Party

Posted kanchuang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[USACO07FEB]银牛派对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?

寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100)。

每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度。

输入输出格式

输入格式:

第一行三个整数N,M, X;

第二行到第M+1行:每行有三个整数Ai,Bi, Ti ,表示有一条从Ai农场到Bi农场的道路,长度为Ti。

输出格式:

一个整数,表示最长的最短路得长度。

输入输出样例

输入样例#1:
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
输出样例#1: 
10

说明

技术图片

分析:

本题实质上就是单源最短路和单终点最短路,而单终点最短路只要在最后反向加边再跑一遍SPFA即可。

 

CODE:

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <deque>
 5 #include <iostream>
 6 #include <algorithm>
 7 using namespace std;
 8 const int M=1000005;
 9 const int INF=2147000000;
10 int nxt[M],head[M],to[M],adj[M],dist[M],inq[M];
11 int nxtl[M],headl[M],tol[M],adjl[M],distl[M];
12 int n,m,tot,totl,x;
13 int fr()
14     char c=getchar();int ans=0;
15     while (c<0||c>9) c=getchar();
16     while (c>=0&&c<=9) ans=(ans<<1)+(ans<<3)+(c^48),c=getchar();
17     return ans;
18 
19 void add(int u,int v,int w)
20     nxt[++tot]=head[u];head[u]=tot;to[tot]=v;adj[tot]=w;
21     return;
22 
23 void addl(int u,int v,int w)
24     nxtl[++totl]=headl[u];headl[u]=totl;tol[totl]=v;adjl[totl]=w;
25     return;
26 
27 void spfa()
28     for (int i=1;i<=n;i++) dist[i]=INF;
29     deque <int> Q;
30     Q.push_back(x);dist[x]=0;inq[x]=1;
31     while (!Q.empty())
32         int top=Q.front();Q.pop_front();inq[top]=0;
33         for (int i=head[top];i;i=nxt[i])
34             if (dist[top]+adj[i]<dist[to[i]])
35                 dist[to[i]]=dist[top]+adj[i];
36                 if (!inq[to[i]])
37                     inq[to[i]]=1;
38                     if (!Q.empty()&&dist[Q.front()]>dist[to[i]]) Q.push_front(to[i]);
39                     else Q.push_back(to[i]);
40                 
41             
42     
43     return;
44 
45 void spfal()
46     for (int i=1;i<=n;i++) distl[i]=INF;
47     deque <int> Q;
48     Q.push_back(x);distl[x]=0;inq[x]=1;
49     while (!Q.empty())
50         int top=Q.front();Q.pop_front();inq[top]=0;
51         for (int i=headl[top];i;i=nxtl[i])
52             if (distl[top]+adjl[i]<distl[tol[i]])
53                 distl[tol[i]]=distl[top]+adjl[i];
54                 if (!inq[tol[i]])
55                     inq[tol[i]]=1;
56                     if (!Q.empty()&&distl[Q.front()]>distl[tol[i]]) Q.push_front(tol[i]);
57                     else Q.push_back(tol[i]);
58                 
59             
60     
61     return;
62 
63 int main()
64     n=fr(),m=fr(),x=fr();
65     for (int i=1;i<=m;i++)
66         int u=fr(),v=fr(),w=fr();
67         add(u,v,w);
68         addl(v,u,w);
69     
70     spfa();spfal();
71     int ans=0;
72     for (int i=1;i<=n;i++)
73         ans=max(ans,dist[i]+distl[i]);
74     printf("%d",ans);
75     return 0;
76 

 

 

 

以上是关于[USACO07FEB]银牛派对Silver Cow Party的主要内容,如果未能解决你的问题,请参考以下文章

Luogu P1821 [USACO07FEB]银牛派对Silver Cow Party

[USACO07FEB]银牛派对Silver Cow Party

P1821 [USACO07FEB]银牛派对Silver Cow Party

「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party

[USACO07FEB]银牛派对Silver Cow Party

luogu P1821 [USACO07FEB]银牛派对Silver Cow Party