POJ 3268 Silver Cow Party 单向最短路
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 22864 | Accepted: 10449 |
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
#include <iostream>
#include <deque>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define maxn 100010
using namespace std;
vector<pair<int,int> > E[maxn];
int d1[maxn],d2[maxn];
int n,m,t,x[maxn],y[maxn],z[maxn];
int max(int a,int b){
if(a>=b){
return a;
}
return b;
}
void init1(){
for(int i=;i<maxn;i++){
E[i].clear();
d1[i] = 1e9;
}
}
void init2(){
for(int i=;i<maxn;i++){
E[i].clear();
d2[i] = 1e9;
}
}
void dijkstra(int t,int d[]){
d[t] = ;
priority_queue<pair<int,int> > q;
q.push(make_pair(-d[t],t));
while(!q.empty()){
int now = q.top().second;
q.pop();
for(int i=;i<E[now].size();i++){
int v = E[now][i].first;
if(d[v] > d[now] + E[now][i].second){
d[v] = d[now] + E[now][i].second;
q.push(make_pair(-d[v],v));
}
}
}
}
int main()
{
while(cin >> n >> m >> t){
init1();
for(int i=;i<m;i++){
cin >> x[i] >> y[i] >> z[i];
E[x[i]].push_back(make_pair(y[i],z[i]));
}
dijkstra(t,d1);//正求一次
init2();
for(int i=;i<m;i++){
E[y[i]].push_back(make_pair(x[i],z[i]));
} //记得在这里要把所有的路反过来
dijkstra(t,d2);//反求一次
int num = -;
for(int i=;i<=n;i++){
num = max(num,d1[i]+d2[i]);
}
cout << num << endl;
}
return ;
}
POJ 3268 Silver Cow Party 单向最短路的更多相关文章
- 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 (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- poj 3268 Silver Cow Party(最短路,正反两次,这个模版好)
题目 Dijkstra,正反两次最短路,求两次和最大的. #define _CRT_SECURE_NO_WARNINGS //这是找出最短路加最短路中最长的来回程 //也就是正反两次最短路相加找最大的 ...
- POJ 3268 Silver Cow Party(最短路&Dijkstra)题解
题意:有n个地点,有m条路,问从所有点走到指定点x再走回去的最短路中的最长路径 思路:用Floyd超时的,这里用的Dijkstra. Dijkstra感觉和Prim和Kruskal的思路很像啊.我们把 ...
- 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 (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
随机推荐
- 章节十五、6-log4 2-用默认的配置
一.实例演示 package log4jtutorial; import org.apache.logging.log4j.LogManager; import org.apache.logging. ...
- 【Java例题】7.6文件题3-文本文件统计
6.文本文件统计.已有一个文本文件文件,请统计数字.大写字母.小写字母.汉字及其它字符出现的次数:然后将这些次数由大到小写到另一个文件之中.说明:将次数为零的过滤掉排序 package chapter ...
- Lua语言学习
1,语法 语句不用分号结尾 function ... end if .. else .. end 2, io库, string库, table库, OS库, 算术库, debug库 3, dofile ...
- android ——滑动菜单
一.DrawerLayout是一个拥有两个子控件的布局,第一个子控件是主屏幕中显示的内容,第二个子控件是滑动菜单中显示的内容: <android.support.v4.widget.Drawer ...
- studio无限轮播
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...
- 100天搞定机器学习|Day33-34 随机森林
前情回顾 机器学习100天|Day1数据预处理 100天搞定机器学习|Day2简单线性回归分析 100天搞定机器学习|Day3多元线性回归 100天搞定机器学习|Day4-6 逻辑回归 100天搞定机 ...
- Flink 源码解析 —— Standalone Session Cluster 启动流程深度分析之 Job Manager 启动
Job Manager 启动 https://t.zsxq.com/AurR3rN 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1学习 -- Mac ...
- vs 中本地 git 的基本使用
用 svn 有个毛病就是只有在改好了之后,才能提交.当周期比较长的时候,连自己都不知道自己改了什么东西,或者意外断电的时候,vs 中已保持的项目都有可能被 vs 去掉. 这个时候,使用 git 创建 ...
- 关于在taro使用wx.parse那些事
好久不见,好久没更新博客,最近工作也比较忙,今天在使用解决富文本的时候遇到两个bug,由于第一次使用wx.parse经验不足,走了很多弯路,今天特地把自己修复bug的感想分享一下,希望能帮助更多的小伙 ...
- ZooKeeper 相关概念以及使用小结
Dubbo 通过注册中心在分布式环境中实现服务的注册与发现,而注册中心通常采用 ZooKeeper,研究注册中心相关源码绕不开 ZooKeeper,所以学习了 ZooKeeper 的基本概念以及相关 ...