POJ 3255 Roadblocks(次短路模板题)

Roadblocks

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18939   Accepted: 6641

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 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

===========================

题意:

求1~n的次短路。

思路:

1、分别对起点和终点做求最短路的操作:需要注意的是由于数据范围限制,这里不能用二维数组来储存两点之间的权值,所以这里用vector来实现邻接表储存边与下一个点的信息

2、求次短路:1~x的最短路+x~y距离(某条边)+y~n的最短距离,遍历所有边取最小值,且该值不等于1~n最短路径。

3、输出结果。

AC代码(SPFA求最短路):

#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
struct point
{
    int nextt;
    int v;
};
const int MAX=999999999;
vector <point> a[100100];
bool flag[100100];
int dis1[100100],dis2[100100];
void spfa(int s,int dis[])
{
    memset(flag,0,sizeof(flag));
    dis[s]=0;
    queue <int > Q;
    Q.push(s);
    flag[s]=1;
    while(!Q.empty())
    {
        int p=Q.front();
        Q.pop();
        flag[p]=0;
        for(int i=0;i<a[p].size();++i)
        {
            int q=a[p][i].nextt;
            //cout<<"p="<<p<<"  "<<"q="<<q<<"    dis[q]="<<dis[q]<<"  "<<dis[p]+a[p][i].v<<endl;
            if(dis[q]>dis[p]+a[p][i].v)
            {
                dis[q]=dis[p]+a[p][i].v;
                //cout<<"    "<<dis[q]<<endl;
                if(flag[q]==0)
                {
                    flag[q]=1;
                    Q.push(q);
                }
            }
        }
    }
}
int main()
{
    int n,r;
    while(cin>>n>>r)
    {
        for(int i=0;i<100000;++i)a[i].clear();
        for(int i=1;i<=r;++i)
        {
            int x,y,z;
            cin>>x>>y>>z;
            point p1;
            p1.nextt=y;
            p1.v=z;
            a[x].push_back(p1);
            p1.nextt=x;
            a[y].push_back(p1);
        }
        for(int i=0;i<100000;++i)dis1[i]=dis2[i]=MAX;
        spfa(1,dis1);
        spfa(n,dis2);

        //
        int minn=MAX;
        for(int i=1;i<=n;++i)
        {
            for(int j=0;j<a[i].size();++j)
            {
                if((dis1[i]+dis2[a[i][j].nextt]+a[i][j].v)<minn)
                    if((dis1[i]+dis2[a[i][j].nextt]+a[i][j].v)!=dis1[n])
                        minn=dis1[i]+dis2[a[i][j].nextt]+a[i][j].v;

            }
        }
        cout<<minn<<endl;
    }
    return 0;

}

或者用dijkstra算法(同样用vector实现邻接表来储存下一点和两点之间边的信息):

#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
struct point
{
    int nextt;
    int v;
};
const int MAX=999999999;
vector <point> a[100100];
bool flag[100100];
int n,r;
int dis1[100100],dis2[100100];
void dijkstra(int s,int dis[])
{
    memset(flag,0,sizeof(flag));
    dis[s]=0;
    for(int i=1;i<=n;++i)
    {
        int k=0;
        for(int j=1;j<=n;++j)
        {
            if(!flag[j])
                if(dis[j]<dis[k])k=j;
        }
        flag[k]=1;
        for(int j=0;j<a[k].size();++j)//注意vector这里是从0开始存储的;
        {
            if(!flag[a[k][j].nextt])
                if(dis[a[k][j].nextt]>dis[k]+a[k][j].v)
                    dis[a[k][j].nextt]=dis[k]+a[k][j].v;
        }
    }
}
int main()
{
    while(cin>>n>>r)
    {
        for(int i=0;i<100000;++i)a[i].clear();
        for(int i=1;i<=r;++i)
        {
            int x,y,z;
            cin>>x>>y>>z;
            point p1;
            p1.nextt=y;
            p1.v=z;
            a[x].push_back(p1);
            p1.nextt=x;
            a[y].push_back(p1);
        }
        for(int i=0;i<100000;++i)dis1[i]=dis2[i]=MAX;
        dijkstra(1,dis1);
        dijkstra(n,dis2);

        //
        int minn=MAX;
        for(int i=1;i<=n;++i)
        {
            for(int j=0;j<a[i].size();++j)
            {
                if((dis1[i]+dis2[a[i][j].nextt]+a[i][j].v)<minn)
                    if((dis1[i]+dis2[a[i][j].nextt]+a[i][j].v)!=dis1[n])
                        minn=dis1[i]+dis2[a[i][j].nextt]+a[i][j].v;

            }
        }
        cout<<minn<<endl;
    }
    return 0;

}

The end;

猜你喜欢

转载自blog.csdn.net/qq_41661919/article/details/81136729