Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19226   Accepted: 8775

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 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<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int map[1001][1001],w[1001][1001];
int dis[1001],Team[5000];
bool exist[1001];
int n,m,t;        // exist 为 true 则不可进队
void SPFA(int s)
{
int head=0,tail=1,k=0;
Team[head]=s,exist[s]=true;dis[s]=0;
while(head<tail)
{
k=Team[head];
exist[k]=false;
for(int i=1;i<=n;i++)
{
if((map[k][i]>0)&&(dis[i]>dis[k]+map[k][i]))
{
dis[i]=map[k][i]+dis[k];
if(exist[i]==false)
{
Team[tail++]=i;
exist[i]=true;
}
}
}
head++;
}
}
int main()
{
cin>>n>>m>>t;
memset(map,0x3f,sizeof map );
memset(w,0x3f,sizeof w );
memset(dis,0x3f,sizeof dis );
for(int i=1;i<=m;i++)
{
int x,y,z;
cin>>x>>y>>z;
map[x][y]=z;
w[x][y]=z;
}
SPFA(t);// 从起点开始到其他每个点找一遍最短路
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(w[i][j]>w[i][k]+w[k][j])
w[i][j]=w[i][k]+w[k][j];
}
}
}
int max;
for(int i=1;i<=n;i++)
{
if((i==1)||(dis[i]+w[i][t]>max))
max=dis[i]+w[i][t];
}
printf("%d",max);
return 0;
}

思路:一遍SPFA求出起点到其他所有点的最短路,一遍Floyed求出所有点到起点的最短路,两者相加,和最大者即为anwser...

BZOJ 1631==USACO 2007== POJ 3268 Cow Party奶牛派对相关推荐

  1. USACO 2.3.2 Cow Pedigrees 奶牛家谱

    题目描述: 农民约翰准备购买一群新奶牛. 在这个新的奶牛群中, 每一个母亲奶牛都生两个小奶牛.这些奶牛间的关系可以用二叉树来表示.这些二叉树总共有N个节点(3 <= N < 200).这些 ...

  2. [Usaco2007 Feb]Cow Party 奶牛派对

    好了,好久没发题解,这次来一发SPFA,看题: Description 农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对.共有M(1≤M≤100000)条单向 路连 ...

  3. poj 3268 bzoj 1631: [Usaco2007 Feb]Cow Party(最短路)

    1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 855  Solved: 613 [Subm ...

  4. D - Silver Cow Party POJ - 3268

    D - Silver Cow Party POJ - 3268 dijkstra 是 O(n2),堆优化一下, O(nlogn) 对每个点跑一次 dj, 取 max(dis(x->i)+dis( ...

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

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

  6. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  7. POJ 3263-Tallest Cow

    Description FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow ...

  8. POJ 3268 Bookshelf 2 动态规划法题解

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  9. [USACO 2007 Jan S]Protecting the Flowers

    题目: [USACO 2007 Jan S]Protecting the Flowers ,哈哈,我们今天来看一道简单的贪心算法题嘛,这是选自USACO上的一道题,好了,我们一起来看看题意吧: 题目描 ...

最新文章

  1. pybind11介绍
  2. 每天5分钟玩转python3算法:二分查找法
  3. 【网络安全】Penelope:一款功能强大的Shell处理工具
  4. python爬虫-爬取今日头条(图集)
  5. java的import关键字的使用
  6. Spark Structured SQL : JDBC写入Oracle
  7. 如何用报表解决销售工作衡量问题
  8. 通用Makefile模板
  9. OpenDDS架构说明
  10. ORACLE语句大全
  11. [开源教程] 双路混控有刷电调 DIY教程【作者:我爱萝丽爱萝丽】(双路单向电调)
  12. 概率论与随机过程难题整理复习
  13. lisp坐标一键生成_如何利用lisp程序一次性提取CAD中点的坐标(不要点击每个点,太多了麻烦)...
  14. 常用字体对照表和常用命名
  15. 人类一败涂地显示启动服务器失败,人类一败涂地进不去解决方法-人类跌落梦境手游进不去方法详解...
  16. 快速弄懂iPhone X 设计尺寸和适配
  17. 头歌HTML实训笔录
  18. android 高光动画,记一个头像高光动画的CSS实现
  19. 脏读、重复读、幻读;
  20. 华为S系列交换机如何取消端口类型的设置

热门文章

  1. 互联网晚报 | 9月15日 星期三 | iPhone 13正式发布;天猫双11正式启动商家报名;华为推出鸿蒙矿山操作系统“矿鸿”...
  2. Android API 中文(13) —— ToggleButton
  3. 年薪16薪,工作福利靠砸钱 ?在京东做测试员原来这么爽
  4. R语言ggplot2包学习笔记
  5. 小内存海量数据,找中位数: 只有2G内存,在10G数据量怎样找到中位数?
  6. 开发板连续显示图片 | BAD APPLE 万耦中的二次元世界
  7. 到底还有没有月薪3万以下的程序员?程序员工资真的这么高?
  8. Linux系统接口ioc,Linux下SCSI API研究及应用
  9. 洛谷-3373 【模板】线段树 2
  10. OpenFOAM多孔介质模型_DarcyForchheimer