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. Oracle- 表查询

    这些都比较基础,还是重温一下. 一.简单的查询语句1.查询表结构sql>desc dept; (使用PL/SQL Developer查询时,必需在命令窗口才能使用该语句) 2.查询所有列sql& ...

  2. cocos2d-x之蒙板,局部高亮可点,CCRenderTexture

    转自:http://www.2cto.com/kf/201207/144656.html 蒙板,局部高亮可点的用处大多是在新手引导的时候,引导玩家一步一步的走游戏的操作流程. 之前写了一个cocos2 ...

  3. cocos2d-x 动画加速与减速

    转自:http://novacreo.com/%E7%A8%8B%E5%BA%8F%E7%BB%84/cocos2d-x%E5%8A%A8%E7%94%BB%E5%8A%A0%E9%80%9F%E4% ...

  4. UIWebView 获取html标题

    使用uiwebview.delegate -(void)webViewDidFinishLoad:(UIWebView *)webView { m_labTitle.text=[m_webVie st ...

  5. SQL Server磁盘I/O性能分析

    SQL Server中的I/O操作类型: 1.对于内存中没有缓存的数据,第一次访问时需要将数据从所在的页面从数据文件中读取到内存中 2.在任何Insert/Update/Delete提交前,SQL S ...

  6. KeyTweak 键盘按键功能修改

    最近一致再用ThinkPad S3,悲剧的是上翻页和下翻页竟然和方向键在一起,经常按错光标不知道去哪里了. 实在忍受不了,竟然有这样的软件,哈哈. KeyTweak,用起来太方便了

  7. 设计模式 - 命令模式(command pattern) 多命令 具体解释

    命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...

  8. C# 之 遍历本地文件夹下的所有文件

    /// <summary> /// 遍历 rootdir目录下的所有文件 /// </summary> /// <param name="rootdir&quo ...

  9. oracle删除表语句

    删除表(记录和结构)的语名delete   ————   truncate      ———— drop DELETE (删除数据表里记录的语句) DELETE FROM表名 WHERE 条件; 注意 ...

  10. c++中静态成员变量 静态成员函数 全局变量与静态函数的关系 字符串中括号的匹配编程 (笔试经历)

    笔试经历 1 类中的静态变量不能通过构造函数参数列表来初始化,因为静态变量不属于哪个对象.同时静态变量在不初始化的情况下系统会自动为变量赋值,数值型赋值为零,字符型赋值为空. 非静态变量只有在定义时才 ...