POJ-3255-次短路問題

來源:互聯網
上載者:User

標籤:圖論   次短路   poj   

Roadblocks
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7149   Accepted: 2663

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 andR
Lines 2..R+1: Each line contains three space-separated integers: A,B, and D that describe a road that connects intersections A andB and has length D (1 ≤ D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and nodeN

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

本題目的意思就是要求次短路。我用兩種方法求解:
(一)利用dijkstra演算法進行適當修改,到某個頂點v的次短路:(1)其他某個頂點u的最短路加上u->v的邊長;(2)其他某個頂點u的次短路加上u->v的邊長。所以我們要求出到所有
           頂點的最短路和次短路。因此對每個頂點,同時記錄最短距離和次短距離(開兩個數組記錄,用dijkstra演算法不斷更新)。
#include<queue>#include<vector>#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int INF=1000000000;const int max_e=200000+5;typedef pair<int,int>P;struct edge{    int to,dis;    edge(int to,int dis){        this -> to = to;        this -> dis = dis;    }};int N,R;int a,b,c;int dis[5005];         //記錄最短路徑int disc[5005];       //記錄次短路徑vector<edge>G[max_e];void dijkstra(){    fill(dis+1,dis+N+1,INF);    fill(disc+1,disc+N+1,INF);    priority_queue<P,vector<P>,greater<P> >q;    dis[1]=0;    q.push(P(0,1));    while(q.size()){        P p=q.top();        q.pop();        int dd=p.first;        int v=p.second;        if(disc[v]<dd) continue;        for(int i=0;i<G[v].size();i++){            edge& e=G[v][i];            int d=dd+e.dis;            if(dis[e.to]>d){                swap(dis[e.to],d);                q.push(P(dis[e.to],e.to));            }            if(disc[e.to]>d&&dis[e.to]<d){                   disc[e.to]=d;                q.push(P(disc[e.to],e.to));            }        }    }    cout<<disc[N]<<endl;}int main(){    scanf("%d%d",&N,&R);    for(int i=1;i<=R;i++){        scanf("%d%d%d",&a,&b,&c);        G[a].push_back(edge(b,c));        G[b].push_back(edge(a,c));    }    dijkstra();    return 0;}

(二)一個巧妙的方法,利用dijkstra雙向求出起點到每一點的最短路,以及每一點到終點的最短路徑,然後枚舉每一條邊,就可以得到次短路了。此方法也可以拓展成求第K短
           路問題,不過要注意時間複雜度。
#include<queue>#include<vector>#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int INF=1000000000;const int max_e=200000+5;typedef pair<int,int>P;struct edge{    int to,dis;    edge(int to,int dis){        this -> to = to;        this -> dis = dis;    }};struct dd{    int x,y;    int sum;}an[200005];int N,R;int a,b,c;int d1[5005];int d2[5005];vector<edge>G[max_e];void dijkstra(int dis[],int s){    fill(dis+1,dis+N+1,INF);    priority_queue<P,vector<P>,greater<P> >q;    dis[s]=0;    q.push(P(0,s));    while(q.size()){        P p=q.top();        q.pop();        int v=p.second;        if(dis[v]<p.first) continue;        for(int i=0;i<G[v].size();i++){            edge& e=G[v][i];            if(dis[e.to]>dis[v]+e.dis){                dis[e.to]=dis[v]+e.dis;                q.push(P(dis[e.to],e.to));            }        }    }}int main(){    int K=0;    scanf("%d%d",&N,&R);    for(int i=1;i<=R;i++){        scanf("%d%d%d",&a,&b,&c);        G[a].push_back(edge(b,c));        G[b].push_back(edge(a,c));        an[K].x=a; an[K].y=b; an[K].sum=c; K++;        an[K].x=b; an[K].y=a; an[K].sum=c; K++;    }    dijkstra(d1,1);    dijkstra(d2,N);    int ans=INF;    //cout<<ans<<" "<<d1[N]<<endl;    for(int i=1;i<=K;i++){        int aa=d1[an[i].x]+d2[an[i].y]+an[i].sum;        if(aa>d1[N]){            ans=min(ans,aa);        }    }cout<<ans<<endl;    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.