POJ 3255
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 6605 | Accepted: 2458 | 
Description
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
Input
Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
Output
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <utility> using namespace std; typedef pair<int,int> pii; const int INF = 1e9;
const int MAX_V = ;
const int MAX_E = ;
int N,R;
int first[MAX_V],next[ * MAX_E],v[ * MAX_E],w[ * MAX_E];
int dist[MAX_V],dist2[MAX_V]; void addedge(int a,int b,int id) {
int e = first[a];
next[id] = e;
first[a] = id;
} void solve() {
fill(dist + ,dist + N + ,INF);
fill(dist2 + ,dist2 + N + ,INF); dist[] = ;
priority_queue<pii,vector<pii>,greater<pii> > q;
q.push(pii(,)); while(!q.empty()) {
pii x = q.top(); q.pop();
int d = x.first,u = x.second;
for (int e = first[u]; e != -; e = next[e]) {
int d2 = d + w[e];
if(dist[v[e]] > d + w[e]) {
dist[v[e]] = d + w[e];
q.push(pii(dist[ v[e] ],v[e]));
} if(d2 < dist2[ v[e] ] && d2 > dist[ v[e] ]) {
dist2[ v[e] ] = d2;
q.push(pii(d2,v[e]));
} }
} printf("%d\n",dist2[N]);
}
int main()
{
//freopen("sw.in","r",stdin); scanf("%d%d",&N,&R);
for (int i = ; i <= N; ++i) first[i] = -;
for (int i = ; i < * R; i += ) {
int u;
scanf("%d%d%d",&u,&v[i],&w[i]);
v[i + ] = u;
w[i + ] = w[i];
addedge(u,v[i],i);
addedge(v[i],u,i + );
} solve(); return ;
}
POJ 3255的更多相关文章
- POJ 3255 Roadblocks(A*求次短路)
		
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12167 Accepted: 4300 Descr ...
 - POJ   3255   Roadblocks  (次级短路问题)
		
解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...
 - poj 3255(次短路)
		
题目链接:http://poj.org/bbs?problem_id=3255 思路:分别以源点1和终点N为源点,两次SPFA求得dist1[i](1到各点的最短距离)以及dist2[i](各点到N的 ...
 - POJ 3255 Roadblocks (次短路模板)
		
Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS Memory Limit: 65536K Descriptio ...
 - 次最短路径 POJ 3255 Roadblocks
		
http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...
 - poj 3255 Roadblocks
		
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13216 Accepted: 4660 Descripti ...
 - Roadblocks(poj 3255)
		
题意:给出n个点,m条双向边,求严格次短路. /* 先spfa预处理出起点到每个点的和每个点到终点的最短距离,然后枚举每条边(这条边必须走),计算此时的最短路径,得出严格次短路. 正确性:因为对于一条 ...
 - POJ 3255 Roadblocks --次短路径
		
由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...
 - MST:Roadblocks(POJ 3255)
		
路上的石头 题目大意:某个街区有R条路,N个路口,道路双向,问你从开始(1)到N路口的次短路经长度,同一条边可以经过多次. 这一题相当有意思,现在不是要你找最短路径,而是要你找次短路经,而且次短 ...
 
随机推荐
- Ruby使用gets的错误:gets得到的有'\n',需要使用chomp去掉
			
gets方法得到的字符串包含一个“\n”回车符,所以我们需要继续使用chomp方法把"\n"回车符去掉
 - hdu 1026 Ignatius and the Princess I
			
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
 - JavaScript 中怎样判断文本框只能输出英文字母、汉字和数字,不能输入特殊字符!
			
JS-只能输入中文和英文2008-11-08 10:17在js中用正则表达式对象(RegExp)判断中文 ^[\u0391-\uFFE5]+$英文 ^[A-Za-z]+$中文和英文/^[\u0391- ...
 - Go语言参数中的三个点是干什么的
			
Go语言函数中有三个点...表示为可变参数,可以接受任意个数的参数. 示例代码: package main import "fmt" func Greeting(prefix st ...
 - Qt---在QLabel上实现系统时间
			
参考:http://blog.csdn.net/g457499940/article/details/11923887 ---------------------------------------- ...
 - Qt:禁止qDebug的输出
			
Qt:禁止qDebug的输出 在工程的.pro文件里加上以下编译批令即可: DEFINES += QT_NO_DEBUG_OUTPUT
 - TFT LCD 参数详解
			
我的板子设置HCLK=100M因此CLKVAL= int(HCLK/(VCLK*2)-1),其中VCLK即上图的DCLK=6.4M, CLKVAL="int"(100/12.8-1 ...
 - (转) ASCII码对应表chr(9)、chr(10)、chr(13)、chr(32)、chr(34)、chr(39)、chr(
			
chr(9) tab空格 chr(10) 换行 chr(13) 回车 Chr(13)&chr(10) 回车换行 chr(32) 空格符 ...
 - 转载:SQL索引一步到位
			
原文: http://www.cnblogs.com/AK2012/archive/2013/01/04/2844283.html SQL索引一步到位(此文章为“数据库性能优化二:数据库表优化”附属文 ...
 - 20145120 《Java程序设计》第10周学习总结
			
20145120 <Java程序设计>第10周学习总结 教材学习内容总结 转自:http://www.cnblogs.com/springcsc/archive/2009/12/03/16 ...