CodeForces - 602C The Two Routes (最短路

The Two Routes

题目描述

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.

输入

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 ≤ n, u ≠ v).
You may assume that there is at most one railway connecting any two towns.

输出

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.

样例

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

题意

晕死了,因为图保证了是个完全图,而且两者之间压根不可能走在一个点上面,最大的坑点没了贴板子上去就好了

AC代码

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;

#define ls              st<<1
#define rs              st<<1|1
#define fst             first
#define snd             second
#define MP              make_pair
#define PB              push_back
#define LL              long long
#define PII             pair<int,int>
#define VI              vector<int>
#define CLR(a,b)        memset(a, (b), sizeof(a))
#define ALL(x)          x.begin(),x.end()
#define ber(i,s,e) for(int i=(s); i<=(e); i++)
#define rep(i,s,e) for(int i=(s); i>=(e); i--)

const int INF = 0x3f3f3f3f;
const int MAXN = 2e3+10;
const int mod = 1e9+7;
const double eps = 1e-8;

void fe() {
  #ifndef ONLINE_JUDGE
      freopen("in.txt", "r", stdin);
      freopen("out.txt","w",stdout);
  #endif
}
LL read()
{
   LL x=0,f=1;char ch=getchar();
   while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
   while (ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
   return x*f;
}

struct edge{
    int v, cost;
    edge(int _v=0, int _cost=0):v(_v),cost(_cost){}
};

vector<edge> E[MAXN];
void addedge(int u, int v, int w) {
    E[u].push_back(edge(v,w));
    E[v].push_back(edge(u,w));
}
bool vis[MAXN];
int dis[MAXN];

void SPFA(int s, int n) {
    CLR(vis,false);
    for(int i = 1; i <= n; i++)
        dis[i] = INF;
    vis[s] = true;
    dis[s] = 0;
    queue<int> que;
    while(!que.empty()) que.pop();
    que.push(s);
    while(!que.empty()) {
        int u = que.front();
        que.pop();
        vis[u] = false;
        for(int i = 0; i < E[u].size(); i++) {
            int v = E[u][i].v;
            int cost = E[u][i].cost;
            if(dis[v] > dis[u]+cost) {
                dis[v] = dis[u]+cost;
                if(!vis[v]) {
                    vis[v] = true;
                    que.push(v);
                }
            }
        }
    }
}

bool mps[MAXN][MAXN];
int main()
{
    CLR(mps,false);
    int n, m;
    cin >> n >> m;
    int u, v;
    for(int i = 0; i < m; i++) {
        cin >> u >> v;
        addedge(u,v,1);
        mps[u][v] = mps[v][u] = true;
    }
    int ans1, ans2;
    SPFA(1,n);
    ans1 = dis[n];
    for(int i = 1; i <= n; i++)
        E[i].clear();
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(i == j) continue;
            if(!mps[i][j]) {
                mps[i][j] = true;
                addedge(i,j,1);
                addedge(j,i,1);
            }
        }
    }
    SPFA(1,n);
    ans2 = dis[n];
    if(ans1==INF || ans2==INF)
        cout << -1 << endl;
    else
        cout << max(ans1,ans2) << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wang2332/article/details/80300840
今日推荐