Silver Cow Party

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 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.
 
题目大意:给你n个点,m条有向边。x为起点。问你其他人从自己所在位置到达x,然后返回自己所在位置。问最晚到达自己所在位置的时间。每人的移动速度相同。
解题思路:反向建图一次,跑一次最短路。正向建图,跑一次最短路。然后把两次的d值加和,求出最大值即可。
 
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
const int maxn = 1e4+200;
const int maxo = 1e5+200;
const int INF = 0x3f3f3f3f;
struct Oper{
int u,v,w;
}opers[maxo];
struct HeapNode{
int d;
int u;
bool operator < (const HeapNode & rhs)const {
return d > rhs.d; //
}
};
struct Edge{
int from,to;
int dist;
};
vector<Edge>edge;
vector<int>G[maxn];
priority_queue<HeapNode>PQ;
int d[maxn] , vis[maxn];
int ans[maxn];
int n,m;
void init(){
for(int i = 0; i <= n;i++){
G[i].clear();
}
edge.clear();
}
void AddEdge(int u,int v,int w){
edge.push_back( (Edge){ u ,v, w } );
m = edge.size();
G[u].push_back(m-1);
}
void Dijstra(int s){
for(int i = 0; i <= n; i++){
d[i] = INF;
}
d[s] = 0;
PQ.push( (HeapNode){ d[s],s} );
memset(vis,0,sizeof(vis));
while(!PQ.empty()){
HeapNode x = PQ.top();
PQ.pop();
int u = x.u;
if(vis[u]) continue;
vis[u] = 1;
for( int i = 0; i < G[u].size(); i++){
Edge & e = edge[G[u][i]];
if(d[e.to] > d[e.from]+e.dist){
d[e.to] = d[e.from] + e.dist;
PQ.push( (HeapNode){d[e.to] , e.to} );
}
}
}
}
int main(){
int k , mm;
while(scanf("%d%d%d",&n,&mm,&k)!=EOF){
init();
memset(ans,0,sizeof(ans));
int a,b,c;
for(int i = 0; i < mm; i++){
scanf("%d%d%d",&a,&b,&c);
a--,b--;
AddEdge(b,a,c);
opers[i].u = a ;
opers[i].v = b ;
opers[i].w = c ;
}
Dijstra(k-1);
for(int i = 0; i < n;i++){
ans[i] = d[i];
}
init();
for(int i = 0; i < mm; i++){
AddEdge(opers[i].u,opers[i].v,opers[i].w);
}
Dijstra(k-1);
int res = 0;
for(int i = 0; i < n; i++){
ans[i] += d[i];
if(res < ans[i]){
res = ans[i];
}
}
printf("%d\n",res);
}
return 0;
}

  

 

最新文章

  1. springmvc4 mybatis 整合 框架源码 bootstrap
  2. struts2进阶篇(2)
  3. [转载]Eclipse提示No java virtual machine
  4. 从MySQL 5.5到5.7看复制的演进
  5. Windows7给C盘扩容
  6. WPF程序只运行一个实例
  7. maven的仓库、生命周期与插件
  8. WinMain初始化详细过程以及消息循环
  9. unity脚本的基础语法
  10. python会什么比c慢
  11. windows上使用SecureCRT连接linux
  12. linux系统centOS7下搭建redis集群中ruby版本过低问题的解决方法
  13. [转] HTML5 FormData 方法介绍以及实现文件上传
  14. StackExchange.Redis和Log4Net构建日志
  15. Spring Boot整合MyBatis(使用Spring Tool Suite工具)
  16. spring中xml配置方式和注解annoation方式(包括@autowired和@resource)的区别
  17. ps常用快捷键
  18. 使用nginx反向代理解决前端跨域问题
  19. Mysql读写分离——主从数据库+Atlas
  20. 【TOJ 1072】编辑距离(动态规划)

热门文章

  1. pycharm ubuntu安装
  2. mongodb循环插入测试数据
  3. [51nod]多重背包模板
  4. bos物流面试题
  5. ps -ef | grep java 输出的具体含义是什么?
  6. LeetCode第14题:最长公共前缀
  7. Entity Framework 分页处理
  8. Kolla Ocata版本安装及镜像制作流程
  9. ERROR 1010 (HY000): Error dropping database (can&#39;t rmdir &#39;./nsd&#39;, errno: 39)
  10. PHP 预定义常量(魔术常量)