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最 ...
随机推荐
- Python大黑阔—url采集+exp验证,带你批量测试
i春秋作家:大木瓜 前言: 最近几天在整理从各处收集来的各种工具包,大大小小的塞满了十几个G的硬盘,无意间发现了一个好几年前的0day.心血来潮就拿去试了一下,没想到真的还可以用,不过那些站点都已经老 ...
- maven项目无法新增java test目录的问题
有时候当我们构建好maven项目时,再导入eclipse中会缺少src/main/java 和src/test/java,这是需要我们手动创建: 但是有时候在 项目视图下或者 enterprise ...
- 【2019北京集训2】duck 线段树优化建图+tarjan
题目大意:给你$n$个点,第$i$个点有点权$v_i$.你需要将这$n$个点排成一排,第$i$个点的点权能被累加当且仅当这个点前面存在编号在$[l_i,r_i]$中的点,问你这些点应该如何排列,点权和 ...
- [Leetcode]120.三角形路径最小和
---恢复内容开始--- 题目的链接 简单的动态规划题,使用了二维dp数组就能很好的表示. 由于有边界的问题,所以这个dp数组为 dp[n+1][n+1]. dp[i][j]意思是终点为(i-1,j- ...
- CentOS6.8 安装 Oracle11.2.0.4
1. 安装操作系统 安装的时候选择中文+英文支持 注意分区: swap sda盘做系统盘 sdb盘做数据盘 配置完成后的服务器分区路径信息: [root@dbserver ~]# df -h File ...
- JavaSE-java8-谓词复合的用法
谓词接口包括三个方法: negate. and 和 or,让你可以重用已有的Predicate来创建更复杂的谓词 一.比如可以用negate方法来返回一个Predicate非 public class ...
- php -- 数学函数
----- 016-math.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv="c ...
- springboot+zuul(一)------实现自定义过滤器、动态路由、动态负载。
参考:https://blog.csdn.net/u014091123/article/details/75433656 https://blog.csdn.net/u013815546/articl ...
- mycat ER 分片表
<table name="order" dataNode="dn$1-32" rule="mod-long"> <chil ...
- Ajax初始接触
演示JS对象的属性,方法和事件的使用 (1)window.location.href (2)form.submit() <form action="" method=&quo ...