洛谷——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 ...
随机推荐
- BZOJ 2083 vector的巧用+二分
2083: [Poi2010]Intelligence test Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 469 Solved: 227[Su ...
- 重构改善既有代码设计--重构手法11:Move Field (搬移字段)
你的程序中,某个字段被其所驻类之外的另一个类更多的用到.在目标类建立一个新字段,修改源字段的所有用户,令它们改用新字段. 动机:在类之间移动状态和行为,是重构过程中必不可少的措施.随着系 ...
- [php排错] Forbidden You don't have permission to access / on this server.
刚开始接触PHP,在搭建完环境后发现输入127.0.0.1可以访问界面,但是输入http://localhost却提醒无权访问,在百度之后发现是php中的httpd.conf的作用 在wamp中搜索发 ...
- 51nod 1073 约瑟夫环
题目链接 先说一下什么是约瑟夫环,转自:传送门 关于约瑟夫环问题,无论是用链表实现还是用数组实现都有一个共同点:要模拟整个游戏过程,不仅程序写起来比较烦,而且时间复杂度高达O(nm),当n,m非常大( ...
- zepto.js 实现原理解析
zepto 是移动端常用的 dom 库,代码轻巧,操作方式类同 jquery.那么 zepto 的核心实现原理是什么呢?
- tf.segment_sum和tf.unsorted_segment_sum理解实例
本文来自 guotong1988 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/guotong1988/article/details/77622790 import ...
- isolation forest进行异常点检测
一.简介 孤立森林(Isolation Forest)是另外一种高效的异常检测算法,它和随机森林类似,但每次选择划分属性和划分点(值)时都是随机的,而不是根据信息增益或者基尼指数来选择.在建树过程中, ...
- weblogic nmap扫描脚本
CVE-2018-2894 / Nmap利用脚本,可批量批量快速扫描getshell.检测漏洞.利用漏洞 地址:https://github.com/Rvn0xsy/nse_vuln/tree/ma ...
- imperva agent 的重新注册
情况是这样 公司搭了一个环境有mysql的数据库并且安装了agent,imperva管理平台上也可以看到agent的注册信息,但是没想到的是有人把我的虚机给还原快照了,而且还没保存..... 这次写个 ...
- 010 JVM类加载
转自http://www.importnew.com/23742.html 前言 我们知道我们写的程序经过编译后成为了.class文件,.class文件中描述了类的各种信息,最终都需要加载到虚拟机之后 ...