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路口的次短路经长度,同一条边可以经过多次. 这一题相当有意思,现在不是要你找最短路径,而是要你找次短路经,而且次短 ...
随机推荐
- EMVTag系列13《脱机PIN》
DGI8010用于个人化借记贷记交易中使用的脱机PIN.数据强制要求加密.制卡数据传输过程中,此DGI采用DEK加密保护. 数据分组标识 '8010'的数据内容 要求 ...
- 菜鸟学习Hibernate——缓存
Hibernate的缓存分为三种:一级缓存.二级缓存.查询缓存.下面我就为大家介绍一下. 一.概念. 一级缓存:第一级存放于session中称为一级缓存.Session 级别的缓存,它同session ...
- oracle 配置 oem
1.启动命令是[oracle@yoon ~]$ cd $ORACLE_HOME/bin [oracle@yoon ~]$ ./emctl start dbconsole 停止命令是[oracle@yo ...
- core java 1~4(HelloWorld & 标识符|关键字|数据类型 & 表达式|流程控制 & 数组)
MODULE 1 Java的编译和运行----------------------------编译:javac -d bin src\HelloWorld.java -d:指定编译后的class 文件 ...
- Entity Framework学习笔记(三)----CRUD(2)
请注明转载地址:http://www.cnblogs.com/arhat 昨天晚上老魏配的机器终于到了,可是拿回来之后什么都组装好了,唯独差一个非常重要的组件"电源线",老魏那个汗 ...
- vi/vim编辑器
vi / vim是Unix / Linux上最常用的文本编辑器而且功能非常强大.
- [shell基础]——I/O重定向
文件标识符(FD) 1. Linux使用文件标识符(FD)来标识一个进程正在访问的特定文件 2. 当打开一个文件或创建一个文件时,Linux将返回一个文件标识符供其他操作引用 3. 文件标识符是一个小 ...
- mysql使用二进制日志恢复数据
一.恢复到某个二进制文件 1.开启二进制日志 在mysqld的配置节点下添加如下配置 log-bin="E:/Mysql57BinLog/binlog"(windows下的路径,l ...
- 受限玻尔兹曼机RBM—简易详解
- 二、break,continue区别
break:作用于switch,和循环语句,用于跳出,或者称为结束 break语句单独存在,下面不要定义其他语句,因为执行不到,编译会失败,当循环套时,break会跳出当前所在循环,要跳出外部循环,只 ...