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

Line 1: Three space-separated integers, respectively: NM, and X 
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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
Sample Input

Sample Output

题意:

  有编号为1-N的牛,它们之间存在一些单向的路径。给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求这些牛中所花的最长的来回时间是多少。

  输入第一行n,m,x   表示n头牛 m条路  起点x

思路:

  每头牛返回的最短时间很简单就可以算出来,这相当于从目标牛为起点求单源最短路径。

  但每头牛出发到目标牛的最短时间无法直接算出来,稍微转换一下,发现这个最短时间其实可以通过把矩阵转置,然后再从目标牛求一次单源最短路径得到。

  得到这两个最短路径之后,取它们的和的最大者即可。

  转置:假如起点是1,从1到2是2km,从2到1是3km,回来的时候是从1到2是2km,这个不用转置。去的时候从2到1是3km,转置后从1到2是3km,从2到1是2km,我们要的是从1到2这一个。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAX 0x3f3f3f3f
using namespace std;
int map[][];//矩阵存储信息
int n,m,s;
int come[],dis[];//come表示回来的最短路,dis表示回来的最短路
int logo[];//标记数组
void dijkstra()
{
int min,k;
memset(logo,,sizeof(logo));
for(int i=; i<=n; ++i)
dis[i]=map[s][i];
dis[s]=;
logo[s]=;
for(int i=; i<=n; ++i)
{
min=MAX;
for(int j=; j<=n; ++j)
{
if(!logo[j]&&dis[j]<min)
{
min=dis[j];
k=j;
}
}
logo[k]=;
for(int j=; j<=n; ++j)
if(!logo[j]&&dis[j]>dis[k]+map[k][j])
dis[j]=dis[k]+map[k][j];
}
}
int main()
{
scanf("%d%d%d",&n,&m,&s);
int sum=;
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
map[i][j]=MAX;
for(int i=; i<m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(c<map[a][b])
map[a][b]=c;
}
dijkstra();
for(int i=; i<=n; i++)
come[i]=dis[i];//计算回来的最短路,然后用come记录下俩
for(int i=; i<=n; i++)//将矩阵转置,表示从终点回来
for(int j=i+; j<=n; j++)
{
int c;
c=map[j][i];
map[j][i]=map[i][j];
map[i][j]=c;
}
dijkstra();
for(int i=; i<=n; i++)
{
if(sum<come[i]+dis[i]&&i!=s)
sum=come[i]+dis[i];//计算最大值
}
printf("%d\n",sum);
return ;
}

POJ3268 Silver Cow Party Dijkstra最短路的更多相关文章

  1. POJ3268 Silver Cow Party(dijkstra+矩阵转置)

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

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

  3. poj3268 Silver Cow Party(最短路)

    非常感谢kuangbin专题啊,这道题一开始模拟邻接表做的,反向边不好处理,邻接矩阵的话舒服多了. 题意:给n头牛和m条有向边,每头牛1~n编号,求所有牛中到x编号去的最短路+回来的最短路的最大值. ...

  4. POJ3268 Silver Cow Party —— 最短路

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

  5. 【POJ - 3268 】Silver Cow Party (最短路 Dijkstra算法)

    Silver Cow Party Descriptions 给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x, ...

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

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

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

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

  8. POJ_3268 Silver Cow Party 【最短路】

    一.题面 POJ3268 二.分析 该题的意思就是给定了一个由每个节点代表农场的有向图,选定一个农场X办party,其余农场的都要去,每个农场的cow都走最短路,走的时间最久的cow耗时多少. 了解题 ...

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

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

随机推荐

  1. 关于java反射获取泛型

    public class Test<T> { private final TypeToken<T> typeToken = new TypeToken<T>(get ...

  2. 关于MATLAB处理大数据坐标文件

    原先有3000条测试数据,MATLAB表现出来强大的数据处理能力,十几秒就可以把数据分类.分装并储存,这次共有10万条坐标数据,MATLAB明显后劲不足,显示内存不足 自我认识:以前MATLAB数据处 ...

  3. php5.6在yum下安装gd库

    yum install php-gd --enablerepo=remi,remi-php56 php.ini配置文件中增加 extension=gd.so 重启web服务器即可

  4. Linux操作数据库基本

    连接数据库MySQL中每个命令后都要以分号;结尾1: mysql -h 192.168.10.250 -u root -p2:Enter password //要求你输入密码cug313@com3:s ...

  5. Python3.5学习笔记-列表、元组、字典

    Python中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建.在Python中,变量就是变量,它没有类型,我们所说的"类型"是变量所指的内存中对象的类型 ...

  6. Js作用域&作用域链

    js构建类 一 构建类的原则 构造函数 等于 原型的constructor //构造函数 function Hero(name,skill){ this.name = name; this.skill ...

  7. 15个必须知道的 Chrome 开发技巧

    在 Web 开发者中,Chrome 是使用最广泛的浏览器.六周一次的发布周期和一套强大的不断扩大开发功能,使其成为了web开发者必备的工具.你可能已经熟悉了它的部分功能,如使用 console 和 d ...

  8. 不借助第三方网站四步实现手机网站转安卓APP

    今天本来是帮朋友查看是否在APP里可以点外链的一个测试,做着做来感觉了,就把这个测试优化了一下.好了我们来进入正题. 工具:Android Studio 第一步:新建项目 第二步:拖入控件(WebVi ...

  9. React Image加载图片过大导致ListView滑动卡顿

    今天莫名的发现ListView加载Item很卡,一顿一顿的... ListView Item 中只加载一张图片,小编从百度爸爸上随便复制的链接,这张图片很大,以致埋下如此大坑... Image的Sty ...

  10. Java分形

    目前笔者接触过的分形主要有一下几种: 1.类似Clifford的分形.这种分形的特点是:分形的初始坐标为(0,0),通过初始坐标经过大量的迭代,得到一系列的点,根据得到的点来绘制分形曲线.这类分形的参 ...