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最 ...
随机推荐
- LOJ#3048. 「十二省联考 2019」异或粽子(trie树+堆)
题面 传送门 题解 我们先把它给前缀异或和一下,然后就是要求前\(k\)大的\(a_i\oplus a_j\).把\(k\)乘上个\(2\),变成前\(2k\)大的\(a_i\oplus a_j\), ...
- Linux的基本操作
1.linux系统的基本命令 ls 查看当前所在文夹下的内容pwd 查看当前所在的位置cd 打开文件目录touch 创建文件, 如果文件不存在, 就创建新的文件mkdir 创建文件夹rm 删 ...
- idea 没有智能提示
开发工具要对源码产生提示,现在比较流行的是使用 .d.ts 文件 来编写提示消息 以下的解决办法不是根本之道,估计是开发工具有什么地方可以设置 自动下载 .d.ts 文件,知道的读者还请留言赐教~ 使 ...
- 帝国CMS-后台管理工具
后台管理工具 apache+mysql 搭建的后台管理工具 参考手册*************http://www.phome.net/doc/ecmsedu/ 1.安装----- 使用的一键安装包. ...
- vmware workstation 下安装ubuntu
安装时我是借鉴 https://blog.csdn.net/xiaohua0877/article/details/78507631 期间遇到几个问题 键盘不好使,解决办法是重新运行wmware wo ...
- 27-hadoop-hbase安装
hbase的安装分为单机模式和完全分布式 单机模式 单机模式的安装很简单, 需要注意hbase自己内置一个zookeeper, 如果使用单机模式, 那么该机器的zookeepr不可以启动 1, 添加j ...
- elasticSearch6源码分析(9)ActionModule
1.ActionModule概述 /** * Builds and binds the generic action map, all {@link TransportAction}s, and {@ ...
- org.apache.commons.lang.StringUtils的常用方法
org.apache.commons.lang.StringUtils是apache的commons-lang-x.x.jar下的包,里面包含很多字符串操作方法, 官网(http://commons. ...
- C编程基础
1. Hello World! 依照惯例首先Hello World镇楼: 1 #include<stdio.h> 2 3 int main(void) { 4 printf("H ...
- Java maven项目的小随笔
1.web.xml里面有filter拦截设置,注意. 2.编译之后,网页中读取资源的路径是apache-tomcat/wtpwebapps/..,若该路径下没有相应资源,则报404错误.