Roadblocks
Time Limit: 2000MS   Memory Limit: 65536K
     

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 intersectionN.

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 
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)
 
题意:求严格次短路
 
次短路一定是由最短路上出去经过其他的边在回到最短路上
所以先求出每个点到1的最短路dis1,每个点到n的最短路dis2
枚举每一条边
那么经过这条边u-->v的最短路为dis1[u]+w+dis2[v]
如果它大于最短路,更新答案
 
#include<queue>
#include<cstdio>
#include<cstring>
#define N 5001
#define M 100001
using namespace std;
int front[N],to[M<<],nxt[M<<],val[M<<],from[M<<],tot;
int dis1[N],dis2[N];
bool vis[N];
queue<int>q;
void add(int u,int v,int w)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; val[tot]=w; from[tot]=u;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; val[tot]=w; from[tot]=v;
}
void spfa(int s,int *dis)
{
//memset(dis,63,sizeof(dis));
memset(vis,false,sizeof(vis));
q.push(s);
vis[s]=true;
dis[s]=;
int now;
while(!q.empty())
{
now=q.front();
q.pop();
vis[now]=false;
for(int i=front[now];i;i=nxt[i])
if(dis[to[i]]>dis[now]+val[i])
{
dis[to[i]]=dis[now]+val[i];
if(!vis[to[i]])
{
vis[to[i]]=true;
q.push(to[i]);
}
}
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int u,v,w;
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
memset(dis1,,sizeof(dis1));
memset(dis2,,sizeof(dis2));
spfa(,dis1);
spfa(n,dis2);
int minn=dis1[n],tmp,ans=0x7fffffff;
for(int i=;i<=tot;i++)
{
u=from[i]; v=to[i];
tmp=dis1[u]+val[i]+dis2[v];
if(tmp>minn && tmp<ans) ans=tmp;
}
printf("%d",ans);
}

POJ 3255 Roadblocks (次短路模板)的更多相关文章

  1. POJ 3255 Roadblocks (次级短路问题)

    解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...

  2. poj 3255 Roadblocks 次短路(两次dijksta)

    Roadblocks Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  3. POJ 3255 Roadblocks (次短路 SPFA )

    题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...

  4. POJ 3255 Roadblocks (次短路)

    题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...

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

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

  6. POJ 2449Remmarguts' Date K短路模板 SPFA+A*

    K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...

  7. poj 2499第K短路模板

    第k*短路模板(单项边) #include <iostream> #include <cstdio> #include <algorithm> #include & ...

  8. poj - 3225 Roadblocks(次短路)

    http://poj.org/problem?id=3255 bessie 有时会去拜访她的朋友,但是她不想走最快回家的那条路,而是想走一条比最短的路长的次短路. 城镇由R条双向路组成,有N个路口.标 ...

  9. 次最短路径 POJ 3255 Roadblocks

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

随机推荐

  1. POJ 2986 A Triangle and a Circle(三角形和圆形求交)

    Description Given one triangle and one circle in the plane. Your task is to calculate the common are ...

  2. 第四次作业之psp

    psp 进度条 博文字数累积折线图 代码行数累积折线图 psp饼状图

  3. 用URL传参带特殊字符,特殊字符丢失

    文章:URL中编码URL特殊字符 文章:用URL传参带特殊字符,特殊字符丢失(encode) 如果url中有特殊字符,需要对url进行编码,否则特殊字符丢失,导致最终接收到的值不对.

  4. 修改IntelliJ IDEA字体

  5. Android OCR文字识别 实时扫描手机号(极速扫描单行文本方案)

    身份证识别:https://github.com/wenchaosong/OCR_identify 遇到一个需求,要用手机扫描纸质面单,获取面单上的手机号,最后决定用tesseract这个开源OCR库 ...

  6. NCAIOC

    NCAIOC Npm Cli All In One Client https://github.com/xgqfrms/NCAIOC https://cdn.xgqfrms.xyz/web-ide/i ...

  7. linux 安装 bitnamid-redmine

    Unix 和 Linux 安装 Perl Unix/Linux 系统上 Perl 安装步骤如下: 通过浏览器打开 http://www.perl.org/get.html. 下载适用于 Unix/Li ...

  8. lambda 分组练习

    public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<Person> per ...

  9. 前端基础:HTML标签(下)

    前端基础HTML标签(下) 1.表单 表单的功能主要用于向服务器传输数据,从而实现客户端与Web服务器的交互.表单能够包含input系列标签,比如:文本字段.复选框.单选按钮.提交按钮等:表单还包含t ...

  10. 【刷题】BZOJ 4830 [Hnoi2017]抛硬币

    Description 小A和小B是一对好朋友,他们经常一起愉快的玩耍.最近小B沉迷于**师手游,天天刷本,根本无心搞学习.但是已经入坑了几个月,却一次都没有抽到SSR,让他非常怀疑人生.勤勉的小A为 ...