题目描述

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

#include<bits/stdc++.h>
#define inf 2000000000
using namespace std;
struct edge{
int v,next,w;
}edge1[],edge2[];
int n,m,s;
int head1[],head2[];
int in[];
int d1[];
int d2[];
int num;
void add_edge(int x,int y,int w)
{
edge1[++num].v=y;edge1[num].w=w;edge1[num].next=head1[x];head1[x]=num;
edge2[++num].v=x;edge2[num].w=w;edge2[num].next=head2[y];head2[y]=num;
}
void spfa(){
queue<int> q;
q.push(s);
in[s]=;
d1[s]=;
while(!q.empty()){
int t=q.front();
q.pop();
in[t]=;
for(int i=head1[t];i;i=edge1[i].next){
if(d1[t]+edge1[i].w<d1[edge1[i].v]){
d1[edge1[i].v]=d1[t]+edge1[i].w;
if(!in[edge1[i].v]){
in[edge1[i].v]=;
q.push(edge1[i].v);
}
}
}
}
}
void spfa2(){
queue<int> q;
q.push(s);
in[s]=;
d2[s]=;
while(!q.empty()){
int t=q.front();
q.pop();
in[t]=;
for(int i=head2[t];i!=;i=edge2[i].next){
if(d2[t]+edge2[i].w<d2[edge2[i].v]){
d2[edge2[i].v]=d2[t]+edge2[i].w;
if(!in[edge2[i].v]){
in[edge2[i].v]=;
q.push(edge2[i].v);
}
}
}
}
}
int main(){
cin>>n>>m>>s;
memset(d1,0x3f,sizeof d1);
memset(d2,0x3f,sizeof d2);
for(int i=;i<=m;i++){
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
add_edge(a,b,w);
}
spfa();
memset(in,,sizeof(in));
spfa2();
int ans=;
for(int i=;i<=n;i++){
ans=max(ans,d1[i]+d2[i]);
}
cout<<ans;
return ;
}

luogu P1821 [USACO07FEB]银牛派对Silver Cow Party的更多相关文章

  1. 【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1821 反向多存一个图,暴力跑两遍 #include <cstdio> #include < ...

  2. 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  3. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party 题解

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  4. P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  5. 洛谷P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  6. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party

    银牛派对 正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可. 反向建图以及双向建图的做法是学习图论的必备思想. #include <iostream> #include & ...

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

    更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...

  8. [USACO07FEB]银牛派对Silver Cow Party

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

  9. [USACO07FEB]银牛派对Silver Cow Party---最短路模板题

    银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...

随机推荐

  1. jmeter运行脚本后,请求偶发性的传参错误

    问题现象:jmeter写好脚本后,请求偶发性的传参错误 排查过程:1.结合报错返回值,看是不是线程并发引起: 2.排除线程并发引起后,看看是不是取值策略:如果是参数化,看看是不是每次迭代,每次都取唯一 ...

  2. dpkg.cfg

  3. 一些优秀的SLAM博主

    http://blog.csdn.net/u010566411 http://blog.csdn.net/qq_18661939/article/details/51782376 http://www ...

  4. Ubuntu 安装jdk与tomcat

    1.官网下载jdk,地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ,选择 ...

  5. RabbitMQ-Java客户端API指南-上

    RabbitMQ-Java客户端API指南-上 客户端API严格按照AMQP 0-9-1协议规范进行建模,并提供了易于使用的附加抽象. RabbitMQ Java客户端使用com.rabbitmq.c ...

  6. NBUT校赛 J Alex’s Foolish Function(分块+延迟标记)

    Problem J: Alex’s Foolish Function Time Limit: 8 Sec  Memory Limit: 128 MB Submit: 18  Solved: 2 Des ...

  7. [luogu3676] 小清新数据结构题 [树链剖分+线段树]

    题面 传送门 思路 本来以为这道题可以LCT维护子树信息直接做的,后来发现这样会因为splay形态改变影响子树权值平方和,是splay本身的局限性导致的 所以只能另辟蹊径 首先,我们考虑询问点都在1的 ...

  8. [codeforces] 527A Playing with Paper

    原题 简单的gcd #include<cstdio> #include<algorithm> typedef long long ll; using namespace std ...

  9. BZOJ4826 [Hnoi2017]影魔 【线段树 + 单调栈】

    题目链接 BZOJ4826 题解 蒟蒻智力水平捉急orz 我们会发现相邻的\(i\)和\(j\)贡献一定是\(p1\),可以很快算出来[然而我一开始忘了考虑调了半天] 我们现在只考虑不相邻的 我们只需 ...

  10. BZOJ4031 [HEOI2015]小Z的房间 【矩阵树定理 + 高斯消元】

    题目链接 BZOJ4031 题解 第一眼:这不裸的矩阵树定理么 第二眼:这个模\(10^9\)是什么鬼嘛QAQ 想尝试递归求行列式,发现这是\(O(n!)\)的.. 想上高斯消元,却又处理不了逆元这个 ...