Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19325   Accepted: 8825

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.
 
思路:单元最短路径,dijkstra算法,出发时分别以每一个点作为起点,搜索到达终点的最短路径;以及回来时从终点到达各点的最短路径,最后将来回两条路径长度取和,取其中的最大值。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
using namespace std;
typedef pair<int, int> P;
const int V_MAX = + ;
struct edge {
int to, time;
};
int V;
vector<edge>G[V_MAX];
int d[V_MAX];
int x[V_MAX];
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P>>que;
fill(d,d+V,INT_MAX);
d[s] = ;
que.push(P(,s));
while (!que.empty()) {
P p = que.top();que.pop();
int v = p.second;
if (d[v] < p.first)continue;
for (unsigned int i = ;i < G[v].size();i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.time) {
d[e.to] = d[v] + e.time;
que.push(P(d[e.to],e.to));
}
}
}
} int main() {
int N, M,X;
scanf("%d%d%d", &N, &M, &X);
X--;
V = N;
for (int i = ;i < M;i++) {
edge E;
int from;
scanf("%d%d%d",&from,&E.to,&E.time);
from--;E.to--;
G[from].push_back(E);
}
dijkstra(X);
memset(x,,sizeof(x));
for (int i = ;i < V;i++) {
x[i] += d[i];
}
for (int i = ;i < V;i++) {
dijkstra(i);//以i点为中心,
x[i] += d[X];//计算i到目的地的最短距离并累加
}
int result = *max_element(x,x+V);
printf("%d",result);
return ;
}

这样用dijkstra算法搜索次数过多,耗时过多,看了hankcs博主的文章,很有启发,思路:分别以目的地为起点和终点,使用两次dijkstra算法即可,这样一来存路径时需要用两个数组,一个存正向路径,一个存反向路径,正向路径用于计算以目的地为起点时走到各点的最短路径,反向路径用于计算以目的地为终点时各点走到目的地的最短路径。

AC代码:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
#include<string.h>
using namespace std;
typedef pair<int, int> P;
const int V_MAX = + ;
struct edge {
int to, time;
edge() {};
edge(int a,int b):to(a),time(b){}
};
int V;
vector<vector<edge>>G(V_MAX);//预定义容量,以防止越界
vector<vector<edge>>RG(V_MAX);
//vector<edge>G[V_MAX];
//vector<edge>RG[V_MAX];
int d[V_MAX];
int rd[V_MAX];
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P>>que;
fill(d,d+V,INT_MAX);
d[s] = ;
que.push(P(,s));
while (!que.empty()) {
P p = que.top();que.pop();
int v = p.second;
if (d[v] < p.first)continue;
for (unsigned int i = ;i < G[v].size();i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.time) {
d[e.to] = d[v] + e.time;
que.push(P(d[e.to],e.to));
}
}
}
} int main() {
int N, M,X;
scanf("%d%d%d", &N, &M, &X);
X--;
V = N;
for (int i = ;i < M;i++) {
edge E;
int from,to,time;
scanf("%d%d%d",&from ,&to,&time);
from--;to--;
G[from].push_back(edge(to,time));
RG[to].push_back(edge(from,time));//存反向图
}
dijkstra(X);
//G = RG;
G.swap(RG);
memcpy(rd,d,sizeof(d));
dijkstra(X);
for (int i = ;i < V;i++) {
d[i] += rd[i];
}
int result = *max_element(d,d+V);
printf("%d",result);
return ;
}

poj 3268 Silver Cow Party的更多相关文章

  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 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  3. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  4. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  5. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  6. 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 ...

  7. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  8. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  9. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

随机推荐

  1. 面试感悟----一名3年工作经验的程序员应该具备的技能 JAVA 必读书

    http://www.cnblogs.com/xrq730/p/5260294.html#3470685 http://www.cnblogs.com/xrq730/p/4994545.html

  2. 四元数(Quaternion)和旋转

    四元数介绍 旋转,应该是三种坐标变换——缩放.旋转和平移,中最复杂的一种了.大家应该都听过,有一种旋转的表示方法叫四元数.按照我们的习惯,我们更加熟悉的是另外两种旋转的表示方法——矩阵旋转和欧拉旋转. ...

  3. Foxit Reader 插件下载

    http://www.foxitsoftware.com/Secure_PDF_Reader/addons.php#install 百度云:http://pan.baidu.com/s/1i3DSlv ...

  4. linux连接静态库

    在项目中发现,使用 -l连接某个库时,如果存在同名的静态库(.a)和动态库(.so),默认会连接.so 那么如何指定连接静态库呢?如果有多个库,有些要连接静态库.有些要连接动态库,连接选项该如何指定呢 ...

  5. 小白日记47:kali渗透测试之Web渗透-XSS(一)

    XSS [推荐书籍:XSS跨站脚本攻击剖析与防御] xss表示Cross Site Scripting(跨站脚本攻击),它与SQL注入攻击类似,SQL注入攻击中以SQL语句作为用户输入,从而达到查询/ ...

  6. WPF 之 后台设置Image的Souce

    后台动态设置Image的Souce. 方法一: BitmapImage imgSource = new BitmapImage(new Uri("location",UriKind ...

  7. 在虚拟环境中安装pygame

    http://www.pygame.org/wiki/CompileUbuntu#Python%203.x%20into%20virtual%20environment 先安装依赖: ᐅ sudo a ...

  8. 【开源项目6】介绍MenuDrawer这个牛x的控件,实现左右出菜单,上下出菜单

    现在很多应用都很潇洒的从左边屏幕手势一划出个左边的隐藏菜单,右边一划出个隐藏菜单,上边一划出个隐藏菜单,下边一划出个隐藏菜单.或者像android的API16左右的激活列表项的功能.很多人肯定都很着迷 ...

  9. 源自梦想 自定义ViewGroup的整理_1

    今天说说自定义控件,稍微偏底层一点的东西.今天的主要任务是自己完全写代码,写一个ViewGroup,实现一个类似ViewPager这样的一个功能. 大家自定义View肯定写过,不过估计写的也不多.等大 ...

  10. 关于automatic_Panoramic_Image_Stitching_using_Invariant_features 的阅读笔记(2)

    接上一篇: http://www.cnblogs.com/letben/p/5446074.html#3538201 捆绑调整 (好开心有同学一起来看看这些问题,要不然就是我自己的话,我应该也不会看的 ...