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. tomcat用户配置,内存配置,pid配置

    一:tomcat用户配置 1.打开 webapps\manager\WEB-INF\web.xml 可以看到tomcat所有的角色名,后面我们就是需要配置这些角色 2.给用户添加角色 打开 conf/ ...

  2. RSA签名验证无法通过,检查以下部分

    RSA签名验证无法通过,检查以下部分:1.是否和上游交换公钥,提交给上游的公钥是否配置正确并生效2.检查加密方式是SHA1还是MD5,是否跟上游一致3.上游采用的是否是base64SafeUrl的方式 ...

  3. IOS消息推送(转)

    第一部分 首先第一步当然是介绍一下苹果的推送机制(APNS)咯(ps:其实每一篇教程都有),先来看一张苹果官方对其推送做出解释的概要图. Provider是给你手机应用发出推送消息的服务器,而APNS ...

  4. 怎么使用C++标准库来实现二维数组

    在编程里,像界面布局是二维的,那么常常使用二维数组来表示界面的元素,那么就需要使用二维的数组,在现在C++肯定是以标准库为基础了,不再使用C的二维数组,那么怎么样做呢?下面就使用vector来实现二维 ...

  5. Windows 7 64bit Python 2 Install

    安装 setuptools 出现 UnicodeDecodeError 文件 Lib/mimetypes.py 中的 bug, 通过以下 patch 修复: Index: Lib/mimetypes. ...

  6. memset,memcpy,strcpy

    http://www.cppblog.com/junfeng568/archive/2006/03/11/4022.html

  7. C语言 位移 速度 时间 Demo

    /************************************************************************* * C语言(s = v*t + a*t*t/2)D ...

  8. 【opencv基础】Rect类的神奇用法

    前言 最近看github上源码发现对两个cv::Rect使用相与(&)操作,猛地感觉自己蒙啦,Rect类还有这种神奇用法?!翻看opencv官网Rect类,果然如此! opencv中Rect类 ...

  9. 牛客小白月赛2 E:是是非非(尼姆博弈)

    链接:https://www.nowcoder.com/acm/contest/86/E来源:牛客网 题目描述 坎为水,险阳失道,渊深不测:离为火,依附团结,光明绚丽.坎卦:水洊至,习坎:君子以常德行 ...

  10. CH4101 银河英雄传说

    题意 4101 银河英雄传说 0x40「数据结构进阶」例题 描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展.  宇宙历七 ...