原题链接:http://poj.org/problem?id=3268

Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15545   Accepted: 7053

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.

Source

题意

有一只牛举办派对,其他的牛去参加,牛都会走最短路,并且派对结束还要回到自己家里。问哪头牛走的路径最长,输出最长路径。

题解

就跑两边spfa,正着反着跑两次。然后就搞定了。

代码

#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstdio>
#define INF 1000006
#define MAX_N 1003
using namespace std; struct node {
public:
int u, c; node(int uu, int cc) : u(uu), c(cc) { } node() { }
}; struct edge {
public:
int to, cost; edge(int t, int c) : to(t), cost(c) { } edge() { }
}; queue<node> que;
int n,m,x;
void spfa(int s,vector<edge> G[],int d[]) {
fill(d, d + n + , INF);
que.push(node(s, ));
d[s] = ;
while (que.size()) {
node now = que.front();
que.pop();
if (now.c != d[now.u])continue;
int u = now.u;
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i].to;
int t = d[u] + G[u][i].cost;
if (t < d[v]) {
d[v] = t;
que.push(node(v, t));
}
}
}
} vector<edge> G[MAX_N],rG[MAX_N];
int d[MAX_N],rd[MAX_N];
int main() {
scanf("%d%d%d", &n, &m, &x);
for (int i = ; i < m; i++) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
G[u].push_back(edge(v, c));
rG[v].push_back(edge(u, c));
}
spfa(x, G, d);
while (que.size())que.pop();
spfa(x, rG, rd);
int ans = ;
for (int i = ; i <= n; i++)ans = max(ans, d[i] + rd[i]);
cout<<ans<<endl;
return ;
}

POJ 3268 Silver Cow Party 最短路的更多相关文章

  1. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

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

  2. poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13611   Accepted: 6138 ...

  3. poj 3268 Silver Cow Party(最短路dijkstra)

    描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...

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

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

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

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

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

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

  7. poj 3268 Silver Cow Party(最短路)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17017   Accepted: 7767 ...

  8. POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路

    Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...

  9. POJ 3268 Silver Cow Party 单向最短路

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22864   Accepted: 1044 ...

随机推荐

  1. Aizu - 1386 Starting a Scenic Railroad Service (思维乱搞)

    给你n个区间,求: 1:最多有多少区间与同一个区间相交. 2:相交部分的最大区间数目. Sample Input 1 4 1 3 1 3 3 6 3 6 Sample Output 1 2 2 Sam ...

  2. mac 安装composer的方法

    打开命令后 cd /usr/local/bin 然后执行 curl -sS https://getcomposer.org/installer | php 接下来 sudo mv composer.p ...

  3. luogu2865 [USACO06NOV]路障Roadblocks 次短路

    注意:如果是这么个写法,堆数组要开成n+m的. 为什么呢?设想一下从1到2有m条长度递减的路,这岂不是要入队m次-- #include <algorithm> #include <i ...

  4. 28、editText只输入英文字母和'-',用于授权码输入

    InputFilter filter = new InputFilter() { @Override public CharSequence filter(CharSequence source, i ...

  5. 浅谈我所见的CSS命名风格

    在两年工作中,总结一下我所见的css命名风格. 1.单一class命名 .header { width: 500px; } .item { text-indent: 20%; } 优点:简单,渲染效率 ...

  6. Eclipse下创建Spring MVC web程序--非maven版

    首先, 安装eclipse和tomcat, 这里我下载的是tomcat9.0版本64位免安装的:地址https://tomcat.apache.org/download-90.cgi 免安装的如何启动 ...

  7. python学习-- class 类中需要注意的地方

    from django.db import models class Person(models.Model):     name = models.CharField(max_length=30) ...

  8. 基于UDP的交互的实例

    1.实现简单的客户端.服务端聊天交互 问题是:客户端不能单独一直发消息回复.. 服务端: import socket server=socket.socket(socket.AF_INET,socke ...

  9. TOJ4483: Common Digit Pairs

    4483: Common Digit Pairs  Time Limit(Common/Java):3000MS/9000MS     Memory Limit:65536KByteTotal Sub ...

  10. 九度oj 题目1357:疯狂地Jobdu序列

    题目描述: 阳仔作为OJ的数据管理员,每一周的题目录入都让其很抓狂,因为题目不是他出的,他控制不了出题的速度……在等题目的时候,阳仔又不敢出去打篮球,所以只能在纸上乱涂乱写,这天,阳仔在纸上写下了这样 ...