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)

题目链接:http://poj.org/problem?id=3255

代码如下:

<span style="font-size:24px;">#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<functional>
#include<cstdio>
#include<climits>
using namespace std;
const int maxx=100001;
const int INF=INT_MAX/10;
int n,r;
int d[maxx],ju=INF,di[maxx];
struct My
{
    int to,cost;
    My(int a,int b)
    {
        to=a;
        cost=b;
    }
};
vector<My>G[maxx];
void init()
{
    for(int i=0; i<n; i++)G[i].clear();
}
void add_edge(int from,int to,int cost)
{
    G[from].push_back(My(to,cost));
}
typedef pair<int,int>p;
void dijikstra(int s)
{
    priority_queue<p,vector<p>,greater<p> >que;
    que.push(p(0,s));
    for(int i=0; i<=n; i++)d[i]=INF;
    d[s]=0;
    for(int i=0; i<=n; i++)di[i]=INF;
    while(!que.empty())
    {
        p p1=que.top();
        que.pop();
        int v=p1.second;
        if(di[v]<p1.first) continue;
        for(int i=0; i<G[v].size(); i++)
        {
            My &e=G[v][i];
            int num=p1.first+e.cost;
            if(d[e.to]>num)
            {
               swap(d[e.to],num);
                que.push(p(d[e.to],e.to));
            }
            if(di[e.to]>num&&d[e.to]<num)
            {
                di[e.to]=num;
                que.push(p(di[e.to],e.to));
            }
        }
    }
}
int main()
{
    int a,b,c;
    scanf("%d%d",&n,&r);
    for(int i=0; i<r; i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        add_edge(a,b,c);
        add_edge(b,a,c);
    }
    dijikstra(1);
   cout<<di[n]<<endl;
    return 0;
}</span>
这是标准解法,就是将每次的最短路和次短路都存到优先队列里,然后一个一个取出计算。


还有一种我自己想的,感觉思路没错,但是不知道哪个地方出了问题。。反正没A。

我的想法是这样的,因为对于dij算法而言,每一步都是取当前的最小值。起点为1,终点为N。则1--B的最短路是由1--A(B前面的一个点)的最短路加上AB。我们需要知道的是,由于这是选择次短路,那么只有可能是某一个相邻两点的最短路变长了一点。不可能出现两个地方都变长的情况,这样就不是次短路了。然后就可以枚举相邻两点的最小边长距离,然后取最小值即可。这就是最短路与次短路的距离差。

代码如下:没能AC:

<span style="font-size:24px;">#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<functional>
#include<cstdio>
#include<climits>
using namespace std;
const int maxx=100001;
const int INF=INT_MAX/10;
int n,r;
int d[maxx],ju=INF,di[maxx],pre[maxx]= {0};
struct My
{
    int to,cost;
    My(int a,int b)
    {
        to=a;
        cost=b;
    }
};
vector<My>G[maxx];
void init()
{
    for(int i=0; i<n; i++)G[i].clear();
}
void add_edge(int from,int to,int cost)
{
    G[from].push_back(My(to,cost));
}
typedef pair<int,int>p;
void dijikstra(int s)
{
    priority_queue<p,vector<p>,greater<p> >que;
    que.push(p(0,s));
    for(int i=0; i<=n; i++)d[i]=INF;
    d[s]=0;
    for(int i=0; i<=n; i++)di[i]=INF;
    di[s]=0;
    while(!que.empty())
    {
        p p1=que.top();
        que.pop();
        int v=p1.second;
        if(d[v]<p1.first)
        {
            continue;
        }

        for(int i=0; i<G[v].size(); i++)
        {
            My e=G[v][i];
            if(d[e.to]>d[v]+e.cost)
            {
                di[e.to]=min(di[e.to],d[e.to]);
                pre[e.to]=v;
                d[e.to]=d[v]+e.cost;
                que.push(p(d[e.to],e.to));
            }
            else
            {
                di[e.to]=min(di[e.to],d[v]+e.cost);
            }
        }
    }
}
int main()
{
    int a,b,c;
    scanf("%d%d",&n,&r);
    for(int i=0; i<r; i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        add_edge(a,b,c);
        add_edge(b,a,c);
    }
    dijikstra(1);
    for(int t=n; t!=1; t=pre[t])
    {
        ju=min(ju,di[t]-d[t]);
    }

    cout<<d[n]+ju<<endl;
    return 0;
}</span>
<span style="font-size:24px;">欢迎大神指出错误。。。</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值