CodeForces 602C The Two Routes(最短路)

Description

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

Sample Input

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

题意:

在北极,有n个城镇(编号从1到n)和m个双向铁路。这个城镇有一个简单的道路网络 - 对于每一对不同的城镇x和y来说,当且仅当它们之间没有铁路时候,在x,y两镇之间必定有一条双向的公路。

从任意一个小镇走一条铁路或一条公路到一个不同的小镇需要一个小时。火车和巴士同时离开城镇1。他们两个都有相同的目的地n,他们并没有在路上停下来(但他们可以在城镇n等待)。火车只能沿着铁路行驶,公共汽车只能沿着公路行驶。

你需要规划出火车和巴士的路线;

每条路线可以多次使用任何公路或者铁路。要考虑的最重要的方面之一是安全 - 为了避免在铁路口岸发生事故,火车和公共汽车不能同时到达同一个镇(n镇除外)。在这些限制下,两辆车到达城镇n所需的最少小时数中较大的那个(巴士和火车到达时间的最大值)是多少?请注意,巴士和火车不需要在同一时间到达城镇n,但是可以同时到达。

思路:

这个题真的一句话都不能丢。其中很重要的一句话是      当且仅当它们之间没有铁路时候,在x,y两镇之间必定有一条双向的公路。也就是说起点和终点一定会有一条路,要么是铁路,要么是公路。

如果是有铁路直连,那么直接求公路的起点到终点的最短路。如果是有公路直连,那么直接求铁路的起点到终点的最短路。就是一个简单的最短路的题目!

代码:

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <stdio.h>
#define IO ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
using namespace std;
#define inf 0x3f3f3f3f
int map1[2100][2100];
int map2[2100][2100];
int dis[2100],visit[2100];
int n,m;
int dijstra(int map1[2100][2100])
{
    int i,j,pos=1,min,sum=0;
    memset(visit,0,sizeof(visit));
    for(i=1; i<=n; ++i)
    {
        dis[i]=map1[1][i];
    }
    visit[1]=1;
    dis[1]=0;
    for(i=1; i<n; i++)
    {
        min=inf;
        for(j=1; j<=n; ++j)
        {
            if(visit[j]==0&&min>dis[j])
            {
                min=dis[j];
                pos=j;
            }
        }
        visit[pos]=1;
        for(j=1; j<=n; ++j)
        {
            if(visit[j]==0&&dis[j]>dis[pos]+map1[pos][j])
                dis[j]=dis[pos]+map1[pos][j];
        }
    }
    return dis[n];
}
int main()
{
    //IO;
    int i,j;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=1; i<=n; ++i)
        {
            for(j=1; j<=n; ++j)
            {
                map1[i][j]=inf;
                map2[i][j]=inf;
            }
        }
        int a,b,c;
        for(i=1; i<=m; ++i)
        {
            scanf("%d%d",&a,&b);
            c=1;
            map1[a][b]=map1[b][a]=c;
        }
        for(i=1; i<=n; ++i)
        {
            for(j=1; j<=n; ++j)
            {
                if(map1[i][j]==inf)
                    map2[i][j]=1;
            }
        }
        int count=0;
        if(map1[1][n]==1||map1[n][1]==1)
            count=dijstra(map2);
        else
            count=dijstra(map1);
        if(count==inf)
            printf("-1
");
        else
            printf("%d
",count);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/aiguona/p/8252431.html