POJ3268 Silver Cow Party —— 最短路
题目链接:http://poj.org/problem?id=3268
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24527 | Accepted: 11164 |
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; int n, m, X;
int g[MAXN][MAXN]; int dis1[MAXN], dis2[MAXN];
bool vis[MAXN];
void dijkstra(int st, int dis[])
{
memset(vis, , sizeof(vis));
for(int i = ; i<=n; i++)
dis[i] = (i==st?:INF); for(int i = ; i<=n; i++)
{
int k, minn = INF;
for(int j = ; j<=n; j++)
if(!vis[j] && dis[j]<minn)
minn = dis[k=j]; vis[k] = ;
for(int j = ; j<=n; j++)
if(!vis[j] && g[k][j]!=INF)
dis[j] = min(dis[j], dis[k]+g[k][j]);
}
} int main()
{
while(scanf("%d%d%d", &n, &m, &X)!=EOF)
{
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
g[i][j] = INF;
for(int i = ; i<=m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
g[u][v] = w;
}
dijkstra(X, dis1); //第一次跑最短路,计算X到各点的最短距离 for(int i = ; i<=n; i++) //将边取反
for(int j = i+; j<=n; j++)
swap(g[i][j], g[j][i]);
dijkstra(X, dis2); //第二次跑最短路,计算X到各点的距离,但因为边取反了,所以实际上是各点到X的最短距离。 int ans = ;
for(int i = ; i<=n; i++) //取两段距离之和的最大值
ans = max(ans, dis1[i]+dis2[i]);
printf("%d\n", ans);
}
}
POJ3268 Silver Cow Party —— 最短路的更多相关文章
- POJ3268 Silver Cow Party(dijkstra+矩阵转置)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15156 Accepted: 6843 ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13611 Accepted: 6138 ...
- POJ3268 Silver Cow Party Dijkstra最短路
Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to atten ...
- POJ3268 Silver Cow Party【最短路】
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...
- poj3268 Silver Cow Party(最短路)
非常感谢kuangbin专题啊,这道题一开始模拟邻接表做的,反向边不好处理,邻接矩阵的话舒服多了. 题意:给n头牛和m条有向边,每头牛1~n编号,求所有牛中到x编号去的最短路+回来的最短路的最大值. ...
- poj3268 Silver Cow Party (SPFA求最短路)
其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #i ...
- (poj)3268 Silver Cow Party 最短路
Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...
随机推荐
- hdu5396 Expression
Expression Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- testng自定义html报告,根据freemaker生成
[转] https://testerhome.com/topics/3487 [参考]https://www.cnblogs.com/cheese320/p/8890929.html 做了些修改,换 ...
- 背单词(bzoj 4567)
Description Lweb 面对如山的英语单词,陷入了深深的沉思,“我怎么样才能快点学完,然后去玩三国杀呢?”.这时候睿智 的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计 ...
- Cookie 和 Session 有什么区别呢?
Cookie 和 Session 有什么区别呢?大部分的面试者应该都可以说上一两句,比如:什么是 Cookie?什么是 Session?两者的区别等 但如果再往深入探讨的话,就慢慢有一些朋友不太了解了 ...
- TortoiseSVN如何更换或重置登录用户
昨天手贱把svn重新卸载了,再安装后便与之前的项目断了,因为第一次使用这个,也不清楚再怎么登录,还有就是上次是使用别人的账号,也不知道怎么清除别人的账号. 鼠标右键找到settings,点击打开 找到 ...
- git(二):一些简单入门命令
一.创建仓储(版本库) 可以创建在空目录下创建git仓库,也可以在已有项目里创建git仓储. $ mkdir NewName //仓储名 $ cd Newname //进入到该仓储目录中 $ git ...
- codevs——1958 刺激
1958 刺激 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description saffah的一个朋友S酷爱滑雪,并且追求刺 ...
- 洛谷 P1503鬼子进村
题目背景 小卡正在新家的客厅中看电视.电视里正在播放放了千八百次依旧重播的<亮剑>,剧中李云龙带领的独立团在一个县城遇到了一个鬼子小队,于是独立团与鬼子展开游击战. 题目描述 描述 县城里 ...
- DTD概述
1. 什么是XML文件 可扩展标记语言,标准通用标记语言的子集,是用于标记电子文件使其具有结构性的标记语言. 2. 什么是dtd文件 DTD(文档类型定义)的作用是定义XML文档的合法构建模块.它使用 ...
- time machine不备份指定文件夹
osx中常常会使用timemachine来备份一些文件,timemachine能够使某个文件夹恢复到之前某个时刻的状态,很的方便.但是备份须要空间,特别是有些我们并不想备份一些无关紧要的文件,比方电影 ...