洛谷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
说明

分析:一道比较水的题,先求单源最短路,然后把边反过来再求一边就好了.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue> using namespace std; const int maxn = ,maxm = ,inf = 0x7ffffff; int n,m,x,head[maxn],to[maxm],nextt[maxm],tot = ,a[maxm],b[maxm],t[maxm],ans[maxn],w[maxm],d[maxn],vis[maxn];
int maxx; void add(int x,int y,int z)
{
w[tot] = z;
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void spfa()
{
memset(vis,,sizeof(vis));
queue <int> q;
q.push(x);
for (int i = ; i <= n; i++)
d[i] = inf;
vis[x] = ;
d[x] = ;
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for (int i = head[u];i;i = nextt[i])
{
int v = to[i];
if (d[v] > d[u] + w[i])
{
d[v] = d[u] + w[i];
if (!vis[v])
{
vis[v] = ;
q.push(v);
}
}
}
}
} int main()
{
scanf("%d%d%d",&n,&m,&x);
for (int i = ; i <= m; i++)
{
scanf("%d%d%d",&a[i],&b[i],&t[i]);
add(a[i],b[i],t[i]);
}
spfa();
for (int i = ; i <= n; i++)
ans[i] = d[i];
memset(head,,sizeof(head));
tot = ;
for (int i = ; i <= m; i++)
add(b[i],a[i],t[i]);
spfa();
for (int i = ; i <= n; i++)
{
ans[i] += d[i];
maxx = max(maxx,ans[i]);
}
printf("%d\n",maxx); return ;
}
洛谷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 题解
P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- 洛谷 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 ...
随机推荐
- Ryu学习总结(持续更新)
Ryu学习总结 该篇学习笔记,与其他分析Ryu控制器代码的笔记不同,主要按照程序的构成来进行分块总结,由于本人为新手入门,不能保证没有错误,如果发现错误,欢迎指教. 以下的内容主要来源: 源码 官方文 ...
- Redis5.0:在这些场景使用,高效率还低成本!
很多大型电商网站.视频直播和游戏应用等,存在大规模数据访问,对数据查询效率要求高,且数据结构简单,不涉及太多关联查询. 这种场景使用Redis,在速度上对传统磁盘数据库有很大优势,能够有效减少数据库磁 ...
- Java线上应用故障排查之一:高CPU占用 (转)
一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环. (友情提示:本博文章欢迎转载,但请注明出处:hankchen,http://www.blogjava.net/hank ...
- call appiy
其实就是动态的改变this了,下面例子就说明了... function add(a, b){ console.dir(this); } function sub(a, b){ console.dir( ...
- java 数据存储
简单的记录一下而已. 1.寄存器: 特点:快,存储有限. 存储地点:处理器内部. 2.堆栈 特点:仅次于寄存器快,通过堆栈指针在处理器获取支持.堆栈指针下移,分配内存,上移,释放内存.此外须知生命周期 ...
- 提升Android ListView性能的几个技巧
ListView如何运作的? ListView是设计应用于对可扩展性和高性能要求的地方.实际上,这就意味着ListView有以下2个要求: 尽可能少的创建View: 只是绘制和布局在屏幕上可见的子Vi ...
- “Hello World!”团队第六周的第二次会议
今天是我们团队“Hello World!”团队第六周召开的第二次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...
- C#从一个窗体传递参数到另一个窗体的链接
http://blog.sina.com.cn/s/blog_60d69ce00100eldt.html
- laravel5.6 调用第三方类库
大概流程: 1. 新建一个目录方类库 2. 配置composer配置文件 3. 在项目中使用终端运行composer dumpautoload 4. 使用时 方法调用可以new对象后->方法名 ...
- CDN加速-内容分发网络
内容分发网络 (互联网技术) 编辑 CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输 ...