poj 3268 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
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
#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的更多相关文章
- 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算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- 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 最短路
原题链接: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 ...
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13982 Accepted: 6307 ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
随机推荐
- 百度UEditor组件出现Parameters: Invalid chunk '' ignored警告的分析
使用百度UEditor在线编辑器组件时,出现Parameters: Invalid chunk '' ignored的警告,之前的项目使用却没有.两个项目的环境应该是一样的. 没有时间去对照两项目使用 ...
- C和C++函数互相调用
Call C++ function from C & Call C function from C++ (C和C++函数互相调用) By williamxue on Jun 12, 2007 ...
- Android精品课程—PullToRefresh 下拉刷新
http://edu.csdn.net/course/detail/1716 TableLayout http://edu.csdn.net/course/detail/2262 Android开发之 ...
- UBI FAQ and HOWTO
转:http://www.linux-mtd.infradead.org/faq/ubi.html UBI FAQ and HOWTO Table of contents How do I enabl ...
- 小白日记49:kali渗透测试之Web渗透-XSS(三)-存储型XSS、DOM型XSS、神器BEFF
存储型XSS与DOM型XSS [XSS原理] 存储型XSS 1.可长期存储于服务器端 2.每次用户访问都会被执行js脚本,攻击者只需侦听指定端口 #攻击利用方法大体等于反射型xss利用 ##多出现在留 ...
- iOS开发中添加PrefixHeader.pch要注意的问题
在Xcode6.0已经不默认生成PrefixHeader.pch文件了,而PrefixHeader.pch文件对我们开发带来的便利性是不言而喻的,所以我们怎么在工程中添加PrefixHeader.pc ...
- WPF 之 style文件的引用
总结一下WPF中Style样式的引用方法. 一.内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment.VerticalAlignment等属 ...
- .net 后台获取当前请求的设备
检查当前发起请求的设备是手持设备还是电脑端 以便显示不同的视图 public static bool CheckIsMobile(HttpRequestBase req) { bool flag = ...
- jquery 请求apache solr 跨域解决方案
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> ...
- iOS 网络/本地 图片 按自定义比例缩放 不失真 方法
我尝试了很多种方法,终于,设计了一个方法,能按自己规定的大小压缩 还没失真 如果以后不好用 我再升级 分享给大家: + (CGRect )scaleImage:(UIImage *)image toS ...