描述:

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.
 

题解:

  要求的其实就是1-n的每个点到x以及从x回来的最短路之和的最大值。就可以用两次dijkstra算法求解,一次算从i到x,一次算从x到i,最后求得相加的最大值即可。

代码:

#include <iostream>
#include <stdio.h> using namespace std;
#define inf 1<<29
int n,m,x;
bool vis[];
int map[][];
int go[],dback[]; //go是从i—>x back是从x—>i int dijkstra()
{
int i,j,f,v;
for(i=;i<=n;i++)
{
vis[i]=;
go[i]=map[i][x];
dback[i]=map[x][i];
} for(i=;i<=n;i++)
{
f=inf;
for(j=;j<=n;j++)
{
if(!vis[j]&&dback[j]<f)
{
v=j;
f=dback[j];
}
}
vis[v]=;
for(j=;j<=n;j++)
if(!vis[j]&&map[v][j]+dback[v]<dback[j])
dback[j]=map[v][j]+dback[v];
} for(i=;i<=n;i++) vis[i]=; for(i=;i<=n;i++)
{
f=inf;
for(j=;j<=n;j++)
{
if(!vis[j]&&go[j]<f)
{
v=j;
f=go[j];
}
}
vis[v]=;
for(j=;j<=n;j++)
{
if(!vis[j]&&map[j][v]+go[v]<go[j])
go[j]=map[j][v]+go[v];
}
} f=-;
for(i=;i<=n;i++)
{
if(go[i]+dback[i]>f)
f=go[i]+dback[i];
}
return f;
} int main()
{
int a,b,c;
while(~scanf("%d%d%d",&n,&m,&x)){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(i!=j) map[i][j]=inf;
else map[i][j]=;
}
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
map[a][b]=c;
}
printf("%d\n",dijkstra());
}
return ;
}
 
 

poj 3268 Silver Cow Party(最短路dijkstra)的更多相关文章

  1. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  2. POJ 3268 Silver Cow Party (双向dijkstra)

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

  3. POJ 3268 Silver Cow Party 最短路

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

  4. poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13611   Accepted: 6138 ...

  5. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  6. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  7. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

  8. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  9. POJ 3268 Silver Cow Party 单向最短路

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22864   Accepted: 1044 ...

随机推荐

  1. 深入理解SpringIOC容器

    转载来源:[https://www.cnblogs.com/fingerboy/p/5425813.html] 前言: 在逛博客园的时候突然发现一篇关于事务的好文章,说起spring事物就离不开AOP ...

  2. [ffmpeg] AVOption

    在ffmpeg中,常使用AVOption的API来进行参数设置.AVOption的API主要分为设置参数以及提取参数两种,无论是哪一种API都主要分为两大步骤: 寻找出参数所在的内存位置. 如果是设置 ...

  3. ILRuntime_NewbieGuide—进阶

    进阶篇其实要求你应该拥有一个云服务器才有意思,但你用本地电脑也是一样的道理,只是没有这么有趣了. 笔者大一的时候,腾讯云搞活动,学生认证可以抢到1元的云主机,配置很低,但是平时练练手还是可以的,现在没 ...

  4. P1536 村村通

    原题链接 https://www.luogu.org/problemnew/show/P1536 昨天刚学的并查集,今天正好练习一下,于是就找到了这个题 看起来好像很简单,尤其是你明白了思路之后,完全 ...

  5. freeCodeCamp中一些有趣的方法

    1. 过滤对象(找到数组中(collection)与之(source)匹配对象,过滤掉数组中(collection)不匹配对象) function where(collection, source) ...

  6. android wake lock 电源管理简单学习

    需要配置清单文件:<uses-permission android:name="android.permission.WAKE_LOCK" /> 也可以参考我之前写的这 ...

  7. 【BZOJ4030】[HEOI2015]小L的白日梦

    [BZOJ4030][HEOI2015]小L的白日梦 题面 BZOJ 洛谷 题解 要求的是最小的不开心连续段的期望. 然后发现自己就不会做了. 然后就可以来抄题解啦. 首先来猜性质: 第一个,一定是按 ...

  8. 【学习笔记】JS知识点整理

    1 原型/原型链 1-1 原型 定义:原型是function对象的一个属性,定义了构造函数制造出的对象的公共祖先.通过该构造函数产生的对象,可以继承该原型的属性和方法. 原型是一个对象. 可以利用原型 ...

  9. css解决图片拉伸问题

    在实际场景中,我们经常会遇到图片大小固定的需求,但是由于原始图片大小,比例不一样,不同图片以相同的大小展示会参差不齐.解决方法就是object-fit或者background-size属性.他们的区别 ...

  10. Python的开发之路

    一.python入门 二.基本数据类型 三.输入与输出 四.基本运算符 五 .流程控制之if判断 六.流程控制之while循环 七.流程控制之for循环 八.数据类型与内置方法 九.文件的处理 十.字 ...