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. sem_timedwait的用法

    #include <semaphore.h> int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout); Link ...

  2. python threading基础学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' """ python是支持多线程的,并 ...

  3. (史上最全的ios源码汇总)

    按钮类         按钮 Drop Down Control         http://www.apkbus.com/android-106661-1-1.html 按钮-Circular M ...

  4. c++编程碰到的奇怪问题与解决

    今天写一个工具,调试过程中莫名其妙崩溃,类某些成员变量指针很奇怪,为0x00003001.最后检查的结果居然是这样的: 文件class1.h: class1 { int a; int b; } 文件: ...

  5. Android应用程序绑定服务(bindService)的过程源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6745181 Android应用程序组件Serv ...

  6. CLR via C# - Char_String

    .NET中Char表示为16为的Unicode值,Char提供两个public const字段MinValue('\0',写成'\u0000'也是一样的)和MaxValue('\uffff'). Ch ...

  7. C#遍历获取枚举的值,名和属性

    获取: Type type = typeof(ParamServiceType); var values = Enum.GetValues(type); ; i < values.Length; ...

  8. vue中关于computed的一点理解

    computed相当于属性的一个实时计算,如果实时计算里关联了对象,那么当对象的某个值改变的时候,同事会出发实时计算. 例子: <body id="content"> ...

  9. Git入门——基础知识问答

    问题一:为什么要选择Git作为Android开发的版本控制工具?     答:1)git是android项目和社区的统一语言.            2)高通版本发布频繁,需要与平台及时同步,快速re ...

  10. JAVA按字节读取文件

    JAVA的IO流一直都是我比较头疼的部分(我没有系统学过JAVA,一般需要实现什么功能再去看文档). 最近遇到一个需求:一个字节一个字节地读取一个文件.网上很多方法,代码一大堆.我在这里和大家分享一个 ...