博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3268 Silver Cow Party(最短路)
阅读量:5051 次
发布时间:2019-06-12

本文共 2764 字,大约阅读时间需要 9 分钟。

题面

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) 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 1: Three space-separated integers, respectively: N, M, and X

Lines 2.. M+1: Line i+1 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 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2

1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

题解

题目大意:1-N节点的牛要到X节点开Party,问来回所需时间最大的牛所花费的时间(所有的边都是单向边)

存正边和反边,跑2遍SPFA求最大值即可

#include
#include
#include
#include
#include
#include
#include
using namespace std;#define MAX 1100#define MAXL 100100inline int read(){ register int x=0,t=1; register char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-')ch=getchar(); if(ch=='-'){t=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();} return x*t;}struct Line{ int v,next,w;}e[MAXL],E[MAXL];//正边和反向边int N,M,X;int h[MAX],H[MAX],cnt=1;bool vis1[MAX],vis2[MAX];int dis1[MAX],dis2[MAX];queue
Q; inline void Add(int u,int v,int w){ e[cnt]=(Line){v,h[u],w}; E[cnt]=(Line){u,H[v],w}; h[u]=H[v]=cnt++;}int main(){ N=read();M=read();X=read(); for(int i=1;i<=M;++i) { int u=read(),v=read(),w=read(); Add(u,v,w); } /****SPFA1****/ vis1[X]=true; for(int i=1;i<=N;++i) dis1[i]=1000000000; dis1[X]=0; Q.push(X); while(!Q.empty()) { int u=Q.front();Q.pop(); vis1[u]=false; for(int i=h[u];i;i=e[i].next) { int v=e[i].v,w=e[i].w; if(dis1[v]>dis1[u]+w) { dis1[v]=dis1[u]+w; if(!vis1[v]) { vis1[v]=true; Q.push(v); } } } } /****SPFA2****/ vis2[X]=true; for(int i=1;i<=N;++i) dis2[i]=1000000000; dis2[X]=0; Q.push(X); while(!Q.empty()) { int u=Q.front();Q.pop(); vis2[u]=false; for(int i=H[u];i;i=E[i].next) { int v=E[i].v,w=E[i].w; if(dis2[v]>dis2[u]+w) { dis2[v]=dis2[u]+w; if(!vis2[v]) { vis2[v]=true; Q.push(v); } } } } int Ans=0; for(int i=1;i<=N;++i) Ans=max(Ans,dis1[i]+dis2[i]); cout<
<

转载于:https://www.cnblogs.com/cjyyb/p/7242632.html

你可能感兴趣的文章
Good Bye 2015 D. New Year and Ancient Prophecy
查看>>
使用Oracle SQL Developer 连接SQL Server
查看>>
Scss sass
查看>>
load image
查看>>
PHP之XML节点追加操作讲解
查看>>
鼠标拖动事件
查看>>
Log4j2 配置
查看>>
接口测试
查看>>
Underscore.js 入门
查看>>
IDEA设置网络代理&&Maven设置网络代理
查看>>
win7下64位系统memcache/memcached安装教程
查看>>
C#用DesignSurface实现一个简单的窗体设计器
查看>>
CUDA跟OpenCV的混合编程,注意OpenCV需要重新编译
查看>>
Team Foundation Server 2010 Performance Tuning – Lessons learned
查看>>
obj文件转换为gltf的方法
查看>>
系统运行与维护
查看>>
纯css画哆啦A梦
查看>>
SpringIOC学习一
查看>>
摄像头脸部识别 (1)opencv 抓取视频数据并保存
查看>>
[译]Django first steps Part3
查看>>