首页 > 代码库 > POJ3255Roadblocks[次短路]

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 41 2 1002 4 2002 3 2503 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

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=5005,M=100005,INF=1e9;inline int read(){    char c=getchar();int x=0,f=1;    while(c<0||c>9){if(c==-)f=-1;c=getchar();}    while(c>=0&&c<=9){x=x*10+c-0;c=getchar();}    return x;}int n,m,u,v,w;struct edge{    int v,w,ne;}e[M<<1];int h[N],cnt=0;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][2],inq[N],q[N],head=1,tail=0;void spfa(){    for(int i=1;i<=n;i++){d[i][0]=d[i][1]=INF;}    d[1][0]=0; inq[1]=1; q[++tail]=1;    while(head<=tail){        int u=q[head++];//printf("u %d\n",u);        inq[u]=0;        for(int i=h[u];i;i=e[i].ne){            int v=e[i].v,w=e[i].w;            if(d[v][0]>d[u][0]+w){                d[v][1]=d[v][0];                d[v][0]=d[u][0]+w;                if(!inq[v]){inq[v]=1;q[++tail]=v;}            }else if(d[v][1]>d[u][0]+w&&d[v][0]<d[u][0]+w){                d[v][1]=d[u][0]+w;                if(!inq[v]){inq[v]=1;q[++tail]=v;}            }            if(d[v][1]>d[u][1]+w){                d[v][1]=d[u][1]+w;                if(!inq[v]){inq[v]=1;q[++tail]=v;}            }        }    }}int main(int argc, const char * argv[]) {    n=read();m=read();    for(int i=1;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}    spfa();    printf("%d",d[n][1]);    return 0;}

 

//dijkstra 63MS#include <iostream>#include <cstdio>#include <algorithm>#include <queue>#include <cstring>using namespace std;const int N=5005,M=100005,INF=1e9;inline int read(){    char c=getchar();int x=0,f=1;    while(c<0||c>9){if(c==-)f=-1;c=getchar();}    while(c>=0&&c<=9){x=x*10+c-0;c=getchar();}    return x;}int n,m,u,v,w;struct edge{    int v,w,ne;}e[M<<1];int h[N],cnt=0;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][2],vis[N][2];struct hn{    int u,d,p;    hn(int a=0,int b=0,int c=0):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=1;i<=n;i++) {d[i][0]=d[i][1]=INF;}    q.push(hn(1,0,0));    d[1][0]=0;    while(!q.empty()){        hn now=q.top();q.pop();        int u=now.u,p=now.p;         if(vis[u][p]) continue;        vis[u][p]=1;        for(int i=h[u];i;i=e[i].ne){            int v=e[i].v,w=e[i].w;            if(d[v][0]>d[u][p]+w){                d[v][1]=d[v][0];                d[v][0]=d[u][p]+w;                q.push(hn(v,d[v][0],0));                q.push(hn(v,d[v][1],1));            }else if(d[v][1]>d[u][p]+w){                d[v][1]=d[u][p]+w;                 q.push(hn(v,d[v][1],1));            }            }            }}int main(int argc, const char * argv[]) {    n=read();m=read();    for(int i=1;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}    dijkstra();    printf("%d",d[n][1]);    return 0;}

 

POJ3255Roadblocks[次短路]