poj 3255 Roadblocks 次短路(两次dijksta)

2023-02-25,,

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)的相关教程结束。

《poj 3255 Roadblocks 次短路(两次dijksta).doc》

下载本文的Word格式文档,以方便收藏与打印。