题目链接:

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. java 大任务分解成小任务 fork/join

    https://blog.csdn.net/weixin_41404773/article/details/80733324目标求 0+1+2+3+4+5+....+1000 初始 start=0 , ...

  2. HTML中的table导出为Excel文件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. android 测试

    Android Monkey压力测试 monkey测试结果详细分析 Android自动化测试工具 UiAutomator使用详解 在Android Sudio中使用Uiautomator 六款Andr ...

  4. Beta冲刺 (3/7)

    Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 熟悉并编写小程序的自定义控件 编辑文 ...

  5. docker知识点杂记

    最近重新梳理了一下docker的一些基本知识,简单做了下总结.以后会逐步补充. 1.查看系统内核版本,需要3.10以上 $ uname -r 2.下载docker 说明:centos安装docker比 ...

  6. C#自定义异常 统一异常处理

    异常类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  7. ReactNative 深拷贝

    1: 导入 import _ from 'lodash' 2: _.cloneDeep(obj)

  8. accept:Invalid Argument

    错误 #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> int accept(int sockfd, ...

  9. grid布局学习二之子元素(项目)

    /* grid-column-start属性:左边框所在的垂直网格线 grid-column-end属性:右边框所在的垂直网格线 grid-row-start属性:上边框所在的水平网格线 grid-r ...

  10. 如何正确的使用Ubuntu以及安装常用的渗透工具集.

    文章来源i春秋 入坑Ubuntu半年多了  记得一开始学的时候基本一星期重装三四次=-= 尴尬了 觉得自己差不多可以的时候 就吧Windows10干掉了 c盘装Ubuntu 专心学习.   这里主要来 ...