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 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。
输出格式:
一个整数,表示最长的最短路得长度。
输入输出样例
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
说明

最短路裸题,来回两边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的更多相关文章
- 【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解
题目链接:https://www.luogu.org/problemnew/show/P1821 反向多存一个图,暴力跑两遍 #include <cstdio> #include < ...
- 洛谷——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 题解
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
题目描述 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 & ...
- 「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party
更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...
- [USACO07FEB]银牛派对Silver Cow Party
题目简叙: 寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100). 每头牛参加 ...
- [USACO07FEB]银牛派对Silver Cow Party---最短路模板题
银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...
随机推荐
- python实现单链表的反转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/usr/bin/env python #coding = utf-8 ...
- CSU-1986 玄学
题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=1986 题目 Description 阴阳师子浩君,最近从<初等数论 ...
- Fomo3D代码分析以及漏洞攻击演示
Fomo3D过去的一周内赚足了噱头,一场光明正大的"庞氏"游戏疯狂吸金,在链得得此前的报道中提到"Fomo3D的开发者,是对生态有深刻理解的现实主义者.Fomo3D鼓励黑 ...
- GCD 开发详情
目录 一.简介 二.dispatch Queue - 队列 三.dispatch Groups - 组 四.dispatch Semaphores - 信号量 五.dispatch Barriers ...
- 绑定域名到 GitHub Pages
简介 我在阿里云上注册了一个新域名:yuanzb.com,我已经在GitHub Pages上建立了自己的博客:http://yuanzb.github.io/yuanzb/.现在我希望将yuanzb. ...
- 设置RobotFramework的ftplibrary中,将Upload_file操作的异常改为回显错误信息。
测试中需要通过FTP通道,将数据发送给服务器,而这个上传的数据要被阻断.在结合RobotFramework测试中,安装的ftplibrary,使用upload_file操作,如果上传动作失败,会抛出异 ...
- DB2设置code page(日文943)
为了便于 DB2 在执行 DB2 命令或语句之后显示错误.警告和指示性消息,必须安装您期望使用的语言的 DB2 消息文件集.因为 DB2 有基于语言分组的不同分发版,您必须验证安装 CD-ROM 上有 ...
- 启动、停止、删除Windows服务
启动: @echo.服务启动...... @echo off @sc create Service_SMS binPath= "D:\公司制度等文件\项目\河北劳动力市场检测系统\Windo ...
- nginx配置C compiler cc is not found
1.需求 linux安装个编译器 参考资料:http://blog.csdn.net/testcs_dn/article/details/51461750
- vue倒计时页面
https://www.cnblogs.com/sichaoyun/p/6645042.html https://blog.csdn.net/sinat_17775997/article/detail ...