(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 ...
随机推荐
- AdapterView及其子类之二:使用ListActivity及ArrayAdapter创建列表
见归档项目ListActivityDemo.zip. 基本步骤如下: 1.创建一个TextView,用于指定每一个ListView的格式 <?xml version="1.0" ...
- Pivot运算符用于在列和行之间
本文导读:T-SQL语句中,Pivot运算符用于在列和行之间对数据进行旋转或透视转换,PIVOT命令可以实现数据表的列转行,同时执行聚合运算,UNPIVOT则与其相反,实现数据的行转列. PIVOT通 ...
- iOS7 人机界面设计指南
iOS7 人机界面设计指南 苹果在WWDC 2013大会上发布了iOS 7,新系统一改5年来的拟物路线,在乔纳森•艾维的主导下,加入了更多的“扁平化”和“极简”现代设计元素. iOS7系统界面 ...
- hadoop的hdfs文件操作实现上传文件到hdfs
这篇文章主要介绍了使用hadoop的API对HDFS上的文件访问,其中包括上传文件到HDFS上.从HDFS上下载文件和删除HDFS上的文件,需要的朋友可以参考下hdfs文件操作操作示例,包括上传文件到 ...
- Nape的回调系统 nape.callbacks
在Nape中增加一个回调大致分为三步 1:定义一些标签,并根据需求为不同的Interactor打上不同的标签 2:定义一个监听器,这个监听器定义了哪些标签触发了哪种行为之后做何种回调 3:为Space ...
- SmoothProgressBar
https://github.com/castorflex/SmoothProgressBar
- Linux中的文件描述符与打开文件之间的关系------------每天进步一点点系列
http://blog.csdn.net/cywosp/article/details/38965239 1. 概述 在Linux系统中一切皆可以看成是文件,文件又可分为:普通文件.目录文件. ...
- linux-insides-cn-BOOK
https://www.gitbook.com/book/xinqiu/linux-insides-cn/details https://www.gitbook.com/book/looly/elas ...
- mysql datetime、date、time、timestamp区别
我们看看这几个数据库中(mysql.oracle和sqlserver)如何表示时间 mysql数据库:它们分别是 date.datetime.time.timestamp和year.date :“yy ...
- Creader VIII VS. Creader VII+
Launch x431 diagun is professional universal auto diagnostic tool which designed and developed by LA ...