博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Silver Cow Party
阅读量:7021 次
发布时间:2019-06-28

本文共 2426 字,大约阅读时间需要 8 分钟。

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 ≤ 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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 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.

题解:还是正向建图和逆向建图。就是求往返路程中最大的一条。

djistra:

#include 
#include
#include
#include
using namespace std;const int INF= 0x3fffffff;int map[2][1003][1003];bool visited[1003];int d[1003];int ans[1003];int max(int a,int b){ return a > b ? a : b;}void prim(int n,int s,int flag){ memset(visited,false,sizeof(visited)); for(int i = 1;i <= n;i++) { d[i] = map[flag][s][i]; //cout<
<
< n;i++) { int min = INF; int k; for(int j = 1;j <= n;j++) { if(!visited[j] && min > d[j]) { min = d[j]; k = j; } } if(min == INF) { break; } visited[k] = true; for(int j = 1;j <= n;j++) { if(!visited[j] && d[j] > d[k] + map[flag][k][j]) { d[j] = d[k] + map[flag][k][j]; } } }}int main(){ int n,m,st; while(scanf("%d%d%d",&n,&m,&st) != EOF) { for(int i = 1;i <= n;i++) { for(int j = 1;j <= n;j++) { if(i == j) { map[0][i][j] = 0; map[1][i][j] = 0; } else { map[0][i][j] = INF; map[1][i][j] = INF; } } } int u,v,c; for(int i = 0;i < m;i++) { scanf("%d%d%d",&u,&v,&c); map[0][u][v] = c; map[1][v][u] = c; } prim(n,st,1); for(int i = 1;i <= n;i++) { ans[i] = d[i]; } prim(n,st,0); int res = 0; for(int i = 1;i <= n;i++) { ans[i] += d[i]; res = max(res,ans[i]); } printf("%d\n",res); } return 0;}

转载地址:http://robxl.baihongyu.com/

你可能感兴趣的文章
礼贺新年|宝瓷林·玉堂春·酒具
查看>>
spring cloud微服务主要组件作用和架构介绍
查看>>
(四十四)java版spring cloud+spring boot+redis多租户社交电子商务平台-security简单使用...
查看>>
(二十二)java版spring cloud+spring boot+redis多租户社交电子商务平台-Ribbon设计原理...
查看>>
原生骨架屏 - 2.0.0重磅推出
查看>>
Toolfk.com 程序员工具网上线
查看>>
《Dark Reader》为任意网站启用夜间模式
查看>>
JavaScript数据类型详解
查看>>
微服务架构实战篇(二):Spring boot2.0 + Swagger2 让你的API可视化
查看>>
网络 tcp 和 udp
查看>>
正则表达式
查看>>
人工智能强势来袭,我们普通人怎么做?清华教授给你支招!
查看>>
python爬虫爬取各大平台女主播图片
查看>>
企业级 SpringCloud 教程 (四) 断路器(Hystrix)
查看>>
阿里云MVP:开发者的超能力,用技术创造更好世界
查看>>
Spring Cloud Config-快速开始
查看>>
Web功能测试的4种类型
查看>>
HashMap的工作原理
查看>>
Linux下自动备份Oracle数据库
查看>>
[git] warning: LF will be replaced by CRLF
查看>>