一、题目

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: N and R

Line 1: Two space-separated integers: N and R 

Lines 2..R+1: Each line contains three space-separated integers: AB, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and node N

Sample Input

4 4
1 2 100
2 4 200
2 3 250
3 4 100

Sample Output

450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

二、思路&心得

  • 定义权值的最大值时,尽可能大,综合题目的条件,比如这题的MAX_D就应该大于两倍的D最大值
  • 注意题目条件:比如这题存在自环边、双向边,同一条道路可以经过
  • 图论中,注意根据题目图的特点选择对应的算法,比如这题为稀疏图,应选择邻接表进行表示图

三、代码

#include<cstdio>
#include<vector>
#include<algorithm>
#define MAX_SIZE 5005
#define MAX_D 10005
using namespace std; struct edge {
int to, cost;
}; vector<edge> G[MAX_SIZE]; int N, R;
int dist[MAX_SIZE];
int dist2[MAX_SIZE];
int visit[MAX_SIZE];
int visit2[MAX_SIZE]; void Dijkstra(int s) {
dist[s] = 0;
while (true) {
int min_D = MAX_D;
int index;
int d, flag = 0;
for (int j = 1; j <= N; j ++) {
if (!visit[j] && dist[j] < min_D) {
min_D = dist[j];
index = j;
flag = 1;
} else if (!visit2[j] && dist2[j] < min_D) {
min_D = dist[j];
index = j;
flag = 2;
}
} if (!flag) break;
else if (flag == 1) {
visit[index] = 1;
d = dist[index];
}
else if (flag == 2) {
visit2[index] = 1;
d = dist2[index];
} for (int k = 0; k < G[index].size(); k ++) {
edge e = G[index][k];
int temp = e.cost + d;
if (!visit[e.to] && temp < dist[e.to]) {
swap(dist[e.to], temp);
}
if (dist[e.to] < temp && temp < dist2[e.to]) {
dist2[e.to] = temp;
visit2[e.to] = 0;
}
}
}
printf("%d\n", dist2[N]);
} void solve() {
for (int i = 1; i <= N; i ++) {
visit[i] = 0;
visit2[i] = 1;
dist[i] = MAX_D;
dist2[i] = MAX_D;
G[i].clear();
}
int from;
edge e;
for (int i = 1; i <= R; i ++) {
scanf("%d %d %d", &from, &e.to, &e.cost);
G[from].push_back(e);
swap(from, e.to);
G[from].push_back(e);
}
Dijkstra(1);
} int main() {
while (~scanf("%d %d", &N, &R)) {
solve();
}
return 0;
}

【图论】POJ-3255 次短路径的更多相关文章

  1. POJ - 3255 次短路径

    题意:给你无向带权图,求次短路径 题解:加一个次短路的数组,用于距记录源点到此点的次短路长度,注意初始化是源点到自己的次短路是极大值 接着再使用dijkstra算法,它是每次选用现在连上(记录了)的点 ...

  2. POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16425   Accepted: 5797 Descr ...

  3. POJ - 3255 SPFA+邻接表求次短路径

    题意:给出m条边 , n个顶点,u [ i ]到v [ i ] 的距离w [ i ],求除了最短路的那条最短的边的长度. 思路:之前有做过相似的题,使用迪杰斯特拉算法求单源最短路径,并且记录路径,枚举 ...

  4. 学习笔记CB006:依存句法、LTP、n元语法模型、N-最短路径分词法、由字构词分词法、图论、概率论

    依存句法分析,法国语言学家L.Tesniere1959年提出.句法,句子规则,句子成分组织规则.依存句法,成分间依赖关系.依赖,没有A,B存在错误.语义,句子含义. 依存句法强调介词.助词划分作用,语 ...

  5. 单源次短路径:poj:3255-Roadblocks

    Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17521 Accepted: 6167 Descripti ...

  6. 次最短路径 POJ 3255 Roadblocks

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

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

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

  8. MST:Roadblocks(POJ 3255)

       路上的石头 题目大意:某个街区有R条路,N个路口,道路双向,问你从开始(1)到N路口的次短路经长度,同一条边可以经过多次. 这一题相当有意思,现在不是要你找最短路径,而是要你找次短路经,而且次短 ...

  9. poj 3255 求次大最短路

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5508   Accepted: 2088 Descri ...

随机推荐

  1. ACM1001:Sum Problem

    Problem Description In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.   Input ...

  2. CF 1110 E. Magic Stones

    E. Magic Stones 链接 题意: 给定两个数组,每次可以对一个数组选一个位置i($2 \leq i \leq n - 1$),让a[i]=a[i-1]+a[i+1]-a[i],或者b[i] ...

  3. angularjs 常用方法

    一 angular的copy和extend 1.angular.extend() angular.extend():依次将第二个参数及后续的参数的第一层属性(不管是简单的属性还是对象)拷贝,赋给第一个 ...

  4. Python之面向对象的组合、多态、菱形问题、子类中重用父类的两种方式

    一.组合 ''' 1.什么是组合 组合就是一个类的对象具备某一个属性,该属性的值是指向另外一个类的对象 2.为何用组合 组合也是用来解决类与类直接代码冗余问题的 3.如何用组合 ''' # 继承减少代 ...

  5. 我用 Python 爬取微信好友,最后发现一个大秘密

    前言 你身处的环境是什么样,你就会成为什么样的人.现在人们日常生活基本上离不开微信,但微信不单单是一个即时通讯软件,微信更像是虚拟的现实世界.你所处的朋友圈是怎么样,慢慢你的思想也会变的怎么样.最近在 ...

  6. ipa包兼容性大作战!WeTest iOS深度兼容测试全新升级

    2018年,移动端适配话题热闹无比,有iOS新版本新机型发布,全面屏.异形屏.曲面屏争相斗艳,从而产生了各类特殊的屏幕分辨率设备. 正是因为这些特殊分辨率,导致2018年手机设备频繁出现适配问题,如屏 ...

  7. Web开发框架趋势

    Node.js增长很快,已经冒尖了 ASP.NET MVC 发展平稳(平稳很重要) Spring MVC沾着Spring的光,渐渐超越了Struts 2 Struts作为一个整体(Struts 1 和 ...

  8. 17-使用公共 Registry

    Docker Hub 是 Docker 公司维护的公共 Registry.用户可以将自己的镜像保存到 Docker Hub 免费的 repository 中.如果不希望别人访问自己的镜像,也可以购买私 ...

  9. 六边形地图Cube coordinates理解

    1.这个是 Axial coordinates,可以实现六边形4个方向上的移动 2.但是六边形还有两个方向需要移动,所以引入了Cube coordinates,这个坐标系多了一个轴向,Y轴,X轴沿水平 ...

  10. PHP的学习路线规划

    第一阶段:WEB的快速入门 前期入门学习我们需要学一些HTML+CSS+JS前端的一些技术,这个阶段不需要太深入的学习,学习到可以制作出一个像样点的静态页面就可以了.因为大家是学习PHP,对于新人来说 ...