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. Oracle sqlldr导入之“MAXIMUM ERROR COUNT EXCEEDED”

    昨天看到一个同事在通过PL/SQL Developer工具把文本数据往oracle表;有两个文本:一个有30万条记录:一个7万多条记录.在导入到过程中:出现错误记录还需要点击确认.不过使用黑科技(屏幕 ...

  2. 【安全开发】java安全编码规范

    申明:本文非笔者原创,原文转载自:https://github.com/SecurityPaper/SecurityPaper-web/blob/master/_posts/2.SDL%E8%A7%8 ...

  3. MQTT服务器搭建--Mosquitto用户名密码配置

    Mosquitto用户认证配置 前言:基于Mosquitto服务器已经搭建成功,大部分都是采用默认的是允许匿名用户登录模式,正式上线的系统需要进行用户认证. 1.用户参数说明 Mosquitto服务器 ...

  4. RN(八)——react-native-image-viewer & react-native-swiper

    以项目(业务GO)为例: react-native-swiper 轮播(用在首页的图集轮播) https://github.com/leecade/react-native-swiper react- ...

  5. C++ template —— 类型区分(十一)

    前面的博文介绍了模板的基础,深入模板特性,模板和设计的一些内容.从这篇开始,我们介绍一些高级模板设计,开发某些相对较小.并且互相独立的功能,而且对于这些简单功能而言,模板是最好的实现方法:(1)一个用 ...

  6. 使用java中的session来记录访问次数

    <%@ page import="java.net.CookieHandler" %><%-- Created by IntelliJ IDEA. User: D ...

  7. (原)强类型dataset(类型化dataset)中动态修改查询条件(不确定参数查询)

    原创博客,转载请注明:http://www.cnblogs.com/albert1017/p/3361932.html 查询时有多个参数,参数个数由客户输入决定,不能确定有多少个参数,按一般的方法每种 ...

  8. 怎么安装WinXP和Win7双系统

    本文介绍一下如何安装Windows XP和Windows 7双系统以及启动菜单问题的常用修复方法,推荐安装win7和xp双系统时最好采用从低版本到高版本的顺序进行,一般是XP下安装win7,另本文已假 ...

  9. 游戏服务器学习笔记 5———— twisted Perspective Broker 透明代理

    实际上这章压根不需要我来说,twisted官网的Doc里面有专门介绍的章节.写的非常详细. http://twistedmatrix.com/documents/current/core/howto/ ...

  10. 【大数据系列】win10不借助Cygwin安装hadoop2.8

    一.下载安装包 解压安装包并创建data,name,tmp文件夹 二.修改配置文件 1.core-site.xml <?xml version="1.0" encoding= ...