Silver Cow Party

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 Titime 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.
 
题意: 有n个农场,将在农场x举行party,农场a->b用时c;有向图;求所有农场中的牛去到农场x再回去所需的最长时间;可以两次运用Dij算法,用dist[i]表示起点到i的最短时间
 
 
Dij1()中是maps[index][j]而Dij2()中是maps[j][index];
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define INF 0xfffffff
#define N 1100
using namespace std;
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
int x,n,m,dist1[N],dist2[N],vis[N];
int maps[N][N]; void Dij1()
{
int i,j; vis[x]=; dist1[x]=; for(i=; i <= n; i++)
{
int Min=INF,index=-; for(j = ; j <= n; j++)
{
if(vis[j] == && Min > dist1[j])
{
Min = dist1[j];
index = j;
}
}
if(index==-)break;
vis[index]=; for(j = ; j <= n; j++)
dist1[j] = min(dist1[j],dist1[index] + maps[index][j]);
}
}
void Dij2()
{
int i,j; vis[x]=; dist2[x]=; for(i=; i <= n; i++)
{
int Min=INF,index=-; for(j = ; j <= n; j++)
{
if(vis[j] == && Min > dist2[j])
{
Min = dist2[j];
index = j;
}
}
if(index==-)break;
vis[index]=; for(j = ; j <= n; j++)
dist2[j] = min(dist2[j],dist2[index] + maps[j][index]);
}
} int main()
{
int i,a,b,c;
while(scanf("%d%d%d",&n,&m,&x)!=EOF)
{
for(i=;i<=n;i++)
{
dist1[i]=dist2[i]=INF;
for(int j=;j<=n;j++)
maps[i][j]=INF;
}
while(m--)
{
scanf("%d%d%d", &a, &b, &c); maps[a][b] = c;
} memset(vis,,sizeof(vis));
Dij1();//去 memset(vis,,sizeof(vis));
Dij2();//回来; int ans=; for(i=;i<=n;i++)
{
ans=max(ans,dist1[i]+dist2[i]);
}
printf("%d\n",ans);
}
return ;
}
 
 

Silver Cow Party---poj3268(最短路,迪杰斯特拉)的更多相关文章

  1. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. 最短路——迪杰斯特拉算法 HDU_3790

    初识最短路,今天只弄了一个迪杰斯特拉算法,而且还没弄成熟,只会最基本的O(n^2),想弄个优先队列都发现尼玛被坑爆了,那个不应该用迪杰斯特拉算法写 表示还是不会优化版的迪杰斯特拉算法,(使用优先队列) ...

  3. POJ 1062 昂贵的聘礼 (最短路 迪杰斯特拉 )

    题目链接 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请 ...

  4. HUD 2544 最短路 迪杰斯特拉算法

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. hdu 1874 畅通工程续(迪杰斯特拉优先队列,floyd,spfa)

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  6. Poj 3268 Silver cow party 迪杰斯特拉+反向矩阵

    Silver cow party 迪杰斯特拉+反向 题意 有n个农场,编号1到n,每个农场都有一头牛.他们想要举行一个party,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...

  7. (最短路)Silver Cow Party --POJ--3268

    题目链接: http://poj.org/problem?id=3268 题意: 先求出所有牛到x的最短路,再求出x到所有牛的最短路,两者相加取最大值(单向图)(可以用迪杰斯特拉,SPFA) 迪杰斯特 ...

  8. pat1003 迪杰斯特拉法和dfs求最短路

    本题的背景是求定点和定点之间的最短路问题(所有的最短路 不是一个解  是全部解,方法手段来自数据结构课程中的迪杰斯特拉算法和dfs(深度优先遍历). 分别用两种方法编程如下代码 dfs #includ ...

  9. HDU 2544最短路 (迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Me ...

  10. hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

随机推荐

  1. x264编码的图像出现乱码的问题

    将YUV进行x264编码的时候,建议将 i_threads 参数设置成 X264_SYNC_LOOKAHEAD_AUTO//* 取空缓冲区继续使用不死锁的保证. 否则有可能编码出来的数据会出现IDR_ ...

  2. Selenium 选项卡管理

    什么是选项卡: from selenium import webdriver browser = webdriver.Chrome() browser.get("http://www.bai ...

  3. DRBD架构详解(原创)

    DRBD概述Distributed Replicated Block Device(DRBD)是一种基于软件的,无共享,复制的存储解决方案,在服务器之间的对块设备(硬盘,分区,逻辑卷等)进行镜像.DR ...

  4. ip防刷脚本

    #!/bin/sh #防刷脚本 #env ACCESS_PATH=/home/wwwlogs ACCESS_LOG=y.log IPTABLES_TOP_LOG=iptables_top.log DR ...

  5. git切换分支(自记)

    git fetch git checkout feature/A4-page

  6. iOS - 栈顶VC控制器的获取

    iOS 获取当前顶层的ViewController #pragma mark - topVC - (UIViewController *)theTopviewControler { UIViewCon ...

  7. Bootstrap学习总结笔记(24)-- 基于BootstrapValidator的Form表单验证

    Form表单进行数据验证是十分必要的,我们可以自己写JS脚本或者使用JQuery Validate 插件来实现.对于Bootstrap而言,利用BootstrapValidator来做Form表单验证 ...

  8. PHP 中文字符串截取

    $str = "abcdef啊啊吧啊"; function my_sub($str, $st ,$len){ $ret = ""; for( $st; $len ...

  9. WEB服务器控件对应生成的HTML标签 及最常应用事例

    首先得了解WEB服务器控件对应生成的HTML标签 label----------<span/>button---------<input type="submit" ...

  10. java(7)LinkedList源码

    系统环境 JDK1.7 LinkedList的基本结构 :在JDK1.6中LinkedList是双向引用的环形结构,JDK1.6中是双向引用的线性结构 提醒:看链表代码时最好用笔画下链表结构 有助于理 ...