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. 限制input[type=number]的输入位数策略整理

    当我们使用类型number的input输入框的时候,我们可能需要限制输入的位数,这个时候通常会想到maxlength,但是maxlength是在number类型的时候是不支持的,下面是一些解决这种问题 ...

  2. smarty获取变量的两种方式

    从上一篇随笔中,我们知道smarty可以通过assign()的方法注册变量,从而在前段读取变量:我们也可以从配置文件中获取变量,来具体看一下: 1.在configs文件夹中建一个test.conf文件 ...

  3. Spring+SpringMVC+MyBatis深入学习及搭建(十五)——SpringMVC注解开发(基础篇)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7065294.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十四)--S ...

  4. Unity 游戏框架搭建 (二) 单例的模板

      上一篇文章中说到的manager of managers,其中每个manager都是单例的实现,当然也可以使用静态类实现,但是相比于静态类的实现,单例的实现更为通用,可以适用大多数情况. 如何设计 ...

  5. CSS3学习系列之布局样式(一)

    多栏布局 使用float属性或position属性的缺点: 使用float属性或position属性进行页面中的简单布局的例子. <!DOCTYPE html> <html lang ...

  6. peoplesoft function PSTREENODE 通过 deptid 获得部门树 全路径 code

    create or replace function getUnitFullcode(deptid in varchar) return varchar2 is r ); c int; n ); m ...

  7. SpringMvc多视图配置(jsp、velocity、freemarker) velocity在springmvc.xml配置VelocityViewResolver,VelocityConfigurer,FreeMarkerConfigurer,FreeMarkerViewResolver

    ?xml version="1.0"encoding="UTF-8"?> <beans xmlns="http://www.springf ...

  8. JavaWeb 后端 <一> 之 Tomcat服务器 - Http协议 学习笔记

    1.Web开发概述 1.1服务器上的资源分类: a.静态资源:指web页面中供人们浏览的数据始终是不变.html css js 图片 多媒体 b.动态资源:指web页面中供人们浏览的数据是由程序产生的 ...

  9. mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

    最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网: ...

  10. iOS多线程开发之NSOperation - 快上车,没时间解释了!

    一.什么是NSOperation? NSOperation是苹果提供的一套多线程解决方案.实际上NSOperation是基于GCD更高一层的封装,但是比GCD更加的面向对象.代码可读性更高.可控性更强 ...