题目:

Roadblocks
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7075 Accepted: 2629
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: A, B, 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)

题目

题意:无向图。 起点为1, 终点为 n, 输出仅次于 最短路的次短路, 路径可重复走。

思路:正反求两遍最短路,得出1 到所有点的最短距离, 和 n到所有点的最短距离。因为路径可以重复走,所以两点之间只有一条边的可以重复经过。接着将每条边视作单项边,枚举每条单项边e(u, v),Dist = dist(1, u) + dist(n, v) + dist(u,v).这样只会重复一条最小边。所求得的 大于 dist(1, n)的最小的Dist,就是题目的解。(理解不了的画一下图就明白了)

AC代码:

 #include<cstdio>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<cstring>
#include<climits>
using namespace std;
#define maxn 100009
#define INF INT_MAX-100000
struct Edge
{
int from, to, dist;
}; struct Heapnode
{
int d, u;
bool operator < (const Heapnode& that) const {
return d > that.d;
}
}; int dist1[maxn], dist2[maxn];
struct Dijkstra
{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
int d[maxn];
int p[maxn]; void init(int n) {
this->n = n;
for(int i = ; i < n; i++) G[i].clear();
edges.clear();
} void AddEdge(int from, int to , int dist) {
edges.push_back((Edge) {from, to , dist});
m = edges.size();
G[from].push_back(m-);
} void dijkstra(int s, int* dx) {
priority_queue<Heapnode> Q;
for(int i = ; i < n; i++) dx[i] = d[i] = INF;
d[s] = dx[s] = ;
memset(done, , sizeof(done));
Q.push((Heapnode) {, s});
while(!Q.empty()) {
Heapnode x = Q.top(); Q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = true;
int size = G[u].size();
for(int i = ; i < size; i++) {
Edge& e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist) {
dx[e.to] = d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
Q.push ((Heapnode) {d[e.to], e.to});
}
}
}
}
}dij; void work(int n, int r)
{
dij.init(n+);
for(int i = ; i < r; i++){
int a, b, c; scanf("%d%d%d", &a, &b, &c);
dij.AddEdge(a, b, c);
dij.AddEdge(b, a, c);
}
dij.dijkstra(, dist1);
dij.dijkstra(n, dist2);
int Dist, sta = dist1[n], res = INF;
for(int i = ; i <= n; i++){
int ss = dij.G[i].size();
for(int j = ; j < ss; j++){
int m = dij.G[i][j];
Edge ee = dij.edges[m];
Dist = dist1[ee.from] + dist2[ee.to] + ee.dist;
if(Dist < res && Dist > dist1[n]) res = Dist;
//cout<<Dist<<" "<<res<<endl;
}
}
printf("%d\n", res);
}
int main()
{
int n, r;
while(scanf("%d%d", &n, &r) != EOF){
work(n, r);
}
return ;
}

poj-3255-Roadblocks-路径可重复次短路的更多相关文章

  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 Roadblocks 次短路(两次dijksta)

    Roadblocks Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

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

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

  5. POJ 3255 Roadblocks --次短路径

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

  6. POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16425   Accepted: 5797 Descr ...

  7. POJ 3255 Roadblocks (次短路 SPFA )

    题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...

  8. POJ 3255 Roadblocks (次短路)

    题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...

  9. 次最短路径 POJ 3255 Roadblocks

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

  10. poj 3255 Roadblocks

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

随机推荐

  1. Android 动态Tab分页效果实现

    当前项目使用的是TabHost+Activity进行分页,目前要做个报表功能,需要在一个Tab页内进行Activity的切换.比方说我有4个Tab页分别为Tab1,Tab2,Tab3,Tab4,现在的 ...

  2. 【Python】可变对象和不可变对象

    Python在heap中分配的对象分成两类:可变对象和不可变对象.所谓可变对象是指,对象的内容是可变的,例如list.而不可变的对象则相反,表示其内容不可变. 不可变对象:int,string,flo ...

  3. Facebook 和 Google 如何激发工程师的创造力

    原文链接:http://kb.cnblogs.com/page/193450/ 今天终于“朝圣”了两个伟大的公司——Facebook和Google,对创造力和驱动力的来源有了更多的理解,尤其是对于典型 ...

  4. 使用命令行进行 VS单元测试 MSTest

    测试 指定的方法 "D:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\MSTest.exe" /test ...

  5. CSS中常用的字体单位:px、em、rem和%的区别

    在刚接触CSS时,px用的比较多,也很好理解,可是用久了就会发现有些缺陷,特别是在做响应式开发的时候. 那这么多单位到底在什么时候用什么单位合适呢?今天就来探讨一下. 先大致解释一下这些单位的意思: ...

  6. BZOJ 1088

    真是智商不够, 智商题:.... 假如:第1,2个格子已知,然后根据第二列的情况,就可以把所有满足的情况推出来,又萌萌哒.. 无耻攒字数: #include<stdio.h> using ...

  7. zero to one:创业秘籍并不存在,因为任何创新都是新颖独特的,任何权威都不可能具体规定如何创新

    彼得·蒂尔(Peter Thiel)的新作<从0到1>从预售开始就占据美国亚马逊排行榜第一名的位置,被一批创业家和企业家评为“迄今为止最好的商业书”.这是一本关于如何创建创新公司的书,主要 ...

  8. 帝国cms数据还原后提示数据库表不存在怎么解决?

    下午,ytkah用帝国cms在wamp调试时发现了一个问题,还原备份好的数据后更新的页面提示数据库表不存在,查看了phpmyadmin分类的数据库表实际上是存在的,这个是怎么回事呢?重新搭建一个新站点 ...

  9. Ruby Profiler 详解之 stackprof

    简介 stackprof 是基于采样的一个调优工具,采样有什么好处呢?好处就是你可以线上使用,按照内置的算法抓取一部分数据,只影响一小部分性能.它会产生一系列的 dump 文件,然后你在线下分析这些文 ...

  10. HDU4871 Shortest-path tree(树分治)

    好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...