次短路
来源:互联网 发布:linux 安装apache命令 编辑:程序博客网 时间:2024/06/10 09:40
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
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
Sample Input
4 41 2 1002 4 2002 3 2503 4 100
Sample Output
450
Hint
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXX 5000+10
using namespace std;
struct point
{
int fr, to, c;
};
vector<point>g[MAXX];
vector<point>p;
int vis[MAXX];
int n, r;
void dijk(int u, int* dis)
{
memset(vis, 0, sizeof(vis));
for (int i=0; i<=n; i++)
dis[i] = INF;
dis[u] = 0;
for (int i=0; i<n; i++)
{
int minn = INF, mark = -1;
for (int j=1; j<=n; j++)
{
if (dis[j] < minn && !vis[j])
{
minn = dis[j];
mark = j;
}
}
if (mark == -1)
break;
vis[mark] = 1;
for (int j=0; j<g[mark].size(); j++)
{
point t=g[mark][j];
if (dis[t.to] > dis[mark]+t.c)
dis[t.to] = dis[mark]+t.c;
}
}
}
int main()
{
// freopen("in.txt", "r", stdin);
while (scanf("%d%d", &n, &r)!=EOF)
{
int rdis[MAXX], ldis[MAXX];
for (int i=0; i<r; i++)
{
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
g[u].push_back(point{u, v, c});
g[v].push_back(point{v, u, c});
p.push_back(point{u, v, c});
p.push_back(point{v, u, c});
}
dijk(1, ldis);
dijk(n, rdis);
int minn = INF;
for (int i=0; i<p.size(); i++)
{
int ans = p[i].c+ldis[p[i].fr]+rdis[p[i].to];
if (ans > ldis[n] && ans < minn)
minn = ans;
}
cout<<minn<<endl;
for (int i=1; i<=n; i++)
g[i].clear();
p.clear();
}
return 0;
}
- 次短路
- 次短路
- 次短路
- 次短路
- 次短路
- 次短路
- 最短路 & 次短路
- poj3463 最短路+次短路
- 最短路及次短路
- 次短路与k短路
- hdu 3191 次短路
- POJ 3225 次短路
- 【次短路】赛跑
- 次短路--poj3255
- POJ3255 Roadblocks , 次短路
- POJ3255 Roadblocks(次短路)
- poj 3255(次短路)
- 次短路问题
- MAC上配置code review
- GridView交替行颜色
- Android 6.0指纹识别App开发demo
- iOS开发从入门到精通-- 滚动视图UIScrollView
- qt 之QToolButton
- 次短路
- redo和undo
- TensorFlow安装及注意事项
- WCF服务与调用服务端(移动端)联调VS设置
- POJ 3258 River Hopscotch (最大化最小值)二分
- Android开发之View与Layout XML文件之间的理解
- 什么是事件委托
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- Makefile (十一) 使用make 更新函数库文件