Description

One cow from each of N farms ( ≤ N ≤ ) conveniently numbered ..N is going to attend the big cow party to be held at farm #X ( ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirectional (one-way roads connects pairs of farms; road i requires Ti ( ≤ Ti ≤ ) 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 : Three space-separated integers, respectively: N, M, and X
Lines ..M+: Line i+ 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 : One integer: the maximum of time any one cow must walk.
Sample Input
Sample Output

题目网址:http://poj.org/problem?id=3268

题意:有N只牛,编号从1到N他们从自己的地方到X去开会然后再回来,他们都选择最短的路径,问从去到回来,每只牛走的最远距离是多少?

方法:先求从X到各个点的最短路,然后把路径交换一下,再求一次从x到各点的最短路

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
#define N 1010
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a));
vector<vector<int> >Q;
struct node
{
int u,l,next;
}s[];
int a[N],b[N],k,n;
int vis[N],used[N],dis1[N],dis[N];
void add(int e,int f,int l)
{
s[k].u=f;
s[k].l=l;
s[k].next=a[e];
a[e]=k++;
s[k].u=e;
s[k].l=l;
s[k].next=b[f];
b[f]=k++;
}
void spfa1(int x)
{
met(vis,);met(used,);
for(int i=;i<=n;i++)
dis1[i]=INF;
queue<int>q;
int p=x,v;
q.push(p);
vis[x]=;
dis1[x]=;
used[x]=;
while(q.size())
{
p=q.front();
vis[p]=;
q.pop();
for(int i=a[p];i!=-;i=s[i].next)
{
v=s[i].u;
if(dis1[v]>dis1[p]+s[i].l)
{
dis1[v]=dis1[p]+s[i].l;
q.push(v);
vis[v]=;
} } }
}
void spfa2(int x)
{
met(vis,);met(used,);
for(int i=;i<=n;i++)
dis[i]=INF;
queue<int>q;
int p=x,v;
q.push(p);
vis[x]=;
dis[x]=;
used[x]=;
while(q.size())
{
p=q.front();
vis[p]=;
q.pop();
for(int i=b[p];i!=-;i=s[i].next)
{
v=s[i].u;
if(dis[v]>dis[p]+s[i].l)
{
dis[v]=dis[p]+s[i].l;
q.push(v);
vis[v]=;
} } }
}
int main()
{
int m,x,e,f,l;
while(scanf("%d %d %d",&n,&m,&x)!=EOF)
{
k=;
met(a,-);met(b,-);
for(int i=;i<m;i++)
{
scanf("%d %d %d",&e,&f,&l);
add(e,f,l);
}
//int ans=spfa1();
int ans=;
spfa1(x);
spfa2(x);
for(int i=;i<=n;i++)
{
ans=max(ans,dis[i]+dis1[i]);
} printf("%d\n",ans);
}
return ;
}

(poj)3268 Silver Cow Party 最短路的更多相关文章

  1. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  2. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  3. poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13611   Accepted: 6138 ...

  4. poj 3268 Silver Cow Party(最短路dijkstra)

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

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

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

  6. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  7. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  8. poj 3268 Silver Cow Party(最短路)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17017   Accepted: 7767 ...

  9. POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路

    Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...

  10. POJ 3268 Silver Cow Party 单向最短路

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22864   Accepted: 1044 ...

随机推荐

  1. 手把手教你入门mac idea

    一.前沿 去年入职后, 公司有很多人使用的是idea , 而不是eclipse. 之前就想转向idea, 但一直没转过来~~原因是团队的人提倡用开源eclipse.现在下定决心转向idea. 虽然, ...

  2. 在WWDC 2014上,没提到的iOS 8 八大新特性

    今天凌晨1点,36氪如约为大家研磨出WWDC 2014全程 "贴身直播"(我不得不佩服牺牲个人时间,熬夜为大家奉上好文的5位氪星人:JasonZheng.WANGJINGYU.pa ...

  3. 从零开始学android开发-Json转换利器Gson之实例

    Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. jar和源码下载地址: h ...

  4. sphinx中过滤的简单介绍

    1.过滤字符串为空的一些记录的话,可以在sql_query中直接加上where来限制,如: sql_query  = \  SELECT a.location_id as id,a.location_ ...

  5. BugZilla的安装过程简明教程

    Bugzilla+Mysql+iis+perl模块+ ActivePerl 安装过程 (原:http://www.cnblogs.com/Warmsunshine/archive/2012/03/25 ...

  6. OC中-方法到底是如何使用的?

    方法:方法是Objective-C独有的一种结构,只能在Objective-C中声明.定义和使用,C语言不能声明.定义和使用. 1.类方法以+号开头,对象方法以-号开头+ (void) init;  ...

  7. ios代理设计模式

    代理设计模式的作用:     1.A对象监听B对象的一些行为,A成为B的代理     2.B对象想告诉A对象一些事情,A成为B的代理   代理设计模式的总结:     如果你想监听别人的一些行为,那么 ...

  8. CentOS内核升级

    CentOS 6.5自动内核升级的主要步骤: 1)内核文件下载到/usr/src/kernel/目录下: 如:2.6.32-431.5.1.el6.x86_64编译后差不多有48M大小: 2)生成的引 ...

  9. 关于Eclipse中的egit的常规使用和模板

    修改bug的模板 要提交的comment信息.TDI-31426 fix bug: if get studio license use : Import License :after get lice ...

  10. ADO和ADO.NET有什么不同?

    1.一些ADO中常见的类型比如RecordSet在ADO.NET中已经没有了,而且在ADO.NET中也新增了许多在传统ADO中找不到的直接对应的新类型(如数据适配器): 2.传统的ADO主要针对紧密连 ...