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. JavaScript 基础——使用js的三种方式,js中的变量,js中的输出语句,js中的运算符;js中的分支结构

    JavaScript 1.是什么:基于浏览器 基于(面向)对象 事件驱动 脚本语言 2.作用:表单验证,减轻服务器压力 添加野面动画效果 动态更改页面内容 Ajax网络请求 () 3.组成部分:ECM ...

  2. Matlab: 路径的操作

    添加相对路径 在matlab中当代码很多时常常将结果存在不同的文件夹下面,常常使用相对路径对函数进行调用,但有时会存在问题.举个栗子: 代码结构如下: /codes/A/AA/code1.m /cod ...

  3. java怎么处理json数据

    json = new JSONObject(data); int which = json.optInt("which", -1); String label = json.opt ...

  4. H5学习第一周

    已经接触H5一个周了,经过学习,总算对H5有了一些了解和认知,下面就总结一下我对H5的认知和感悟. 首先接触的是H5的常用标签[meta],它有其以下常用属性 1.charset属性.单独使用,设置文 ...

  5. Java post提交表单限制

    According to Tomcat7's documentation, setting maxPostSize in Connector to a value less than or equal ...

  6. axis1.4开发webservice服务端(快速入门)-基于jdk1.4

    写在前面: 现在有很多开发webservice的方法以及框架,什么cxf等,但是这些在你编写服务类的时候都要用到注解这个功能.如果现在的jdk是1.4的,那么就不能使用注解这个功能了.所以这里可以用到 ...

  7. Struts2配置拦截器自定义栈时抛异常:Unable to load configuration. - interceptor-ref - file:/D:/tomcat_install/webapps/crm/WEB-INF/classes/struts.xml

    代码如下: <interceptors>  <!-- 注册自定义拦截器 -->   <interceptor name="LoginInterceptor&qu ...

  8. Android系统--输入系统(十四)Dispatcher线程情景分析_dispatch前处理

    Android系统--输入系统(十四)Dispatcher线程情景分析_dispatch前处理 1. 回顾 我们知道Android输入系统是Reader线程通过驱动程序得到上报的输入事件,还要经过处理 ...

  9. 第四章:Django 模型 —— 设计系统表

    1. Django框架提供了完善的模型(Model )层来创建和存储数据,每一个模型对应数据库中的唯一的一张表. 2. Django 模型基础知识: .每一本模型是一个Python类,继承了djang ...

  10. 使用mysqldump备份数据库

    #! /bin/shday_str=`date +%j`day=`date +%Y%m%d`days_str=`echo "$day_str % 60"|bc`cd /home/d ...