(poj)3268 Silver Cow Party 最短路
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 最短路的更多相关文章
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13611 Accepted: 6138 ...
- 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 ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- 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 ...
- POJ 3268 Silver Cow Party 单向最短路
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22864 Accepted: 1044 ...
随机推荐
- (10.09作业)学生选课数据库SQL语句练习题
- NSTimer运行机制和线程问题
A.首先要理解NSTimer运行机制和Runloop之间的关系: 1.IOS的Run Loops机制 Run Loops是线程的基础部份,任何线程,包括主结程,都包含了一个run loop对象,Coc ...
- NSThead
每个iOS应用程序都有个专门用来更新显示UI界面.处理用户的触摸事件的主线程,因此不能将其他太耗时的操作放在主线程中执行,不然会造成主线程堵塞(出现卡机现象),带来极坏的用户体验.一般的解决方案就是将 ...
- Spring ApplicationContextAware获取上下文
一.ApplicationContextAware 用处 Spring 提供了ApplicationContextAware类,通过它可以获取所有bean上下文. 二.怎么用? ①.定义一个工具类,去 ...
- UIDatePicker 之显示中文 年月日
picker_start=[[UIDatePicker alloc]initWithFrame:CGRectMake(centerView.frame.size.width/-,centerView. ...
- 成员方法与const之间的关系
const可以放在成员方法的三个地方,前.中.后. 首先考虑在中间: 1.const修饰形参,表示形参是否为const 2.如果const修饰引用(指针指向的对象),可以进行过载,如果不是修饰引用(指 ...
- 飘逸的python - 理解打开文件的模式
当我们用open()函数去打开文件的时候,有好几种打开的模式. 'r'->只读 'w'->只写,文件已存在则清空,不存在则创建. 'a'->追加,写到文件末尾 'b'->二 ...
- Swipe2.1更新——移动Web内容滑块
Swipe JS 是一个轻量级(3.7 kb) mobile slider,支持 1:1 触摸移动(基于精确的触摸位置的内容滑动). 但是我使用一段时间后发现两个bug,所以在官方2.0(官网http ...
- CircleProgressBar
http://www.eoeandroid.com/thread-333984-1-1.html CircleProgressBar.rar
- android使用webview加载flash文件
android 字段webview几乎实现了浏览器的全部功能,最近在使用webview加载不固定格式的文章,文章中有一部分嵌入了flash,下面就是webview可以进行视频需要进行的设置,代码如下: ...