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. (Relax DFS专题1.2)POJ 2386 Lake Counting(使用DFS来计算有多少坨东西是连通的)

    题目大意:有N*M的矩阵稻田,'W'表示有积水的地方, '.'表示是干旱的地方,问稻田内一共有多少块积水,根据样例很容易得出,积水是8个方向任一方向相连即可. 题目大意:有N*M的矩阵稻田,'W'表示 ...

  2. win2008下c#调用directshow问题

    打开摄像头时报错 网上查 说缺少qedit.dll,下载后注册也不行. 最后安装暴风影音,测试ok

  3. 淘宝IP地址查询

    官方网址:http://ip.taobao.com/index.php 相关文章: http://www.cnblogs.com/zetee/p/3482085.html http://www.cnb ...

  4. 放弃移动版Flash而非AIR

    之前看到标题为"Adobe放弃移动版flash"的新闻,我很震惊,为何Adobe会放弃这么一个大市场呢? 这样无疑打击原来在flash的开发上的应用,我想很多人和我想的一样,fla ...

  5. 【转】使用junit4进行单元测试(高级篇)

    转自:http://blog.csdn.net/andycpp/article/details/1329218 通过前 2 篇文章,您一定对 JUnit 有了一个基本的了解,下面我们来探讨一下JUni ...

  6. mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication的解决方法

    直接命令行操作没有问题,但是PHP连接就会报上面的错误. SET old_passwords =0; USE mysql; UPDATE user SET password =PASSWORD('yo ...

  7. CPU相关信息

    unit untCpuInfo;interface{ 获取 CPU 制造商 }function GetCpuFactory: String;{ 获取 CPU 家族系统 }function GetCpu ...

  8. Python 魔术方法指南

    入门 构造和初始化 构造定制类 用于比较的魔术方法 用于数值处理的魔术方法 表现你的类 控制属性访问 创建定制序列 反射 可以调用的对象 会话管理器 创建描述器对象 持久化对象 总结 附录 介绍 此教 ...

  9. careercup-C和C++ 13.8

    13.8 编写一个智能指针类.智能指针是一种数据类型,一般用模板实现,模拟指针行为的同时还提供自动垃圾回收机制.它会自动记录SmartPointer<T*>对象的引用计数,一旦T类型对象的 ...

  10. C++ stringstream介绍,使用方法与例子

    From: http://www.usidcbbs.com/read-htm-tid-1898.html C++引入了ostringstream.istringstream.stringstream这 ...