POJ3255Roadblocks[次短路]

Roadblocks
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12697   Accepted: 4491

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


d[u][0]和d[u][1]分别最短路和次短路
更新时类似DP求树的直径
spfa:有一个点的最短路或次短路更新了,把这个店加进去
dijkstra:hn结构体中多一个p,0最短路,1次短路
 
//spfa 32MS NO.1
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int N=,M=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,u,v,w;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int d[N][],inq[N],q[N],head=,tail=;
void spfa(){
for(int i=;i<=n;i++){d[i][]=d[i][]=INF;}
d[][]=; inq[]=; q[++tail]=;
while(head<=tail){
int u=q[head++];//printf("u %d\n",u);
inq[u]=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(d[v][]>d[u][]+w){
d[v][]=d[v][];
d[v][]=d[u][]+w;
if(!inq[v]){inq[v]=;q[++tail]=v;}
}else if(d[v][]>d[u][]+w&&d[v][]<d[u][]+w){
d[v][]=d[u][]+w;
if(!inq[v]){inq[v]=;q[++tail]=v;}
}
if(d[v][]>d[u][]+w){
d[v][]=d[u][]+w;
if(!inq[v]){inq[v]=;q[++tail]=v;}
}
}
}
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}
spfa();
printf("%d",d[n][]);
return ;
}
//dijkstra 63MS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int N=,M=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,u,v,w;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int d[N][],vis[N][];
struct hn{
int u,d,p;
hn(int a=,int b=,int c=):u(a),d(b),p(c){}
bool operator < (const hn &rhs)const{return d>rhs.d;}
};
priority_queue<hn> q;
void dijkstra(){
for(int i=;i<=n;i++) {d[i][]=d[i][]=INF;}
q.push(hn(,,));
d[][]=;
while(!q.empty()){
hn now=q.top();q.pop();
int u=now.u,p=now.p;
if(vis[u][p]) continue;
vis[u][p]=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(d[v][]>d[u][p]+w){
d[v][]=d[v][];
d[v][]=d[u][p]+w;
q.push(hn(v,d[v][],));
q.push(hn(v,d[v][],));
}else if(d[v][]>d[u][p]+w){
d[v][]=d[u][p]+w;
q.push(hn(v,d[v][],));
}
}
}
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}
dijkstra();
printf("%d",d[n][]);
return ;
}
上一篇:swagger学习


下一篇:Delphi TreeView 节点上下移动