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最 ...
随机推荐
- Codeforces Round #425 (Div. 2) B. Petya and Exam(字符串模拟 水)
题目链接:http://codeforces.com/contest/832/problem/B B. Petya and Exam time limit per test 2 seconds mem ...
- Java代码审计连载之—SQL注入
前言近日闲来无事,快两年都没怎么写代码了,打算写几行代码,做代码审计一年了,每天看代码都好几万行,突然发现自己都不会写代码了,真是很DT.想当初入门代码审计的时候真是非常难,网上几乎找不到什么java ...
- centos7----pstree
centos 默认没有pstree 安装 yum -y install psmisc
- 一步步Cobol 400上手自学入门教程04 - 过程部
过程部的作用:编写程序要执行的语句,是程序的核心. 结构: 基本语句 INITIALIZE 设置数据项的初始值 ACCEPT 接收从键盘或指定设备上获得输入数据. 例子: 当批处理文件读到调用ACCP ...
- python高并发?
参考: https://yunsonbai.top/
- Numpy.random中shuffle与permutation的区别(转)
huffle与permutation的区别 函数shuffle与permutation都是对原来的数组进行重新洗牌(即随机打乱原来的元素顺序):区别在于shuffle直接在原来的数组上进行操作,改变原 ...
- GPS/轨迹追踪、轨迹回放、围栏控制
折腾一个多月终于弄完了这个项目,起初都未曾接触GPS/轨迹追踪.轨迹回放.圈划围栏...等一些在百度地图或者Googel地图操作的一些业务,后端的业务相对来说简单点 cas单点登录,mongdb灵活的 ...
- ElasticSearch入门3: 高级查询
单字段 模糊匹配查询与精准查询 postman请求 POST 127.0.0.1:9200/book/_search 请求json: { "query":{ "match ...
- ubuntu安转QTcreator出现The default mkspec symlink is broken
QT Creator安装:https://blog.csdn.net/arackethis/article/details/42326967 QT SDK安装:https://blog.csdn.ne ...
- Java 多线程学习笔记:生产者消费者问题
前言:最近在学习Java多线程,看到ImportNew网上有网友翻译的一篇文章<阻塞队列实现生产者消费者模式>.在文中,使用的是Java的concurrent包中的阻塞队列来实现.在看完后 ...