POJ 3255(次短路+SPFA)

来源:互联网 发布:苹果6s怎么设置4g网络 编辑:程序博客网 时间:2024/06/05 10:19
Roadblocks
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 15411 Accepted: 5427

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

题意:给出N个点和R条边,求1到N的次短路。

分析:首先,最短路很容易求,只要用SPFA跑一遍就可以了,然后至于次短路,肯定有一条边是比最短路中的边要长的,然后我们就是要枚举不在最短路中的最小边,这个最小边必定是出现在这R条边之内。从1开始跑一遍SPFA,再从N开始跑一边SPFA,枚举R条边的起始点,求出比最短路长的最小值。

代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<vector>#include<queue>using namespace std;int n,m;int d1[5005];int d2[5005];struct node{    int x,y,w;};struct Node{    int v,w;};vector<Node> vec[5005];void spfa(int flag,int cur){    queue<int> q;    q.push(cur);    int temp[5005];    memset(temp,0x3f3f3f3f,sizeof(temp));    int id[5005];    memset(id,0,sizeof(id));    temp[cur]=0;    id[cur]=1;    while(q.empty()==0){        int u=q.front();        int len=vec[u].size();        q.pop();        id[u]--;        for(int i=0;i<len;i++){            int v=vec[u][i].v;            if(temp[v]>temp[u]+vec[u][i].w){                temp[v]=temp[u]+vec[u][i].w;                if(id[v]==0){                    id[v]++;                    q.push(v);                }            }        }    }    if(flag==1){        for(int i=1;i<=n;i++){            d1[i]=temp[i];        }    }    else if(flag==2){        for(int i=1;i<=n;i++){            d2[i]=temp[i];        }    }}int main(){    //freopen("test.txt","r",stdin);    while(~scanf("%d%d",&n,&m)){        int u,v,w;;        node a[100005];        for(int i=1;i<=m;i++){            Node temp;            scanf("%d%d%d",&u,&v,&w);            temp.v=v;            temp.w=w;            vec[u].push_back(temp);            temp.v=u;            vec[v].push_back(temp);            a[i].x=u;            a[i].y=v;            a[i].w=w;        }        memset(d1,0x3f3f3f3f,sizeof(d1));        memset(d2,0x3f3f3f3f,sizeof(d2));        spfa(1,1);        spfa(2,n);        int ans=0x3f3f3f3f;        //printf("%d\n",d2[1]);        for(int i=1;i<=m;i++){            int u=a[i].x;            int v=a[i].y;            int w=a[i].w;            //printf("%d %d %d\n",u,v,w);            if(d1[u]+d2[v]+w>d1[n]){                ans=min(ans,d1[u]+d2[v]+w);            }            if(d2[u]+d1[v]+w>d1[n]){                ans=min(ans,d2[u]+d1[v]+w);            }        }        printf("%d\n",ans);    }    return 0;}

原创粉丝点击