The Two Routes【codeforces602C】【最短路】

Codeforces Round #333 (Div. 2) 题目链接


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


  修了铁路的地方就不能建公路了,所以直接邻接矩阵建图,直接去跑最短路,并且判断是否可以跑到,即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 405;
int N, M, dis[maxN];
int mp[maxN][maxN];
struct node
{
    int id, dis;
    node(int a=0, int b=0):id(a), dis(b) {}
    friend bool operator < (node e1, node e2) { return e1.dis > e2.dis; }
};
priority_queue<node> Q;
inline int dij(int op)
{
    memset(dis, INF, sizeof(dis));
    dis[1] = 0;
    while(!Q.empty()) Q.pop();
    Q.push(node(1, 0));
    while(!Q.empty())
    {
        node now = Q.top(); Q.pop();
        int u = now.id;
        for(int i=2; i<=N; i++)
        {
            if(mp[u][i] == op)
            {
                if(dis[i] > now.dis + 1)
                {
                    dis[i] = now.dis + 1;
                    Q.push(node(i, dis[i]));
                }
            }
        }
    }
    return dis[N];
}
int main()
{
    scanf("%d%d", &N, &M);
    memset(mp, 0, sizeof(mp));
    for(int i=1, u, v; i<=M; i++)
    {
        scanf("%d%d", &u, &v);
        mp[u][v] = mp[v][u] = 1;
    }
    int d1 = dij(0), d2 = dij(1);
    int ans = max(d1, d2);
    if(ans >= INF) printf("-1\n");
    else printf("%d\n", ans);
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值