1. 程式人生 > >CodeForces 602C The Two Routes(最短路)

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
2
3
4
Output
Input
6
2
3
4
3
4
4
Output
-1
Input
5
2
5
5
1
2
Output

 

題意:

在北極,有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\n");
        else
            printf("%d\n",count);
    }
    return 0;
}

 

轉自https://www.cnblogs.com/aiguona/p/8252431.html