POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268
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
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
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
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 最短路的更多相关文章
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13611 Accepted: 6138 ...
- 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 ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- 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 ...
- POJ 3268 Silver Cow Party 单向最短路
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22864 Accepted: 1044 ...
随机推荐
- matplotlib学习记录 三
# 绘制自己和朋友在各个年龄的女友数量的折线图 from matplotlib import pyplot as plt # 让matplotlib能够显示中文 plt.rcParams['font. ...
- apply 与 lambda
Python中的lambda和apply用法 https://blog.csdn.net/anshuai_aw1/article/details/82347016
- 库函数的使用:POJ1488-TEX Quotes(getline()的使用)
TEX Quotes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9385 Description TEX is a type ...
- ACM-ICPC 2015 Shenyang Preliminary Contest B. Best Solver
The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart. ...
- $ cat /usr/share/doc/wireshark-common/README.Debian
$ cat /usr/share/doc/wireshark-common/README.Debian I. Capturing packets with Wireshark/Tshark There ...
- JS实现——Base64编码解码,带16进制显示
在网上找了个JS实现的Base64编码转换,所以就想自己研究下,界面如下: 将代码以BASE64方式加密.解密 请输入要进行编码或解码的字符: 编码结果以ASCII码16进制显示 解码结果以ASCII ...
- loj2024「JLOI / SHOI2016」侦查守卫
too hard #include <iostream> #include <cstdio> using namespace std; int n, d, m, uu, vv, ...
- RHEL6.X设置163yum源
目录 RHEL6.X设置163yum源 卸载系统的yum 检查是否已经卸载完成 下载yum以及相关包 安装yum相关rpm包 清除原有缓存,建立yum列表 本地yum源设置 挂载本地光盘 修改配置文件 ...
- postgres 用户管理
首次安装完成 pg 数据库后,会默认自带一个用户, 用户名: postgres 密码: postgres 可以使用命令 \du 查看数据库用户 创建新用户: create user dev with ...
- 浮动 float
1.未设浮动属性,位于标准流中 2.如果设置浮动属性,则容器不再位于标准流中,不再占用空间.容器会根据内容确定宽度 3.尽量将搜索引擎要搜索的内容放到网页的前部,更要容易排名到 4.clear属性清除 ...