题目描述

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?

寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100)。

每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度。

输入输出格式

输入格式:

第一行三个整数N,M, X;

第二行到第M+1行:每行有三个整数Ai,Bi, Ti ,表示有一条从Ai农场到Bi农场的道路,长度为Ti。

输出格式:

一个整数,表示最长的最短路得长度。

输入输出样例

输入样例#1:

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

输出样例#1:

10两次Dijkstra屠龙宝刀点击就送
#include <algorithm>
#include <cstring>
#include <cstdio>#define over() return 0;
#define inf 0x7fffffff
using namespace std;bool vis[1001];
int Answer=-0x7fffffff,n,m,i,j,dis1[1001],dis2[1001],atlas[1001][1001];
int min(int a,int b)
{return a>b?b:a;
}
void Dijkstra1(int start)
{for(i=1;i<=n;++i){dis1[i]=atlas[start][i];vis[i]=0;}vis[start]=1;dis1[start]=0;for(i=1;i<n;++i){int minx=inf,to;for(j=1;j<=n;++j){if(!vis[j]&&minx>dis1[j]){minx=dis1[j];to=j;}}vis[to]=1;for(j=1;j<=n;++j){if(dis1[j]>dis1[to]+atlas[to][j])dis1[j]=dis1[to]+atlas[to][j];}}
}void Dijkstra2(int start)
{for(i=1;i<=n;++i){dis2[i]=atlas[start][i];vis[i]=0;}vis[start]=1;dis2[start]=0;for(i=1;i<n;++i){int minx=inf,to;for(j=1;j<=n;++j){if(!vis[j]&&minx>dis2[j]){minx=dis2[j];to=j;}}vis[to]=1;for(j=1;j<=n;++j){if(dis2[j]>dis2[to]+atlas[to][j])dis2[j]=dis2[to]+atlas[to][j];}}
}
int main()
{int x;scanf("%d%d%d",&n,&m,&x);int u[100001],v[100001],l[100001];memset(atlas,1,sizeof(atlas));for(i=1;i<=m;++i){scanf("%d%d%d",&u[i],&v[i],&l[i]);atlas[u[i]][v[i]]=min(atlas[u[i]][v[i]],l[i]);}Dijkstra1(x);memset(atlas,1,sizeof(atlas));for(i=1;i<=m;++i)atlas[v[i]][u[i]]=min(atlas[v[i]][u[i]],l[i]);Dijkstra2(x);for(i=2;i<=n;++i)Answer=Answer>dis1[i]+dis2[i]?Answer:dis1[i]+dis2[i];printf("%d",Answer);over()
}

转载于:https://www.cnblogs.com/ruojisun/p/6445726.html

洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party相关推荐

  1. 洛谷P1821 [USACO07FEB]银牛派对Silver Cow Party

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

  2. 【洛谷 1821】银牛派对Silver Cow Party

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

  3. [USACO07FEB]银牛派对Silver Cow Party

    题目简叙: 寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100). 每头牛参加 ...

  4. [USACO07FEB]银牛派对Silver Cow Party---最短路模板题

    银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...

  5. [Luogu1821][USACO07FEB]银牛派对Silver Cow Party

    由题意可知,我们需要求的是很多个点到同一个店的最短距离,然后再求同一个点到很多个点的最短距离. 对于后者我们很好解决,就是很经典的单源最短路径,跑一边dijkstra或者SPFA即可. 然而对于前者, ...

  6. 信息学奥赛一本通 1343:【例4-2】牛的旅行 | 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours

    [题目链接] ybt 1343:[例4-2]牛的旅行 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours [题目考点] 1. 图论 最短路径 Floyd算法 Floyd算法时间复杂度 ...

  7. 洛谷·bzoj·伟大的奶牛聚集Great Cow Gather

    初见安~~这里是传送门:洛谷P2986 & bzoj P1827 题目描述 Bessie is planning the annual Great Cow Gathering for cows ...

  8. 洛谷P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  9. 洛谷 P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…

    P1569 [USACO11FEB]属牛的抗议Generic Cow Prote- 题目描述 Farmer John's N (1 <= N <= 100,000) cows are li ...

最新文章

  1. Struts2--ActionContext及CleanUP Filter
  2. MySQL数据库-错误1166 - Incorrect column name 'xxx' 的解决方法
  3. ns 25的L2TP模式×××配置
  4. php编写一个学生类_Python零基础入门之编写测试实例
  5. Flex CursorManager
  6. 在线编写php文件,php单文件版在线代码编辑器_php实例
  7. 【CodeForces - 195A】Let's Watch Football (追及问题,模拟)
  8. how many fibs java_How many Fibs?(java)
  9. 【文献阅读】Fashion-MNIST: a Novel Image Dataset for Benchmarking Machine Learning Algorithms
  10. OPNET网络仿真分析-1.2、OPNET安装教程
  11. 我和面试官的博弈:Redis 篇
  12. 服务器(Windows镜像)自建git服务器超详细教程
  13. xml文件怎么转换成wps_word文档怎样转换成xml 怎么将XML文档转成WORD文档
  14. 实现Ogre的脚本分离 - 天龙八部的源码分析(一)
  15. linux开机自动root,linux怎样设置root自动登录
  16. 2022-2027年中国喷涂机器人行业市场调研及未来发展趋势预测报告
  17. WIN10DOS命令
  18. R语言read.csv()读入行不规则数据
  19. 锐速ServerCheck序列号生成原理
  20. 自动关闭Office2010 OSPPSVC.EXE

热门文章

  1. Android平台类加载流程源码分析
  2. java字数统计_java统计字数
  3. 风格迁移应用_[风格迁移][超分][ECCV2016]Perceptual Losses for Real...
  4. jTable保存到mysql_怎么把从数据库里的数据输到JTABLE里面
  5. [CODEVS 1285] 宠物收养所
  6. 2019年, SGG论文汇总
  7. 集合 Subset Sums
  8. AtCoder AGC030F Permutation and Minimum (DP、计数)
  9. cuda nvcc版本不一致_入坑第一步:Win10安装cuda+cuDNN+TensorFlow-GPU走过的那些路
  10. python中 s是什么意思_python – “S”在同情中意味着什么