Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15156   Accepted: 6843

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 ≤ XN). 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

Line 1: Three space-separated integers, respectively: N, M, and X
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

Line 1: One integer: the maximum of time any one cow must walk.

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

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

Source

题意:两次最短路,第一次求x到其余各点的最短路,第二次求各点到x的最短路。前者易于解决,直接应用dijkstra或其他最短路算法即可,后者要先将邻接矩阵转置再执行最短路算法。

为什么进行矩阵转置?比如u(u != x)到x的最短路为<u,v1>,<v1,v2>,<v2,v3>,...,<vi, x>,这条路径在转置邻接矩阵后变成<x,vi>,...,<v3,v2>,<v2, v1>,<v1,u>.于是乎,在转置邻接矩阵后,执行最短路算法求出x到u的最短 路<x,vi>,...,<v3,v2>,<v2, v1>,<v1,u>即可得到转置前u到x的最短路。
收获:dijkstra紫书上的模板,矩阵转置操作。
 #include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
const int maxn=+; int w[maxn][maxn];
int v[maxn], d[maxn], c[maxn];
int n, m, x; void dijkstra(int k)
{
memset(v, , sizeof(v));
for(int i = ; i <= n; i++) d[i] = (i==k ? : INF);
for(int i = ; i <= n; i++){
int x1, m = INF;
for(int y = ; y <= n; y++) if(!v[y] && d[y]<=m) m = d[x1=y];
v[x1] = ;
for(int y = ; y <= n; y++) d[y] = min(d[y], d[x1]+w[x1][y]);
}
}//dijkstra紫书模板
void tran() {
for(int i = ; i <= n; i++) {
for(int j = ; j <= i; j++)
swap(w[i][j], w[j][i]);
}
}//矩阵转置
int main()
{
while(~scanf("%d%d%d", &n, &m, &x)) {
int a, b, t;
memset(w, INF, sizeof(w));
for(int i = ; i < m; i++) {
scanf("%d%d%d", &a, &b, &t);
w[a][b] = min(w[a][b], t);
}
memset(c, , sizeof(c));
dijkstra(x);
for(int i = ; i <= n; i++) c[i] = d[i];
tran();
dijkstra(x);
int ans = -;
for(int i = ; i <= n; i++) {
c[i] += d[i];
ans = max(ans,c[i]);
}
printf("%d\n", ans);
}
}

POJ3268 Silver Cow Party(dijkstra+矩阵转置)的更多相关文章

  1. POJ3268 Silver Cow Party Dijkstra最短路

    Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to atten ...

  2. poj3268 Silver Cow Party(两次SPFA || 两次Dijkstra)

    题目链接 http://poj.org/problem?id=3268 题意 有向图中有n个结点,编号1~n,输入终点编号x,求其他结点到x结点来回最短路长度的最大值. 思路 最短路问题,有1000个 ...

  3. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  4. POJ 3268 Silver Cow Party 最短路径+矩阵转换

    Silver Cow Party Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) T ...

  5. POJ3268 Silver Cow Party —— 最短路

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  6. POJ-3268 Silver Cow Party---正向+反向Dijkstra

    题目链接: https://vjudge.net/problem/POJ-3268 题目大意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号X,其他牛要去拜访它并且拜访完之后要返回 ...

  7. poj3268 Silver Cow Party(两次dijkstra)

    https://vjudge.net/problem/POJ-3268 一开始floyd超时了.. 对正图定点求最短,对逆图定点求最短,得到任意点到定点的往返最短路. #include<iost ...

  8. 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 ...

  9. poj3268 Silver Cow Party(农场派对)

    题目描述 原题来自:USACO 2007 Feb. Silver N(1≤N≤1000)N (1 \le N \le 1000)N(1≤N≤1000) 头牛要去参加一场在编号为 x(1≤x≤N)x(1 ...

随机推荐

  1. 【转】20个Java 代码生成器

    From: http://www.cnblogs.com/skyme/archive/2011/12/22/2297592.html 1.1 CodeSmith 一款人气很旺国外的基于模板的dotne ...

  2. 4Sum 解答

    Question Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c  ...

  3. Openstack REST API

    There are some high quality resources that already cover the OpenStack API, so this is a YEA (yet an ...

  4. 《Java程序员面试笔试宝典》之 什么是AOP

    AOP(Aspect-Oriented Programming,面向切面编程)是对面向对象开发的一种补充,它允许开发人员在不改变原来模型的基础上动态地修改模型从而满足新的需求.例如,在不改变原来业务逻 ...

  5. Spark函数详解系列之RDD基本转换

    摘要:   RDD:弹性分布式数据集,是一种特殊集合 ‚ 支持多种来源 ‚ 有容错机制 ‚ 可以被缓存 ‚ 支持并行操作,一个RDD代表一个分区里的数据集   RDD有两种操作算子:         ...

  6. Docker image 镜像介绍

    操作镜像 使用 docker 命令行操作 docker 镜像 获取镜像 使用「docker pull +镜像名称」从网络上下载image镜像 core@localhost ~ $ docker pul ...

  7. 关于vi不正常退出产生的swp文件

    关于vi不正常退出产生的swp文件   非正常关闭vi编辑器时会生成一个.swp文件 关于swp文件 使用vi,经常可以看到swp这个文件,那这个文件是怎么产生的呢,当你打开一个文件,vi就会生成这么 ...

  8. Linux用户和用户组

    用户分类 按位置分:本地账户.远程账户 按功能分:普通用户.超级用户(root) 普通用户: (1)系统用户:UID 1-499 (2)本地用户:UID 500+ 每一个用户,都有一个同名的用户组. ...

  9. 关于jQuery获取checkbox状态的问题

    这位大神概括的很好 http://www.cnblogs.com/wangkongming/p/4002710.html

  10. 配置元素customErrors

    Asp.net配置文件的配置方式,其实在MSDN里面是写得最清楚的了.可惜之前一直未曾了解到MSDN的强大. 先贴个地址:http://msdn.microsoft.com/zh-cn/library ...