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 ...
随机推荐
- LeetCode(147) Insertion Sort List
题目 Sort a linked list using insertion sort. 分析 实现链表的插入排序 注意: 程序入口的特殊输入判断处理! 节点的链接处理,避免出现断链! AC代码 /** ...
- (转)减少oracle sql回表次数 提高SQL查询性能
要写出高效的SQL,那么必须必须得清楚SQL执行路径,介绍如何提高SQL性能的文章很多,这里不再赘述,本人来谈谈如何从 减少SQL回表次数 来提高查询性能,因为回表将导致扫描更多的数据块. 我们大家都 ...
- selenium2-元素管理方式及解析
1.管理文件格式:yaml 2.Yaml里面的内容格式: 3.格式说明: baidu_input后面接上":",直接回车,然后空两格 type与value这两个key是固定 ...
- 启动Chrome浏览器弹出“You are using an unsupported command-line flag –ignore-certificate-errors. Stability and security will suffer”
采用如下代码: public static void launchChrome() { System.setProperty("webdriver.chrome.driver", ...
- JSP 页面 jstl 时间戳 long型转时间
转载http://www.cnblogs.com/gmq-sh/p/5528989.html
- Hive学习笔记(二)
Hive内部表跟外部表之间的区别 创建外部表 先删除上面创建的表,指定location 此时在hdfs根目录下就有一个hivedata文件夹 上传文本数据到hivedata目录下 查询表中数据 删除上 ...
- 我是怎么用FullCalendar记录我的2013年(辞职N次,面试2N次)的,它还兼容IE6
地址:wanshanshan.com中的”日程“功能 喜欢就点击我吧 流程介绍 ここはじまる! 前端采用javascriptMVC框架:控制器C,模型M,视图V ,控制器控制着视图和模型之间的联系和 ...
- day03_13 多分支if语句及作业
猜年龄升级版 age_of_princal = 56 guess_age = int( input("请输入您猜测的年龄") ) if guess_age == age_of_pr ...
- 根据已经commit的数据,进行leader和peon之间的同步
Leader Election基本设计 按照rank表示优先级解决冲突问题,为每个monitor预先分配了一个rank 只会接受优先级(rank)比自己高.epoch比上次已接受的epoch大的选举请 ...
- kb-01-e<取余操作,宽搜,巧妙>;
题目描述: n属于1到200,找到对应的一个数只含有0和1,并且是n的倍数: 分析: 本题有几个数会是大数:所以要考虑大数: 用到余数的性质:例如n=6,1%6=1: 1*10%6=4: ...