最短路径

Posted wangzhelin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了最短路径相关的知识,希望对你有一定的参考价值。

最近写了挺多的最短路径的题目,我觉得有必要总结一下了。

首先一道入门的水题:

A. The Two Routes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.

A train and a bus leave town 1 at the same time. They both have the same destination, town n, and don‘t make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.

You‘ve been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety — in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.

Under these constraints, what is the minimum number of hours needed for both vehicles to reach town n (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.

Input

The first line of the input contains two integers n and m (2?≤?n?≤?400, 0?≤?m?≤?n(n?-?1)?/?2) — the number of towns and the number of railways respectively.

Each of the next m lines contains two integers u and v, denoting a railway between towns u and v(1?≤?u,?v?≤?nu?≠?v).

You may assume that there is at most one railway connecting any two towns.

Output

Output one integer — the smallest possible time of the later vehicle‘s arrival in town n. If it‘s impossible for at least one of the vehicles to reach town n, output ?-?1.

Examples
input
4 2
1 3
3 4
output
2
input
4 6
1 2
1 3
1 4
2 3
2 4
3 4
output
-1
input
5 5
4 2
3 5
4 5
5 1
1 2
output
3
Note

In the first sample, the train can take the route 技术分享图片 and the bus can take the route 技术分享图片. Note that they can arrive at town 4 at the same time.

In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there‘s no way for the bus to reach town 4.

这道题目的意思就是铁路与公路不能共存,问火车与汽车花费的最长时间,如果有一种交通方式不能到达目的地,则输出-1,这题我用了dijkstra算法做

#include<iostream>
#include<cmath>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
int m,n;
int vis[450],vis1[450];
int a[450][450],b[450][450],d[450],d1[450];
void dijkstra(){
    int i,j,minn,flag;
    memset(vis,0,sizeof(vis));
    vis[1]=1;
    for(i=2;i<=m;i++){
        d[i]=a[1][i];
    }
    for(i=1;i<=m;i++){
        minn=inf;
        for(j=1;j<=m;j++){
            if(!vis[j]&&d[j]<minn){
                minn=d[j];
                flag=j;
    //            cout<<d[j]<<" "<<minn<<endl;
            }
        }
    //    cout<<minn<<endl;
        if(minn==inf){
            break;
        }
    //    cout<<minn<<endl;
        vis[flag]=1;
    //    cout<<d[flag]<<endl;
    //cout<<a[flag][4];
    //cout<<vis[4];
        for(j=1;j<=m;j++){
            if(!vis[j]&&(d[j]>d[flag]+a[flag][j])){
            //    cout<<d[j]<<" "<<d[flag]+a[flag][j]<<endl;
            //    cout<<flag<<" "<<j<<endl;
            //    cout<<d[3]<<" "<<a[3][2]<<endl;
                d[j]=d[flag]+a[flag][j];
            //    cout<<d[j]<<"*"<<j<<endl;
            }
        }
    //    cout<<d[flag]<<"*"<<endl;
    }
}
void dijkstra1(){
    int i,j,minn,flag;
    memset(vis1,0,sizeof(vis1));
    vis1[1]=1;
    for(i=2;i<=m;i++){
        d1[i]=b[1][i];
    }
    for(i=1;i<=m;i++){
        minn=inf;
        for(j=1;j<=m;j++){
            if(!vis1[j]&&d1[j]<minn){
                minn=d1[j];
                flag=j;
            }
        }
        if(minn==inf){
            break;
        }
        vis1[flag]=1;
        for(j=1;j<=m;j++){
            if(!vis1[j]&&d1[j]>(d1[flag]+b[flag][j])){
                d1[j]=d1[flag]+b[flag][j];
            }
        }
    }
}
int main()
{
    int i,j,x,y;
    while(cin>>m>>n){
        memset(a,inf,sizeof(a));
        memset(d,inf,sizeof(d));
        memset(d1,inf,sizeof(d1));
        for(i=0;i<n;i++){
            cin>>x>>y;
            a[x][y]=1;
            a[y][x]=1;
        }
        for(i=1;i<=m;i++){
            for(j=1;j<=m;j++){
                if(a[i][j]==1){
                    b[i][j]=inf;
                }
                else{
                    b[i][j]=1;
                }
    //            cout<<b[i][j]<<" ";
            }
        }
        dijkstra();
        dijkstra1();
    //    cout<<d[m]<<endl<<d1[m]<<endl;
        if(d[m]==inf||d1[m]==inf){
            cout<<"-1"<<endl;
        }
        else{
            cout<<max(d[m],d1[m])<<endl;
        }
    }
    return 0;
}

改bug 用了很长时间,就是把memset初始化错了memset对于整数只能初始化为0,而不能初始化为其他的数,我就是因为初始化为1 找了很久的错误,找bug的痕迹也留下来了,警醒自己吧,哈哈。

 














以上是关于最短路径的主要内容,如果未能解决你的问题,请参考以下文章

(王道408考研数据结构)第六章图-第四节3:最短路径之BFS算法(思想代码演示答题规范)

最短路径(Dijkstra算法)

最短路径 深入浅出Dijkstra算法(一)

快看!一条进入公益传播的最短路径就在眼前

最短路径

最短路径问题-Dijkstra(基于图的ADT)