题目描述

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. python 读取数据库中文内容显示一堆问号

    需要在连接数据库时 设置编码格式 def select_db(self,db_name): self.conn = MySQLdb.connect( host = self.ip, port = se ...

  2. 深入理解net core中的依赖注入、Singleton、Scoped、Transient(四)【转】

    原文链接:https://www.cnblogs.com/gdsblog/p/8465401.html 相关文章: 深入理解net core中的依赖注入.Singleton.Scoped.Transi ...

  3. install.cloudinit.qga.bat

    @echo off title Auto Install color 1F ::CloudBase-Init echo. msiexec /i \\192.168.122.47\cloudbase\C ...

  4. neutron floating ip 限速

    查看浮动ip的id [root@10e131e69e14 oz]# openstack floating ip show 36.111.0.197 +---------------------+--- ...

  5. CSU-1170 A Simple Problem

    题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=1170 题目 Description ​ 在一个由N个整数组成的数列中,最 ...

  6. Java 打印* 三角形

    package anli1; public class sanjiaoxing { public static void main(String[] agrs){ System.out.println ...

  7. Codeforces 433 Div.2(A、B、C、D)

    A. Fraction 暴力遍历1-1000,取组成的真分数比值最大且分子分母gcd为1时更新答案 代码: #include <stdio.h> #include <algorith ...

  8. 洛谷 P4882 lty loves 96! 解题报告

    P4882 lty loves 96! 题目背景 众所周知,\(lty\)非常喜欢\(96\)这两个数字(想歪的现在马上面壁去),更甚于复读(人本复)! 题目描述 由于爱屋及乌,因此,\(lty\)对 ...

  9. 【BZOJ 4151 The Cave】

    Time Limit: 5 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 293  Solved: 144[Submit][Status][Di ...

  10. 编写Shell脚本的最佳实践,规范一

    随着写的SHELL程序越来越多,发现自己每次写都有不同的习惯或者定义了不同的东西,变量名定义得不一样,整个程序缩进不统一,没有注释等问题,等我回过头看这些程序的时候发现很麻烦.所以写了个shell代码 ...