POJ3268(KB4-D spfa)
Silver Cow Party
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 23426 | Accepted: 10691 |
Description
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?
Input
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
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
Sample Output
10
Hint
Source
//2017-08-08
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; const int N = ;
const int INF = 0x3f3f3f3f;
struct Edge{
int v, w;
Edge(int _v = , int _w = ):v(_v), w(_w){}
};
vector<Edge> E[][N];
bool vis[N];
int dis[][N], cnt[N], n, m, x; bool spfa(int s, int n, int time){
memset(vis, false, sizeof(vis));
memset(dis[time], INF, sizeof(dis));
memset(cnt, , sizeof(cnt));
vis[s] = true;
dis[time][s] = ;
cnt[s] = ;
queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = ; i < E[time][u].size(); i++){
int v = E[time][u][i].v;
int w = E[time][u][i].w;
if(dis[time][v] > dis[time][u] + w){
dis[time][v] = dis[time][u] + w;
if(!vis[v]){
vis[v] = true;
q.push(v);
if(++cnt[v] > n)return false;
}
}
}
}
return true;
} int main()
{
while(scanf("%d%d%d", &n, &m, &x)!=EOF){
int a, b, c;
while(m--){
scanf("%d%d%d", &a, &b, &c);
E[][a].push_back(Edge(b, c));
E[][b].push_back(Edge(a, c));
}
spfa(x, n, );
spfa(x, n, );
int ans = ;
for(int i = ; i <= n; i++)
ans = max(ans, dis[][i]+dis[][i]);
printf("%d\n", ans);
} return ;
}
POJ3268(KB4-D spfa)的更多相关文章
- poj3268 Silver Cow Party(两次SPFA || 两次Dijkstra)
题目链接 http://poj.org/problem?id=3268 题意 有向图中有n个结点,编号1~n,输入终点编号x,求其他结点到x结点来回最短路长度的最大值. 思路 最短路问题,有1000个 ...
- poj3268 Silver Cow Party (SPFA求最短路)
其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #i ...
- Invitation Cards---poj1511(spfa)
题目链接:http://poj.org/problem?id=1511 有向图有n个点m条边,求点1到其他n-1个点的最短距离和+其他点到点1的最小距离和: 和poj3268一样,但是本题的数据范围较 ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- POJ-3268 Silver Cow Party---正向+反向Dijkstra
题目链接: https://vjudge.net/problem/POJ-3268 题目大意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号X,其他牛要去拜访它并且拜访完之后要返回 ...
- POJ1511 Invitation Cards —— 最短路spfa
题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Tota ...
- 【BZOJ-3627】路径规划 分层图 + Dijkstra + spfa
3627: [JLOI2014]路径规划 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 186 Solved: 70[Submit][Status] ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- sgu 240 Runaway (spfa)
题意:N点M边的无向图,边上有线性不下降的温度,给固定入口S,有E个出口.逃出去,使最大承受温度最小.输出该温度,若该温度超过H,输出-1. 羞涩的题意 显然N*H的复杂度dp[n][h]表示到达n最 ...
随机推荐
- C++ 执行Windows cmd命令
#include <windows.h> #include <iostream> #include <cstdio> using namespace std; vo ...
- python3.6使用f-string来格式化字符串
这里的f-string指的是以f或F修饰的字符串,在字符串中使用{}来替换变量,表达式和支持各种格式的输出.详细的格式化定义可以看官方文档 >>> a, b = 30, 20 > ...
- [Umbraco] Data Type之Render control
继续探讨Data Type.如果你创建过Data Type,你就会知道创建一个新的Data Type都需要指定一个Render control,这有点类似开始C#时用到的继承. 那么如何创建我们自己的 ...
- C++:实现类似MFC的IsKindOf功能
假设需要一个类别库,改类别库共包含以下5个类:GrandFather(祖父类).Father(父类).Son(儿子类).Daughter(女儿类).GrandSon(孙子类) 各个类之间的继承关系为: ...
- ubuntu安转QTcreator出现The default mkspec symlink is broken
QT Creator安装:https://blog.csdn.net/arackethis/article/details/42326967 QT SDK安装:https://blog.csdn.ne ...
- 读书笔记(02) - 可维护性 - JavaScript高级程序设计
编写可维护性代码 可维护的代码遵循原则: 可理解性 (方便他人理解) 直观性 (一眼明了) 可适应性 (数据变化无需重写方法) 可扩展性 (应对未来需求扩展,要求较高) 可调试性 (错误处理方便定位) ...
- 面试:vector类的简单实现
vector类的简单实现 #include <vector> #include <iostream> #include <cstring> #include < ...
- mysql索引总结(1)-mysql 索引类型以及创建
mysql索引总结(1)-mysql 索引类型以及创建 mysql索引总结(2)-MySQL聚簇索引和非聚簇索引 mysql索引总结(3)-MySQL聚簇索引和非聚簇索引 mysql索引总结(4)-M ...
- ASP.NET MVC5+EF6+LayUI实战教程,通用后台管理系统框架(5)- 创建项目结构
前言 关于理论知识,我的表达能力有限,知识水平有限,就不过多的讲解编程工作中的专用术语了,大家写的代码多了,自然就懂了 前几节课,我们看到了后台的主页面,以及一个自认为比较漂亮的登录界面,算是编程套路 ...
- 网络之AFNetworking
Json.Xml解析第三方库多了去,就不一一说明,现在开始进入AFNetworking.由于AFNetworking支持ARC,ASI不支持ARC,现在越来越多的开始使用AFNetworking. h ...