http://poj.org/problem?id=3255

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15680   Accepted: 5510

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

 
 
mmp、、、无向图,双向边!!啊啊啊
 #include <algorithm>
#include <cstdio>
#include <queue> using namespace std; const int INF(0x3f3f3f3f);
const int N(+);
const int M(+); int hed[N],had[N],sumedge;
struct Edge
{
int v,next,w;
}edge1[M],edge2[M];
inline void ins(int u,int v,int w)
{
edge1[++sumedge].v=v;
edge1[sumedge].next=hed[u];
edge1[sumedge].w=w;
hed[u]=sumedge;
edge2[sumedge].v=u;
edge2[sumedge].next=had[v];
edge2[sumedge].w=w;
had[v]=sumedge; edge1[++sumedge].v=u;
edge1[sumedge].next=hed[v];
edge1[sumedge].w=w;
hed[v]=sumedge;
edge2[sumedge].v=v;
edge2[sumedge].next=had[u];
edge2[sumedge].w=w;
had[u]=sumedge;
} int dis[N];
bool inq[N];
void SPFA(int s)
{
for(int i=;i<s;i++) dis[i]=INF;
dis[s]=; inq[s]=;
queue<int>que; que.push(s);
for(int u,v;!que.empty();)
{
u=que.front(); que.pop(); inq[u]=;
for(int i=had[u];i;i=edge2[i].next)
{
v=edge2[i].v;
if(dis[v]>dis[u]+edge2[i].w)
{
dis[v]=dis[u]+edge2[i].w;
if(!inq[v]) que.push(v),inq[v]=;
}
}
}
} struct Node
{
int now,g;
bool operator < (Node a) const
{
return a.g+dis[a.now]<g+dis[now];
}
};
int Astar(int s,int t,int k)
{
priority_queue<Node>que;
int cnt=; Node u,v;
u.g=; u.now=s;
que.push(u);
for(;!que.empty();)
{
u=que.top(); que.pop();
if(u.now==t) cnt++;
if(cnt==k) return u.g;
for(int i=hed[u.now];i;i=edge1[i].next)
{
v.now=edge1[i].v;
v.g=u.g+edge1[i].w;
que.push(v);
}
}
return ;
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
// freopen("block.in","r",stdin);
// freopen("block.out","w",stdout); int n,m; read(n),read(m);
for(int v,u,w;m--;)
read(u),read(v),read(w),ins(u,v,w);
SPFA(n); printf("%d\n",Astar(,n,));
return ;
} int I_want_AC=AC();
int main(){;}

Astar AC

次短路正经做法:

SPFA跑出从1到i和从n到i的dis,枚举每条不在最短路上的边,更新ans

 #include <algorithm>
#include <cstdio>
#include <queue> using namespace std; const int INF(0x3f3f3f3f);
const int N(+);
const int M(+); int m,n,head[N],sumedge;
struct Edge
{
int v,next,w;
Edge(int v=,int next=,int w=):
v(v),next(next),w(w){}
}edge[M<<];
inline void ins(int u,int v,int w)
{
edge[++sumedge]=Edge(v,head[u],w);
head[u]=sumedge;
} bool inq[N];
int d1[N],d2[N];
inline void SPFA(int s,int *dis)
{
for(int i=;i<=n;i++)
inq[i]=,dis[i]=INF;
dis[s]=; inq[s]=;
queue<int>que; que.push(s);
for(int u,v;!que.empty();)
{
u=que.front(); que.pop(); inq[u]=;
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
if(!inq[v]) que.push(v),inq[v]=;
}
}
}
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
freopen("block.in","r",stdin);
freopen("block.out","w",stdout); read(n),read(m);
for(int v,u,w;m--;ins(u,v,w),ins(v,u,w))
read(u),read(v),read(w);
SPFA(,d1); SPFA(n,d2);
int ans=INF,tmp;
for(int i,v,u=;u<=n;u++)
{
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
tmp=d1[u]+d2[v]+edge[i].w;
if(tmp>d1[n]&&ans>tmp) ans=tmp;
}
}
printf("%d\n",ans);
return ;
} int I_want_AC=AC();
int main(){;}

SPFA 跑次短路

堆优化的Dijkstra

用两个数组记录到当前点的最小值d1[n]和次小值d2[n],注意d2[s]=INF而不是0

 #include <algorithm>
#include <cstdio>
#include <queue> using namespace std; const int INF(0x3f3f3f3f);
const int N(+);
const int M(+); int m,n,head[N],sumedge;
struct Edge
{
int v,next,w;
Edge(int v=,int next=,int w=):
v(v),next(next),w(w){}
}edge[M<<];
inline void ins(int u,int v,int w)
{
edge[++sumedge]=Edge(v,head[u],w);
head[u]=sumedge;
} struct Node
{
int now,dis;
bool operator < (const Node &x) const
{
return dis>x.dis;
}
}; bool vis[N];
int d1[N],d2[N];
inline void Dijkstar()
{
for(int i=;i<=n;i++) d1[i]=d2[i]=INF;
priority_queue<Node>que; Node u,to;
u.dis=d1[]=; vis[]=;
u.now=; que.push(u);
for(int dis,v;!que.empty();)
{
u=que.top();que.pop();
if(u.dis>d2[u.now]) continue;
for(int i=head[u.now];i;i=edge[i].next)
{
v=edge[i].v;
dis=u.dis+edge[i].w;
if(dis<d1[v])
{
swap(dis,d1[v]);
to.now=v;
to.dis=d1[v];
que.push(to);
}
if(dis>d1[v]&&dis<d2[v])
{
d2[v]=dis;
to.dis=d2[v];
to.now=v;
que.push(to);
}
}
}
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
#define MINE
#ifdef MINE
freopen("block.in","r",stdin);
freopen("block.out","w",stdout);
#endif read(n),read(m);
for(int v,u,w;m--;ins(u,v,w),ins(v,u,w))
read(u),read(v),read(w);
Dijkstar();
printf("%d\n",d2[n]);
return ;
} int I_want_AC=AC();
int main(){;}

Dijkstra AC

POJ——T 3255 Roadblocks|| COGS——T 315. [POJ3255] 地砖RoadBlocks || 洛谷—— P2865 [USACO06NOV]路障Roadblocks的更多相关文章

  1. 洛谷——P2865 [USACO06NOV]路障Roadblocks

    P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...

  2. 洛谷P2865 [USACO06NOV]路障Roadblocks——次短路

    给一手链接 https://www.luogu.com.cn/problem/P2865 这道题其实就是在维护最短路的时候维护一下次短路就okay了 #include<cstdio> #i ...

  3. 络谷 P2865 [USACO06NOV]路障Roadblocks

    P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...

  4. BZOJ 1726 洛谷 2865 [USACO06NOV]路障Roadblocks【次短路】

    ·求1到n的严格次短路. [题解] dijktra魔改?允许多次入队,改了次短路的值也要入队. #include<cstdio> #include<algorithm> #de ...

  5. P2865 [USACO06NOV]路障Roadblocks

    P2865 [USACO06NOV]路障Roadblocks 最短路(次短路) 直接在dijkstra中维护2个数组:d1(最短路),d2(次短路),然后跑一遍就行了. attention:数据有不同 ...

  6. 洛谷P2865 [USACO06NOV]Roadblocks G(次短路)

    一个次短路的问题,可以套用dijkstra求最短路的方法,用dis[0][i]表示最短路:dis[1][i]表示次短路,优先队列中存有最短路和次短路,然后每次找到一条道路对他进行判断,更新最短或次短路 ...

  7. cogs 315. [POJ3255] 地砖RoadBlocks

    315. [POJ3255] 地砖RoadBlocks ★★★   输入文件:block.in   输出文件:block.out   简单对比时间限制:1 s   内存限制:128 MB Descri ...

  8. P2865 【[USACO06NOV]路障Roadblocks】(次短路)

    传送门 算法Dijkstra要求次短路 那么在不考虑重复走一条边的情况下 肯定是把最短路中的一段改成另一段 至少要换另一条边到路径里所以可以枚举所有不属于最短路的每条边(a,b) 那么dis(1,a) ...

  9. 【洛谷 P2865】 [USACO06NOV]路障Roadblocks(最短路)

    题目链接 次短路模板题. 对每个点记录最短路和严格次短路,然后就是维护次值的方法了. 和这题一样. #include <cstdio> #include <queue> #in ...

随机推荐

  1. Epplus做Excel的数据透视

    //表格的范围需要自己定义 var epplus = new ExcelPackage(); var sheet = epplus.Workbook.Worksheets.Add("Shee ...

  2. Net Core链接数据库

    原文 在Windows下,相信大家分分钟都可以搞定.而初次接触.net core + mysql可能需要注意些细节.首先打开vs2017新建一个asp.net core项目(选Web应用程序), 然后 ...

  3. shell-4.bash的变量:用户自定义变量

    目录 内容

  4. Dropout 上

    From <白话深度学习与TensorFlow> Dropout 顾名思义是“丢弃”,在一轮训练阶段丢弃一部分网络节点,比如可以在其中的某些层上临时关闭一些节点,让他们既不输入也不输出,这 ...

  5. ActiveMQ服务安装

    1.下载安装ActiveMQ服务提供者 http://activemq.apache.org/ 2.启用ActiveMQ服务 cd [activemq_install_dir] bin\activem ...

  6. How to customize Skin Gallery - Remove / rename skins and groups

    1. REMOVE (HIDE) A SPECIFIC SKIN Traverse through the gallery group collection, then through its gal ...

  7. C++primer书店程序

    #include <iostream> #include <string> #include <cassert> #include <algorithm> ...

  8. Android数据分批载入-滑动究竟部自己主动载入列表

    Android数据分批载入-滑动究竟部自己主动载入列表 2014年5月9日 摘自:<Android高级开发实战-ui.ndk与安全> 本博文介绍怎样进行数据分批载入,在应用开发其中会常常使 ...

  9. Android清单文件具体解释(六) ---- &lt;activity&gt;节点的属性

    1.android:allowTaskReparenting android:allowTaskReparenting是一个任务调整属性,它表明当这个任务又一次被送到前台时,该应用程序所定义的Acti ...

  10. Invalid property 'sentinels' of bean class redis spring 错误修改

    /* * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Vers ...