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. struts2 实现rest

    参考链接https://www.ibm.com/developerworks/cn/java/j-lo-struts2rest/

  2. Struts2内置校验器——完整实例代码

    一.校验器的配置风格 1.字段校验器: <field name="被校验的字段"> <field-validator type="校验器名"& ...

  3. 网页不能显示PNG验证码的解决办法

    解决方法: 开始->运行,在运行输入框中输入 “regsvr32 c:\windows\system32\pngfilt.dll”(然后点击确定)如果在注册时出现 “已加载c:\windows\ ...

  4. memcached集群安装与测试

    1.上传安装包 libevent-2.0.22-stable.tar.gz memcached-1.4.24.tar.gz 2.安装libevent 创建目录 mkdir -p /apps/insta ...

  5. 使用 Git & Repo 下载代码

    客户端安装 Git 安装 git,gitk 网络连接正常的情况下: $ sudo apt-get install git-core gitk git-gui 不能上网,有.deb安装包的,请执行: $ ...

  6. STM32中断定时,控制LED灯

    #include "led.h" void TIM3_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBas ...

  7. BZOJ1095: [ZJOI2007]Hide 捉迷藏【线段树维护括号序列】【思维好题】

    Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩 捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条 ...

  8. 《DSP using MATLAB》 Problem 3.22

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  9. python type metaclass

    在python中一切皆对象, 所有类的鼻祖都是type, 也就是所有类都是通过type来创建. 传统创建类 class Foo(object): def __init__(self,name): se ...

  10. day3 自动部署安装软件到其他的机器设备上

    PS:原理是在本机创建boot.sh指向每一台主机,使用脚本命令去执行,然后就会自动安装软件 PS:boot.sh里面放着1.免密登录 2.发送每台机器install.sh 这个install.sh中 ...