博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3268 Silver Cow Party
阅读量:7136 次
发布时间:2019-06-28

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

 

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18713   Accepted: 8561

Description

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: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 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.

Source

 
二话不说就是一个DFS砸上去,敲一半才看到这图不一定是树……默默删了代码写SPFA。每头牛都要走到X点再走回去,那么就在正向边和反向边上各跑一次SPFA,枚举每个点看,找最长的来回距离和就是答案。
 
1 /*by SilverN*/ 2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 const int mxm=300010;10 const int mxn=2000;11 struct edge{12 int v,dis;13 int next;14 bool b;//判断正向反向 15 }e[mxm];16 int hd[mxm],cnt;17 int n,m,x;18 int dis[mxn];19 int ans[mxn];20 void add_edge(int u,int v,int dis){21 e[++cnt].v=v;e[cnt].next=hd[u];e[cnt].dis=dis;hd[u]=cnt;22 e[cnt].b=1;//正向 23 e[++cnt].v=u;e[cnt].next=hd[v];e[cnt].dis=dis;hd[v]=cnt;24 e[cnt].b=0;//反向 25 }26 int inq[mxn];27 void SPFA(bool dir){28 memset(dis,32,sizeof dis);29 queue
q;30 inq[x]=1;31 dis[x]=0;32 q.push(x);33 while(!q.empty()){34 int u=q.front();q.pop();inq[u]=0;35 for(int i=hd[u];i;i=e[i].next){36 if(e[i].b!=dir)continue;37 int v=e[i].v;38 if(dis[u]+e[i].dis
mxans)mxans=dis[i]+ans[i];63 }64 printf("%d\n",mxans);65 return 0;66 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/5774651.html

你可能感兴趣的文章
信用算力实现金融级数据服务的实践
查看>>
Xcode配置测试环境和线上环境
查看>>
三大主流软件负载均衡器对比(LVS 、 Nginx 、Haproxy)
查看>>
学习技能总结:
查看>>
高可用集群----理论
查看>>
backtrack两种开启ssh方式
查看>>
redis 目录
查看>>
C语言中const的用法
查看>>
理解inode
查看>>
我的友情链接
查看>>
Delegates (Delegate, Func, Predicate, Action and Lambda)
查看>>
快速打开Windows的大部分程序
查看>>
SSIS 系列 - Lookup 组件的使用与它的几种缓存模式 - Full Cache, Partial Cache, NO Cache...
查看>>
监测textfield输入的值得变化
查看>>
lvs简介和命令
查看>>
RPM 命令详解
查看>>
在Centos7中使用firewall添加端口
查看>>
python 通过threading多线程ssh
查看>>
spark-submit使用及说明
查看>>
jquery验证表单的js代码(HTML+CSS+PHP代码部分省略)
查看>>