Roadblocks

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 6
Problem 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: <i>N</i> and <i>R</i> <br>Lines 2..<i>R</i>+1: Each line contains three space-separated integers: <i>A</i>, <i>B</i>, and <i>D</i> that describe a road that connects intersections <i>A</i> and <i>B</i> and has length <i>D</i> (1 ≤ <i>D</i> ≤ 5000)
 
Output
Line 1: The length of the second shortest path between node 1 and node <i>N</i>
 
Sample Input
4 4 1 2 100 2 4 200 2 3 250 3 4 100
 
Sample Output
450
 
有n个路口r条马路,马路可以重复走,问从1号路口到n号路口的次短路
 

次短路问题,实际上可以这么理解:

在知道最短路的情况下,不走最短路,绕一段路,而且只能绕一段路,否则会不满足次短。

所以就先找到最短路并记录下路径,然后枚举最短路上的每一个点a,从这个点再绕一个点b,然后再加上点b到n的最短路。

所以我们需要知道从1到每个点的最短路,还需要知道从每个点到n的最短路,从每个点到n的最短路就是从n到每个点的最短路

所以两次dijkstra 然后枚举次短路就好啦

邻接矩阵居然超内存

 #include <cstring>
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define inf 0x3f3f3f3f
const int maxn = ;
using namespace std;
int n, m;
int d1[maxn];
int d2[maxn];
bool book[maxn];
struct edge
{
int to, c;
};
vector<edge> e[maxn];
//用邻接矩阵会超内存
void dijkstra(int s, int *d)
{
memset(d,inf, maxn * sizeof(int));
memset(book, , sizeof(book));
int i;
//for (i = 0; i < e[s].size(); i++) d[e[s][i].to] = e[s][i].c;
//book[s] = 1;
//不能直接赋值,也要进行比较,典型样例
//2 2
//1 2 100
//1 2 200
d[s] = ;
while ()
{
int k = -; int min = inf;
for ( i = ; i<= n;i++)
{
if (!book[i] && d[i] < min)
{
min = d[i];
k = i;
}
}
if (k == -) break; else
{
book[k] = ;
for (i=;i<e[k].size();i++)
{
if (d[e[k][i].to] > d[k] + e[k][i].c)
{
d[e[k][i].to] = d[k] + e[k][i].c;
} }
}
}
} int main()
{
int i;
cin >> n >> m;
for (i = ; i <= m; i++)
{
edge t, t1;
int k;
cin >> k >> t.to >> t.c;
t1.to = k;
t1.c = t.c;
e[k].push_back(t);//双向存。存一个点出发的多个目的地从k出发,目的地是t.to,花费t.c
e[t.to].push_back(t1);
} dijkstra(, d1);
dijkstra(n, d2);//某个点到n的最短路就是n到某个点的最短路
int k = n;
int ans = inf;
int minn = d1[n]; for (k=;k<=n;k++)
{
for (i = ; i<e[k].size(); i++)
{
int ee = d1[k] + e[k][i].c + d2[e[k][i].to];
if (ans>ee&&ee>minn)
{
ans = ee;
}
}
}
cout << ans << endl;
return ;
}

超内存代码且错误代码

 #include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
int e[][];
int d1[];
int d2[];
int m, n;
void dijkstra(int s, int *d)
{
int book[];
memset(book, , sizeof(book));
int i;
for (i = ; i <= n; i++)
{
d[i] = e[s][i];
}
book[] = ;
while ()
{
int min = inf;
int k = -;
for (i = ; i <= n; i++)
{
if (d[i] < min&&book[i]==)
{
min = d[i];
k = i;
}
}
if (k == -) break;
book[k] = ;
for (i = ; i <= n; i++)
{
if (book[i] == && d[i] > d[k] + e[k][i])
{
d[i] = d[k] + e[k][i];
}
}
}
}
int main()
{
int i, j;
scanf("%d %d", &m, &n);
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
if (i == j) e[i][j] = ;
else
e[i][j] = inf;
}
}
for (i = ; i <= n; i++)
{
int x, y, z;
scanf("%d %d %d", &x, &y,&z);
if (e[x][y] > z)
{
e[x][y] = z;
e[y][x] = z;
}
}
dijkstra(, d1);
dijkstra(n, d2);
int minn = d1[n];
int ans = inf;
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
if (i == j) continue;
if (e[i][j] == inf) continue;
if (d1[i] + e[i][j] + d2[j] > minn)
{
ans = min(ans, d1[i] + e[i][j] + d2[j]);
}
}
}
printf("%d\n", ans);
return ;
}

poj 3255 Roadblocks 次短路(两次dijksta)的更多相关文章

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

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

  2. POJ 3255 Roadblocks (次短路 SPFA )

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

  3. POJ 3255 Roadblocks (次短路)

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

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

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

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

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

  6. poj - 3225 Roadblocks(次短路)

    http://poj.org/problem?id=3255 bessie 有时会去拜访她的朋友,但是她不想走最快回家的那条路,而是想走一条比最短的路长的次短路. 城镇由R条双向路组成,有N个路口.标 ...

  7. 次最短路径 POJ 3255 Roadblocks

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

  8. poj 3255 Roadblocks

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

  9. POJ 3255 Roadblocks --次短路径

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

随机推荐

  1. 第一章连通性问题-----algorithm in C 读书笔记

    首先不得不吐槽一下翻译的质量,霍红卫....你给我站出来,不打死你,只想问你一下,你当年四级过了吗? 问题描述 输入两个整数,代表两个节点,如果这两个整数没有建立连接(这包括直接连接和通过其他节点连接 ...

  2. 使用shake.js让你博客支持摇一摇

    大家好,又到了随机文章的时间,请使用手机打开演示站点,然后像摇妹子一样摇晃手机,你会发现非常牛逼的事情,炫酷吧.该功能已经集成在Oconnor1.8中.本文主要讲解这货的原理. 首先需要下载shake ...

  3. 《Python》 函数嵌套、闭包和迭代器

    一.函数的嵌套: 1.函数的嵌套调用 def max2(x,y): m = x if x>y else y return m def max4(a,b,c,d): res1 = max2(a,b ...

  4. 软工作业NO.2小学生线上杨永信——四则运算题目生成

    项目题目:实现一个自动生成小学四则运算题目的命令行程序 github地址:https://github.com/a249970271/Formula 驾驶员:梁沛诗 副驾驶:曾祎祺 项目说明 自然数: ...

  5. Hibernate和Spring整合出现懒加载异常:org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    出现问题:  SSH整合项目里,项目目录结构如下: 在EmployeeAction.java的list()方法里将employees的list放入到request的Map中. EmployeeActi ...

  6. Codeforces Round #252 (Div. 2) D

    http://codeforces.com/problemset/problem/441/D 置换群的基本问题,一个轮换内交换成正常顺序需要k-1次,k为轮换内元素个数 两个轮换之间交换元素,可以把两 ...

  7. magento如何在首页显示产品

    1.首先现在magento后台创建一个新的分类,记下这个分类的 ID 号码.使用这个新建的分类来管理你的首页产品,这个分类设置为前台不可见.这样就不会显示在你的分类菜单中了,但使用代码调用的时候却会显 ...

  8. 关于Gson无法将匿名类转化为json字符串的问题

    在使用gson过程中,一般会将数据存在一个对象模型中,使用gson将模型转换成json字符串用于数据交互. 代码形如: ArrayList<String> list = new Array ...

  9. 【opencv基础】测量运行时间的函数getTickCount/getCPUTickCount/getTickFrequency

    函数的计算结果类型是double,单位是秒. 要使用更精确的计时,就需要使用getCPUTickCount(),不过现代计算机CPU的频率会随着负载而变化所以没大有必要使用该函数,可以参看函数的介绍[ ...

  10. git中的分支管理

    /*游戏或者运动才能让我短暂的忘记心痛,现如今感觉学习比游戏和运动还重要——曾少锋*/ 如果对git基础不太熟悉的可以参考:http://www.cnblogs.com/zengsf/p/750621 ...