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. SQL生成随机字符串

    1.SQLserve生成随机字符串 SELECT replace(newid(), '-', '')

  2. poj 1088 滑雪问题

    滑雪问题 import java.util.Scanner; public class Main{ static int a[][],r,c; public static void main(Stri ...

  3. hdu1016Prime Ring Problem

     就是说,给你一个数n, 要你把1到n都连在一起成环. 每一个数不可反复, 且相连的两个数的和要是素数. 把全部情况输出来. 我是用dfs暴力出来的. 首先把素数打表, 然后每次顺时针预測下一个数 ...

  4. linux经常使用命令:打包、复制等

    备份文件 tar -cf /home/app20140703bak.tar /home/app/uat/test.war 拷贝文件到目标目录 例示: cp -af /app/wasapp/appnam ...

  5. (转)javascript中 window.location= window.location;是什么意思

    重载当前页面.但是,重载后,一些$_GET或者$_POST $_REQUEST 表单提交的数据会清空.   重新加载当前页面的资源,就是刷新本页面

  6. Struts2之Action基础与配置

    Action基础 Action是什么 在Struts2中,一个Action类代表一次请求或调用,每个请求的动作都对应于一个相应的Action类,一个Action类是一个独立的工作单元.也就是,用户的每 ...

  7. 安装ubuntu14.10系统的那些瞎折腾

    前段时间自作孽,安装了ubuntu14.04的64位系统,而我的笔记本又是那种老古董,2G的内存所以装好之后各种不稳定,索性这个周末就重装一下吧,本来打算是直接装我以前的那个ubuntu12.04-i ...

  8. Oracle:ORA-01791: 不是 SELECTed 表达式

     项目中写hql语句 出现 ORA-01791: 不是 SELECTed 表达式问题. 语句如下: select distinct(name) where student order by numbe ...

  9. mac Word 怎样放大缩小文档结构图文字大小

    在文档结构图的侧栏里按住control+option,然后滑动鼠标滚轮/双指上下滚动触摸板.

  10. 2015年网易考拉海淘android面试

    经朋友推荐,昨天下午去网易杭州公司参加了考拉海淘android客户端的面试.今天回忆一下面试题目,做个整理进行备案. 1.说说JVM垃圾回收机制. 1.1.画了JVM分代回收的图,大致说了下垃圾分代回 ...