洛谷——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 ...
随机推荐
- springsecurity basic 认证
Basic Access Authentication scheme是在HTTP1.0提出的认证方法,它是一种基于challenge/response的认证模式,针对特定的realm需要提供用户名和密 ...
- poi复杂excel的实现
一:前言 最近帮一个朋友做excel的导出功能,对于我来说还是挺头疼,我看了下表格样式,对于我来说还是挺头疼的,想当年耗子刚刚出社会的时候做的第一份工作,第一份任务就是把把word转换为html,在这 ...
- HDU 6206 青岛网络赛1001 高精度 简单几何
给出的数据1e12规模,常规判点是否在圆范围内肯定要用到半径,求得过程中无法避免溢出,因此用JAVA自带的浮点大数运算,和个ZZ一样比赛中eclipse出现问题,而且太久没写JAVA语法都不清楚变量忘 ...
- 【SRM20】数学场
第一题 n个m位二进制,求异或值域总和. [题解]异或值域--->使用线性基,解决去重问题. m位二进制--->拆位,每位根据01数量可以用组合数快速统计总和. #include<c ...
- Unity下实现弹簧骨骼(Spring Bone)
关于这个效果的名称,我一直没找到一个比较正式的说法.Spring Bone这个说法是来自于Anima2D这个插件中的一个演示用的脚本,我直接译成弹簧骨骼. 一般常见于对人物的头发的模拟上. 当然也可以 ...
- python中赋值、浅拷贝、深拷贝详解(转)
一.赋值 >>> a = [1, 2, 3]>>> b = a>>> print(id(a), id(b), sep='\n')139701469 ...
- PHP中的 get_magic_quotes_runtime
get_magic_quotes_runtime() 获得外部文件及数据库资料时是否进行转义 set_magic_quotes_runtime(1); 临时设置获得外部文件及数据库资料时是否进行转义 ...
- device tree --- #address-cells and #size-cells property
device tree source Example1 / { #address-cells = <0x1>; // 在 root node 下使用 1 個 u32 來代表 address ...
- (转)USB体系结构
转载地址:http://blog.ednchina.com/zenhuateng/203584/Message.aspx USB总线接口层:物理连接.电气信号环境.信息包传输机制:主机一方由USB主控 ...
- 升级vs17中的cordova-simulate
visual studio 17自带的cordova-simulate有一个bug,动态添加的html代码里面如果带有header,会出现js异常导致后面js程序终止执行,这个问题已经给他们提了iss ...