题干:

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 Titime 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.

题目大意:

从出发点到达目的地X,再从X返回到出发点的最短路径中的最大值(因为出发点没有固定,也就是可以:1->X->1, 2->X->2等)。

解题报告:

跑两遍最短路,求一个x到各个点的,再求转置矩阵然后跑一边x到各个点的,也就是原矩阵的各个点到x点的。加起来维护最大值即可。

一个题解:所以我们要枚举所有的可能性,找出其中的最大值。巧妙地运用dijkstra算法,双向求出两次X->m的最短路径长然后相加即得到了m->X->m的最短路径。

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;
const int MAX = 100000 + 5;
const int INF = 0x3f3f3f3f ;
struct Node {int to;int w;int ne;
} e[MAX];
int a[100000 + 5],b[100000 + 5],w[100000 + 5];
struct point {int pos;int c;point(){}//没有此构造函数不能写  node t  这样point(int pos,int c):pos(pos),c(c){}//可以写node(pos,cost)这样bool operator <(const point & b) const {return c>b.c;}
};int head[MAX];
int vis[MAX];
int cnt = 0;
int maze[1005][1005];
int n,m;
int dis1[MAX],dis2[MAX];//表示从出发点开始到该点的最短距离。
void add(int u,int v,int w) {e[cnt].to = v;e[cnt].w = w;e[cnt].ne = head[u];head[u] = cnt;cnt++;
}
void Dijkstra1(int u) {priority_queue<point> pq; dis1[u] = 0;point cur = point(u,0);pq.push(cur);while(!pq.empty()) {point now = pq.top();pq.pop();vis[now.pos] = 1;for(int i = head[now.pos]; i!=-1; i=e[i].ne) {if(  dis1[e[i].to] > dis1[now.pos] + e[i].w ) {dis1[e[i].to] = dis1[now.pos] + e[i].w;pq.push(point(e[i].to,dis1[e[i].to] ) ); }} }
}
void Dijkstra2(int u) {priority_queue<point> pq; dis2[u] = 0;point cur = point(u,0);pq.push(cur);while(!pq.empty()) {point now = pq.top();pq.pop();vis[now.pos] = 1;  for(int i = head[now.pos]; i!=-1; i=e[i].ne) {if(  dis2[e[i].to] > dis2[now.pos] + e[i].w ) {dis2[e[i].to] = dis2[now.pos] + e[i].w;pq.push(point(e[i].to,dis2[e[i].to] ) );   }} }
}
void init1() {cnt = 0;memset(head,-1,sizeof(head) );memset(dis1,INF,sizeof(dis1) ) ;memset(maze,INF,sizeof(maze) );memset(vis,0,sizeof(vis) ) ;
}
void init2() {cnt = 0;memset(head,-1,sizeof(head) );memset(dis2,INF,sizeof(dis2) ) ;memset(maze,INF,sizeof(maze) );memset(vis,0,sizeof(vis) ) ;
}
int main()
{int x;while(~scanf("%d %d %d",&n,&m,&x) ) {init1();for(int i = 1; i<=m; i++) {scanf("%d%d%d",&a[i],&b[i],&w[i]);if(w[i]<maze[a[i] ][b[i] ]) {maze[a[i]][b[i]] = w[i];add(a[i],b[i],w[i]);}}Dijkstra1(x);init2();for(int i = 1; i<=m; i++) {if(w[i]<maze[b[i] ][a[i] ]) {maze[b[i]][a[i]] = w[i];add(b[i],a[i],w[i]);}}Dijkstra2(x);int maxx = 0;for(int i = 1; i<=n; i++) {maxx = max(maxx,dis1[i] + dis2[i]);}
//      printf("::::::::\n");printf("%d\n",maxx);}return 0 ;}

总结:

其实我这里写麻烦了 ,如果用邻接矩阵保存图的话,直接求转置矩阵然后再最短路一发就行了。如果用邻接表这样,就得先保存输入数据然后跑一边后再初始化然后再读入一遍再跑一遍。

【POJ - 3268 】Silver Cow Party(Dijkstra最短路+思维)相关推荐

  1. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  2. [POJ](3268)Silver Cow Party ---最短路径(图)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23762   Accepted: 1085 ...

  3. POJ - 3268 Silver Cow Party(最短路)

    题目链接:点击查看 题目大意:给出n个点以及m条单项路径和一个点x,设从x点到i的距离及从i回到x点的距离分别为d1和d2,求d1+d2的最大值(1<=i<=n) 题目分析:看到这个题的第 ...

  4. POJ 3268 Silver Cow Party--正反Dijkstra

  5. POJ 3268 Silver Cow Party

    题目链接 题意 单向图,N - 1个牛去聚会,求所有牛去聚会和回家路径和的最大值 AC 很骚的操作 首先从派对的地方跑Dijkstra求出回家的最短路,然后将所有边翻转再次从聚会跑Dijkstra就是 ...

  6. 【POJ】3268 Silver Cow Party (将有向图的边反转)

    问题链接:http://poj.org/problem?id=3268 [问题描述] One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  7. 【POJ】3268 Silver Cow Party

    题目链接:http://poj.org/problem?id=3268 题意 :有N头奶牛,M条单向路.X奶牛开party,其他奶牛要去它那里.每头奶牛去完X那里还要返回.去回都是走的最短路.现在问这 ...

  8. POJ 3268 D-Silver Cow Party

    http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  9. 【POJ - 1062】【nyoj - 510】昂贵的聘礼 (Dijkstra最短路+思维)

    题干: 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:& ...

最新文章

  1. WCF Security基本概念(转载)
  2. 用python tkinter显示Mandelbrot图
  3. nginx发布antd-pro项目(别人发的,未测试)
  4. 单源最短路径(Dijkstra算法)
  5. 设计模式 ( 十七 ):Observer 观察者模式 -- 行为型
  6. 六招教你快速提升网站交互体验,降低跳出率
  7. python用空格隔开每一个字符_python实现将一串字符每两个一组,中间用空格隔开...
  8. python如何识别中文_python如何识别图片中的文字
  9. 一个通用的Makefile模板-转
  10. caffe 实践程序2——用细分的方法实现caffe中cifar100的识别
  11. OSMDroid —— 开源的 Android 地图开发库
  12. Linux下载离线安装包
  13. PLC 数据内存读写 调试软件工具
  14. 什么是磁盘?磁盘的组成?接口和分区?
  15. 武汉最最最牛逼的IT公司全在这了
  16. 一群参与境内外赌博网站的开发的程序员被抓,网友:切勿面向监狱编程。。。...
  17. 数据结构与算法学习---数据结构篇(线性表)(默然回首,夯实基础)
  18. 【数据库】用户管理---君权神授
  19. 搭建Vulhub靶场 【附图】
  20. mysql expire_mysql expire_logs_days是怎么生效的

热门文章

  1. linux验证cuda安装成功_Linux环境CUDA 4.0入门:验证安装
  2. C语言 system函数
  3. 赋值给集合_ArrayList集合源码
  4. oracle sal01,oracle中 all any in的用法
  5. java一般做什么_java开发一般做什么
  6. Linux 禁用msi模式,通过禁用MSI模式解决Win10磁盘占用100%的方法
  7. 数据结构c语言版第四章题库,数据结构(C语言版)(第4版)习题
  8. 超几何分布_常见概率分布
  9. Ubuntu 14.04 为 root 帐号开启 SSH 登录
  10. linux powerpc i2c驱动 之 i2c设备层的注册过程