codeforces602C The Two Routes (最短路模板题)

题目链接:http://codeforces.com/problemset/problem/602/C


C. 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 ≤ 4000 ≤ 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 test(s)
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个村庄,然后村庄两两相连,给你m条铁路,火车开的,然后剩下的路都是大巴车开的。路径是双向的,并且火车和巴士除了第n个村庄 不能同时到达一个村庄(这个条件就是迷惑的) ,因为如果两种交通方式都是有路可走,那么必定会有一条路是可以直接从1到达n的,所以判断一下是哪种方式是可以直达的,再求一下另外一种交通方式的最短路就可以了。如果不可以到达输出-1 ,如果可以到达输出时间。直接用dijkstra算法就可以了,边的权值为1 ,基本属于模板题目


#include 
#include 
#include 
#define INF 1 << 29
using namespace std;
int map1[410][410],map2[410][410];  //记录火车的路和大巴的路
int n,m;
void dijkstra(int map[][410])       
{
    int res[410];       //记录结果
    int vis[410];       //访问标记
    int v,min;          //每相连点的最小下标和最短的路
    for(int i = 1 ; i <= n ; i++ )      //初始化
    {
        vis[i] = 0;
        res[i] = map[1][i];             
    }
    for (int i = 1 ; i <= n ; i++ )
    {
        min = 1<< 29;
        for (int j = 1 ; j <= n ; j++ )
        {
            if (vis[j] == 0 && res[j] < min )       //遍历,找出与这个点相连的最小路
            {
                v = j;
                min = res[j];
            }
        }
        vis[v] = 1;
        for (int j = 1 ; j <= n ; j++ )
        {
            if(vis[j] == 0 && res[j] > map[v][j] + res[v])      //判断
            {
                res[j] = map[v][j] +res[v];
            }
        }
    }
    if (res[n] == INF)      //如果最后的值没有改变 说明不可能
    {
        printf("-1\n");
        return ;
    }
    else 
        printf("%d\n",res[n]);
}
int main()
{
    while (~scanf("%d%d",&n,&m ) )
    {
        for (int i = 1 ; i <= n ; i++ )         //初始化
        {
            for (int j = 1 ; j <= n ; j++ )
            {
                if ( i == j)
                {
                    map1[i][j] = 0;
                    map1[i][j] = 0;
                }
                else 
                {
                    map1[i][j] = map1[j][i] = INF;
                    map2[i][j] = map2[j][i] = INF;
                }
            }
        }
        int flag = 0;
        for (int i = 0 ; i < m ; i++ )
        {   
            int a,b;
            scanf("%d%d",&a,&b);
            if ( (a == 1 && b == n) || (a == n && b == 1) )         //必有一条路可以直达,所以判断是火车还是巴士
            {
                flag = 1;
            }
            map1[a][b] = 1;
            map1[b][a] = 1;
        }
        for (int i = 1 ; i <= n ; i ++ )
        {
            for (int j = 1 ; j <= n ; j++ )
            {
                if ( map1[i][j] == INF || map1[i][j] == 0)
                {
                    map2[i][j] = 1;
                    map2[j][i] = 1;
                }
            }
        }
        if (m == n*(n-1)/2)     //如果只有火车有路径
        {
            printf("-1\n");
            continue;
        }    
        if (flag == 1)          //如果火车可以直达
        {
            dijkstra(map2);
        }
        else if (flag == 0)     //如果大巴可以直达
        {
            dijkstra(map1);
        }
    }
    return 0;
}





你可能感兴趣的:(CF)