次短路 poj3255 Roadblocks

题目链接:点击打开链接

Roadblocks
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14743 Accepted: 5184

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

[Submit]   [Go Back]   [Status]   [Discuss]


题意:求出次短路的长度,其中最短路与次短路不重合!

思路:1.挑战上面是利用 dijkstra+堆优化 自定义优先级,代码:http://blog.csdn.net/yuanjunlai141/article/details/50346665

2.我比较习惯用自定义优先级,但是莫名超时~

3.又从其他的博客中学到方法:

分析一下状况,次短路不可能与最短路完全重合,那么就一定会有一段比较绕路。绕路地方不可能超过两处,那样就有一条路短于这条路而长于最短路,矛盾。因此可以对所有边加以枚举。首先用Dijkstra或SPFA以原点和终点为源分别做一次单源最短路,并把答案存在dist_0和dist_n两个数组中。那么,对于任何一条边(i, j),下面二者之一就有可能是次短路长:

dist_0[i] + len(i, j) + dist_n[j] 和 dist_0[j] + len(i, j) + dist_n[i]

注意,如果其中一个长度等于最短路长度(即dist_n[0]),就一定不能选,因为这违反次短路定义。两个都要枚举,因为可能有其中一个等于最短路长,如果只取较小值另外一个就废掉了。

附:dijikstra算法:

    1.找到最短距离已经确定的顶点,从他出发更新相邻顶点的最短距离;

    2.此后不需要再关心1.中的“最短距离已经确定的顶点”


代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

const int  inf=1e9-1;
const int maxv=5005;

struct Edge
{
    int v,cost;
};
vector<Edge>E[maxv];
int d_1[maxv],d_n[maxv];
int d[maxv],vis[maxv];
int cost[maxv][maxv];
int N,R;

void SPFA1(int n,int s)
{
    memset(vis,false,sizeof(vis));
    for(int i=0;i<=n;i++) d[i]=inf;
    vis[s]=true;
    d[s]=0;
    queue<int>que;
    while(!que.empty()) que.pop();
    que.push(s);

    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        vis[u]=false;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v,w=E[u][i].cost;
            if(d[v]>d[u]+w)
            {
                d[v]=d[u]+w;
                if(!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                }
            }
        }
    }

    if(s==1)
    {
        for(int i=0;i<=n;i++)
            d_1[i]=d[i];
    }
    else if(s==n)
    {
        for(int i=0;i<=n;i++)
            d_n[i]=d[i];
    }
}

void addedge(int u,int v,int w)
{
    E[u].push_back(Edge{v,w});
}

int main()
{
    scanf("%d%d",&N,&R);
    for(int i=0;i<R;i++)
    {
        int u,v,c;
        scanf("%d%d%d",&u,&v,&c);
        addedge(u,v,c);
        addedge(v,u,c);
    }
    SPFA1(N,1);
    SPFA1(N,N);

    int minn=inf;
    for(int i=0;i<N;i++)
    for(int j=0;j<E[i].size();j++)
    {
        int x=i,y=E[i][j].v;
        int l=d_1[x]+E[i][j].cost+d_n[y];
        int r=d_n[x]+E[i][j].cost+d_1[y];
        if(l==d[1]) l=inf;
        if(r==d[1]) r=inf;
        minn=min(min(l,r),minn);
    }

    printf("%d\n",minn);
}


超时的dijkstra+堆优化算法,自定义优先级

代码:

#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=10010;

struct qnode
{
    int v,c;
    bool operator < (const qnode &x) const
    {
        if(x.c!=c)
            return x.c>c;
    }
};

struct Edge
{
    int v,cost;
};

vector<Edge>E[maxn];
int n,m;
int dis1[maxn];
int dis2[maxn];
void dijk(int n,int start)
{
    for(int i=1; i<=n; i++)  dis1[i]=inf;
    for(int i=1; i<=n; i++)  dis2[i]=inf;
    priority_queue<qnode>que;
    while(!que.empty()) que.pop();

    dis1[start]=0;
    que.push(qnode {start,0});
    qnode tmp;
    while(!que.empty())
    {
        tmp=que.top();
        que.pop();
        int u=tmp.v;
        int cost1=tmp.c;
        if(dis2[u]<cost1)
            continue;

        for(int i=0; i<E[u].size(); i++)
        {
            int v=E[u][i].v;
            int cost2=cost1+E[u][i].cost;
//            if(dis1[v]>cost2)
//            {
//                dis2[v]=dis1[v];
//                dis1[v]=cost2;
//                que.push(qnode{v,dis1[v]});
//                que.push(qnode{v,dis2[v]});
//            }
//            else if(cost2>dis1[v]&&cost2<dis2[v])
//            {
//                dis2[v]=cost2;
//                que.push(qnode{v,dis2[v]});
//            }
            if(dis1[v]>cost2)
            {
                swap(dis1[v],cost2);
                que.push(qnode {v,dis1[v]});
            }
            if(dis2[v]>cost2&&dis1[v]<cost2)
            {
                dis2[v]=cost2;
                que.push(qnode {v,dis2[v]});
            }
        }
    }
    printf("%d\n",dis2[n]);
}

void addedge(int u,int v,int w)
{
    E[u].push_back(Edge {v,w});
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<m; i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        addedge(a,b,c);
        addedge(b,a,c);
    }
    dijk(n,1);

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值