题目链接:

https://vjudge.net/problem/POJ-3268

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.
 /*
关键是反向存储,求最短路的思维转换
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
void Dijkstra(int s,int e[][]);
#define inf 99999999
int dis[],book[],e[][],f[][],d[],n,m;
int main()
{
int i,j,k,x,a,b,c,maxn,temp;
while(scanf("%d%d%d",&n,&m,&x)!=EOF)
{
memset(d,,sizeof(d));
for(i=;i<=n;i++)
for(j=;j<=n;j++)
if(i==j)
{
e[i][j]=;
f[i][j]=;
}
else
{
e[i][j]=inf;
f[i][j]=inf;
} for(i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(c<e[a][b])
{
e[a][b]=c;
f[b][a]=c;
}
}
Dijkstra(x,e);//回
Dijkstra(x,f);//去
maxn=-;
for(i=;i<=n;i++)
{
if(d[i]>maxn)
maxn=d[i];
}
printf("%d\n",maxn);
}
return ;
}
void Dijkstra(int s,int e[][])
{
int i,j,k,min,u;
for(i=;i<=n;i++)
dis[i]=e[s][i];
memset(book,,sizeof(book));
book[s]=;
for(k=;k<n;k++)
{
min=inf;
for(i=;i<=n;i++)
if(book[i]==&&dis[i]<min)
{
min=dis[i];
u=i;
}
book[u]=;
for(i=;i<=n;i++)
if(book[i]==&&dis[i]>dis[u]+e[u][i])
dis[i]=dis[u]+e[u][i];
}
for(i=;i<=n;i++)
d[i]+=dis[i];
}

POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)的更多相关文章

  1. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  2. POJ 3268 Silver Cow Party (Dijkstra + 优先队列)

    题意:由n个牧场,编号1到n.每个牧场有一头牛.现在在牧场x举办party,每头牛都去参加,然后再回到自己的牧场.牧场之间会有一些单向的路.每头牛都会让自己往返的路程最短.问所有牛当中最长的往返路程是 ...

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

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

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

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

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

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

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

    描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...

  7. POJ 3268 Silver Cow Party 最短路

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

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

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

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

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

随机推荐

  1. [Algorithm]Algorithm章1 排序算法

    1.冒泡排序-相邻交换 (1)算法描述 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也 ...

  2. gitlab 10汉化

    记得备份 先检查一下版本,好下载对应的汉化包 cat /opt/gitlab/embedded/service/gitlab-rails/VERSION 1)然后下载10.0.x.diff 文件到服务 ...

  3. 1.6Eigen中系数运算Reductions, visitors and broadcasting

    Eigen::Matrix2d mat; mat<<,, ,; cout<<"矩阵所有系数之和:"<<mat.sum();//1+2+3+4=1 ...

  4. MySQL安装及后续配置

    rpm -qa | grep mysql  检查已安装的mysql版本 rpm -e --nodeps mysql-libs-5.1.71 卸载 tar -zxvf MySQL.tar.gz 解压 安 ...

  5. sock5客户端解密数据流

    一.安装 略 二.配置 vi /etc/shadowsocks.json { "server":"x.x.x.x", , , "password&qu ...

  6. 微信js sdk的使用初步理解

    第一步引入js文件 在需要调用JS接口的页面引入如下JS文件,(支持https):http://res.wx.qq.com/open/js/jweixin-1.2.0.js 备注:支持使用 AMD/C ...

  7. python open()函数的模式选择

    python open()函数打开文件的模式详解 使用python处理文件时,避免不了要用到open()函数.我们今天主要讨论mode参数的区分. fd = open('文件名(路径)’, mode= ...

  8. java 基础--理论知识

    变量分:局部变量全局变量-----------------------------------------------------变量三大特性[标识符,初始值,作用域]定义变量:语法:[访问修饰符] ...

  9. 使用Jupyter Notebook编写技术文档

    1.jupyter Notebook的组成 这里它的组件及其工程构成,帮助大家更好的用好jupyter Notebook 组件 Jupyter Notebook结合了三个组件: 笔记本Web应用程序: ...

  10. 一行代码实现自定义转场动画--iOS自定义转场动画集

    WXSTransition 这款非常不错,力推 这是作者源码简书地址: http://www.jianshu.com/p/fd3154946919 这是作者源码github地址 https://git ...