隐藏页面特效

poj 3255 Roadblocks

Roadblocks

Time Limit: 2000MS

 

Memory Limit: 65536K

Total Submissions: 13216

 

Accepted: 4660

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)

Source

USACO 2006 November Gold

 

题意:

给出n个点,m条双向边,求严格次短路。

AC代码:

#include<cstdio> #include<cstring> #include<queue> #define R register using namespace std; inline int read(){ R int x=0;bool f=1; R char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return f?x:-x; } const int N=1e5+10; struct node{ int u,v,w,next; }e[N<<1]; int n,m,tot,head[N],dis1[N],dis2[N]; bool vis[N]; void add(int x,int y,int z){ e[++tot].u=x; e[tot].v=y; e[tot].w=z; e[tot].next=head[x]; head[x]=tot; } void spfa1(int S){ queue<int>q; memset(vis,0,sizeof vis); memset(dis1,127/3,sizeof dis1); q.push(S); dis1[S]=0;vis[S]=1; while(!q.empty()){ int x=q.front();q.pop(); vis[x]=0; for(int i=head[x];i;i=e[i].next){ int v=e[i].v,w=e[i].w; if(dis1[v]>dis1[x]+w){ dis1[v]=dis1[x]+w; if(!vis[v]){ vis[v]=1; q.push(v); } } } } } void spfa2(int S){ queue<int>q; memset(vis,0,sizeof vis); memset(dis2,127/3,sizeof dis2); q.push(S); dis2[S]=0;vis[S]=1; while(!q.empty()){ int x=q.front();q.pop(); vis[x]=0; for(int i=head[x];i;i=e[i].next){ int v=e[i].v,w=e[i].w; if(dis2[v]>dis2[x]+w){ dis2[v]=dis2[x]+w; if(!vis[v]){ vis[v]=1; q.push(v); } } } } } void Cl(){ tot=0; memset(e,0,sizeof e); memset(head,0,sizeof head); } void work(){ Cl(); for(int i=1,x,y,z;i<=m;i++){ x=read();y=read();z=read(); add(x,y,z); add(y,x,z); } spfa1(1); spfa2(n); int shortest=dis1[n],shorter=0x7fffffff; for(int i=1;i<=m*2;i++){ int len=dis1[e[i].u]+dis2[e[i].v]+e[i].w; if(len>shortest&&len<shorter) shorter=len; } printf("%d\n",shorter); } int main(){ while(scanf("%d%d",&n,&m)==2) work(); return 0; }

 

 

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/6059319.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(269)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示