Roadblocks
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

Line 1: Two space-separated integers: N and R 
Lines 2..R+1: Each line contains three space-separated integers: AB, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and node N

Sample Input

4 4
1 2 100
2 4 200
2 3 250
3 4 100

Sample Output

450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

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的更多相关文章

  1. POJ 3255 Roadblocks(A*求次短路)

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12167   Accepted: 4300 Descr ...

  2. POJ 3255 Roadblocks (次级短路问题)

    解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...

  3. poj 3255(次短路)

    题目链接:http://poj.org/bbs?problem_id=3255 思路:分别以源点1和终点N为源点,两次SPFA求得dist1[i](1到各点的最短距离)以及dist2[i](各点到N的 ...

  4. POJ 3255 Roadblocks (次短路模板)

    Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K       Descriptio ...

  5. 次最短路径 POJ 3255 Roadblocks

    http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...

  6. poj 3255 Roadblocks

    Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13216 Accepted: 4660 Descripti ...

  7. Roadblocks(poj 3255)

    题意:给出n个点,m条双向边,求严格次短路. /* 先spfa预处理出起点到每个点的和每个点到终点的最短距离,然后枚举每条边(这条边必须走),计算此时的最短路径,得出严格次短路. 正确性:因为对于一条 ...

  8. POJ 3255 Roadblocks --次短路径

    由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...

  9. MST:Roadblocks(POJ 3255)

       路上的石头 题目大意:某个街区有R条路,N个路口,道路双向,问你从开始(1)到N路口的次短路经长度,同一条边可以经过多次. 这一题相当有意思,现在不是要你找最短路径,而是要你找次短路经,而且次短 ...

随机推荐

  1. eclipse集成maven

    1.工具下载: Eclipse4.2 jee版本(这里使用最新的Eclipse版本,3.7以上版本按照以下步骤都可以) 下载地址:http://www.eclipse.org/downloads/do ...

  2. Mybatis typeAliases别名

    <typeAliases> <typeAlias type="com.green.phonemanage.model.CellPhone" alias=" ...

  3. App创意项目助跑计划

    APP创意项目助跑计划 该计划旨在帮助同学们将各种脑中稀奇古怪的想法借助互联网/移动互联网 相关的技术变成真实的项目. 谱写你的故事,从此刻开始! 我们帮助你提高编程(Java.C++.Objecti ...

  4. linux php安装zookeeper扩展

    linux php安装zookeeper扩展 tags:php zookeeper linux ext 前言: zookeeper提供很犀利的命名服务,并且集群操作具有原子性,所以在我的多个项目中被采 ...

  5. 关于ios极光推送server端注意的地方

    今天试用了极光推送API 用它是因为,大多数人说它的文档是最全的,但是用过之后,发现关于IOS的文档,还是很不够,导致走了一点弯路! 特别是服务端的代码:https://github.com/jpus ...

  6. MVC3+中 ViewBag、ViewData和TempData的使用和区别

    在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic),ViewData 是 ...

  7. C#中如何利用操作符重载和转换操作符

    操作符重载 有的编程语言允许一个类型定义操作符应该如何操作类型的实例,比如string类型和int类型都重载了(==)和(+)等操作符,当编译器发现两个int类型的实例使用+操作符的时候,编译器会生成 ...

  8. Querying mergeinfo requires version 3 of the FSFS filesystem schema

    环境: jdk 1.7; svn  3.0.4;  TortoiseSVN 1.7.13 Subversion 1.7.10; IntelliJ IDEA 13.1.1;win7 64位系统 之前那个 ...

  9. Android -- 倒计时的实现

    CountDownTimer                                                                      CountDownTimer这个 ...

  10. How to join a Ubuntu to Windows Domain

    My testing environment: Windows Server 2012 R2 Essentials: With AD and standalone DC in one single b ...