POJ3255 Roadblocks 次短路

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

USACO 2006 November Gold




两种做法,一种简单但是慢,一种难理解但是快。



第一种:假设a到e的最短路为a->b->c->d->e    那么次短路一定是在某个位置断开,之后走的另一条路。比如有一个单独的点F,c->F,F->d.  那么次短路可能就是a->b->c->F->d->e。  所以我们枚举每一条边(x->y),求dist(1,x)+dist(y,n)+dist(x,y)这个值的第二小的值即可。


第二种是普通的dij求最短路,用dist1存1到任何点的最短路。  之后我们再准备一个dist2存次小路。什么条件呢?到点x时,如果长度小于dist1[x],放入dist1中。  如果大于dist1[x](最短路),还小于dist2[x](次短路)那么放入dist2当中。


方法一

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 5000 + 10;
#define INF 0x3f3f3f3f
int n,m;
int edge_num;
int head[maxn];
int d1[maxn],d2[maxn];
struct Edge
{
    int to,w,next;
}edge[200000 + 10];




void Init()
{
    memset(head,-1,sizeof(head));
    edge_num = 0;
    memset(d1,INF,sizeof(d1));
    memset(d2,INF,sizeof(d2));


}


void add_edge(int x,int y,int z)
{
    edge[edge_num].to=y;
    edge[edge_num].w=z;
    edge[edge_num].next=head[x];
    head[x] = edge_num ++;


    edge[edge_num].to=x;
    edge[edge_num].w=z;
    edge[edge_num].next=head[y];
    head[y] = edge_num ++;
}
bool vis[maxn];
void spfa(int s,int d[])
{
    memset(vis,false,sizeof(vis));


    d[s] = 0;
    queue<int>q;
    q.push(s);
    vis[s] = true;
    while(!q.empty())
    {
        int u = q.front();q.pop();
        vis[u] = false;
        for(int i = head[u];i != -1; i = edge[i].next)
        {
            int v = edge[i].to,w = edge[i].w;
            if(d[v] > d[u] + w)
            {
                d[v] = d[u] + w;
                if(!vis[v])
                    vis[v] = true,q.push(v);
            }
        }
    }
}
void solve()
{
    int ans = INF;
    for(int i = 1; i <= n; i ++)
    {
        for(int j = head[i];j != -1; j = edge[j].next)
        {
            int v = edge[j].to,w = edge[j].w;
            if(d1[i] + d2[v] + w > d1[n])
                ans = min(ans,d1[i] + d2[v] + w);
        }
    }
    printf("%d\n",ans);
}
int main()
{
    while( scanf("%d%d",&n,&m)!=EOF)
    {


        Init();


        while(m --)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add_edge(x,y,z);


        }
        spfa(1,d1);
        spfa(n,d2);
        solve();
    }
    return 0;
}



方法二
include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <vector>  
#include <queue>  
#define INF 0x7fffffff  
using namespace std;  
typedef pair<int,int> P;  
struct edge{  
    int to,v;  
    edge(int to,int v):to(to),v(v){}  
    edge(){}  
};  
const int maxn = 5005;  
const int maxe = 100005;  
int V,E;  
vector<edge> g[maxn];  
int d[maxn],d2[maxn];//最短距离和次短距离  
void dijkstra(int s)  
{  
    priority_queue<P,vector<P>,greater<P> > pq;  
    for(int i=1;i<=V;i++)  
    {  
        d[i]=INF;  
        d2[i]=INF;  
    }  
    d[s]=0;  
    pq.push(P(0,s));  
    while(pq.size())  
    {  
        P nowe=pq.top();pq.pop();  
        if(nowe.first>d2[nowe.second]) continue;  //如果这个距离比当前次短路长continue  
        for(int v=0;v<(int)g[nowe.second].size();v++)  
        {  
            edge nexte=g[nowe.second][v];  
            int dis=nowe.first+nexte.v;  
            if(d[nexte.to]>dis)  
            {  
                swap(dis,d[nexte.to]);  
                pq.push(P(d[nexte.to],nexte.to));  
            }  
            if(d2[nexte.to]>dis&&d[nexte.to]<dis)//保证最短路是小于这个次短路的  
            {  
                d2[nexte.to]=dis;  
                pq.push(P(d2[nexte.to],nexte.to));//次短路的点进入pq  
            }  
        }  
    }  
}  
int main()  
{  
    int s;//起点  
    scanf("%d%d",&V,&E);  
    {  
        for(int i=1;i<=V;i++)  
            g[i].clear();  
        for(int i=1;i<=E;i++)  
        {  
            int f,t,v;  
            scanf("%d%d%d",&f,&t,&v);  
            g[f].push_back(edge(t,v));  
            g[t].push_back(edge(f,v));  
        }  
        s=1;//这题默认起点为1  
        dijkstra(s);  
        printf("%d\n",d2[V]);  
    }  
    return 0;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值