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最 ...
随机推荐
- springboot常用注解
@SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration注解.其中@ComponentScan让 ...
- underscore.js源码研究(8)
概述 很早就想研究underscore源码了,虽然underscore.js这个库有些过时了,但是我还是想学习一下库的架构,函数式编程以及常用方法的编写这些方面的内容,又恰好没什么其它要研究的了,所以 ...
- MySQL 逻辑物理备份测试
目录 逻辑备份 mysqldump 普通备份 mysqlpump 并行备份 mysqlpump 压缩并行备份 mydumper 并行备份 mydumper 并行压缩备份 小结 物理备份 xtrabac ...
- oracle安装完成之后,执行sqlplus命令,提示:bash: sqlplus: command not found...
1.安装完oracle后,用终端登录,因为不是直接用oracle用户登录的,所以我就执行$ su oracle切换成oracle用户之后,执行"echo $ORACLE_HOME" ...
- Windows平台下搭建自己的Git服务器
该文章转自:http://www.codeceo.com/article/windows-git-server.html Gitblit 是一个纯 Java 库用来管理.查看和处理 Git 资料库,相 ...
- C#简单操作MongoDB
一 安装MongoDB 官网按需下载, 安装, 一步到位. 二 VS创建新项目 创建一个.netcore console项目, 然后nuget安装驱动MongoDB.Driver 三 建立连接 在Pr ...
- 字符、字符串和文本的处理之Char类型
.Net Framework中处理字符和字符串的主要有以下这么几个类: (1).System.Char类 一基础字符串处理类 (2).System.String类 一处理不可变的字符串(一经创建,字符 ...
- SimpleVisitorMemberType类的visitClassType解读
举个例子,如下: class CA<T>{ public T getVal(){ return null; } } interface IA{} interface IB{} public ...
- Linux的僵尸进程及其解决方法
1. 产生原因: 在UNIX 系统中,一个进程结束了,但是他的父进程没有等待(调用wait / waitpid)他,那么他将变成一个僵尸进程.通过ps命令查看其带有defunct的标志.僵尸进程是一个 ...
- k8s服务网关ambassador部署
1.ambassador是datawire开源的服务网关,很好的支持kubernetes.具体详细介绍参考官网:https://www.getambassador.io/about/why-ambas ...