洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party
P1821 [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。
输出格式:
一个整数,表示最长的最短路得长度。
输入输出样例
说明

spfa跑最长路,再求起点到其他点的最短路的时候可以不跑n边spfa,而转为建反向边,然后在跑一遍spfa就好了
这个题跟我们以前做过的一道题很像——洛谷:邮递员送信 https://www.luogu.org/problemnew/show/1629
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100000
#define maxn 99999999
using namespace std;
long long ans;
int n,m,e,x,y,z,s,tot,dis[N],head[N],dis1[N],dis2[N];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
struct NN
{
int x,y,z;
}edde[N];
struct Edge
{
int to,dis,from,next;
}edge[N];
int add(int x,int y,int z)
{
tot++;
edge[tot].to=y;
edge[tot].dis=z;
edge[tot].next=head[x];
head[x]=tot;
}
int spfa(int s)
{
queue<;
;i<=n;i++) dis[i]=maxn,vis[i]=false;
q.push(s);vis[s]=;
while(!q.empty())
{
int x=q.front(); q.pop();
for(int i=head[x];i;i=edge[i].next)
{
int t=edge[i].to;
if(dis[t]>dis[x]+edge[i].dis)
{
dis[t]=dis[x]+edge[i].dis;
if(!vis[t])
{
q.push(t);
vis[t]=true;
}
}
}
vis[x]=false;
}
}
int main()
{
n=read(),m=read();e=read();
;i<=m;i++)
{
x=read(),y=read(),z=read();
add(x,y,z);
edde[i].x=x;edde[i].y=y,edde[i].z=z;
}
spfa(e);
;i<=n;i++)
dis1[i]=dis[i];
s=tot,tot=;
memset(dis,,sizeof(dis));
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
;i<=s;i++)
add(edde[i].y,edde[i].x,edde[i].z);
spfa(e);
;i<=n;i++)
dis2[i]=dis[i];
;i<=n;i++)
if(dis1[i]+dis2[i]>ans)
ans=dis1[i]+dis2[i];
printf("%lld",ans);
;
}
洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party的更多相关文章
- 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party 题解
P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- 洛谷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 ...
- 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party
银牛派对 正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可. 反向建图以及双向建图的做法是学习图论的必备思想. #include <iostream> #include & ...
- 洛谷 1821 [USACO07FEB]银牛派对Silver Cow Party
[题解] 其实解法 #include<cstdio> #include<cstring> #include<algorithm> #define LL long l ...
- 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 ...
- luogu 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 ...
- 【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解
题目链接:https://www.luogu.org/problemnew/show/P1821 反向多存一个图,暴力跑两遍 #include <cstdio> #include < ...
- [USACO07FEB]银牛派对Silver Cow Party
题目简叙: 寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100). 每头牛参加 ...
- 「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party
更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...
随机推荐
- eclipse常用快捷键大全 (转)
Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升. ...
- redis-cluster 集群搭建详细指南及常见问题集合
只当个搬运工吧 搭建篇:https://www.cnblogs.com/mafly/p/redis_cluster.html 测试能用 常见问题: 1 redis操作key时出现以下错误 (erro ...
- 2017 ACM Arabella Collegiate Programming Contest(solved 11/13)
省选考前单挑做点ACM练练细节还是很不错的嘛- 福利:http://codeforces.com/gym/101350 先来放上惨不忍睹的virtual participate成绩(中间跑去食堂吃饭于 ...
- Linux下如何强制中断一个程序的执行?
CTRL + C 中断 CTRL + Z 暂时放到后台 CTRL + D 保存退出
- Vue的keep-alive
Vue的keep-alive: 简答的做下理解 缓存!页面从某一个页面跳转到另一个页面的时候,需要进行一定的缓存,然后这个时候调用的钩子函数是actived,而在第一次加载的时候,created.ac ...
- PHP开发-最简单的数据库操作,使用ezSQL
PHP数据库操作使用ezSQL来实现,简单好用. 如果用的是mysql数据库,将下载的ezSQL文件中的mysql和shared连个文件夹拷贝到PHP工程目录中引用即可. 在PHP文件中 // Inc ...
- Deep Learning基础--随时间反向传播 (BackPropagation Through Time,BPTT)推导
1. 随时间反向传播BPTT(BackPropagation Through Time, BPTT) RNN(循环神经网络)是一种具有长时记忆能力的神经网络模型,被广泛用于序列标注问题.一个典型的RN ...
- Deep Learning基础--word2vec 中的数学原理详解
word2vec 是 Google 于 2013 年开源推出的一个用于获取 word vector 的工具包,它简单.高效,因此引起了很多人的关注.由于 word2vec 的作者 Tomas Miko ...
- 集合框架之Set学习
前言: 1.何为框架:可以理解为一个基础结构,在基础结构上进行进一步开发会变得很方便. 2.三种集合类型:集合(Set) :元素无序不可重复: 列表(List) :元素有序可重复: 映 ...
- C/C++——C语言跳出多重循环方法
c语言的break语句只能跳出离它最近的一层循环,但是我们有时候需要跳出多层循环,以下有几种跳出多重循环的方法: 1. 使用goto ; i < MAX1; i++) { ; j < MA ...