cf601a The Two Routes

Posted luoyibujue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf601a The Two Routes相关的知识,希望对你有一定的参考价值。

/*
由于道路的互补性  最短路不会再同一时刻相交
所以实际上就是跑两个最短路  取max

*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#define M 410
#define ll long long
using namespace std;
int f[M][M], d[M][M];
const int inf = 1000000000;
int read() {
    int nm = 0, f = 1;
    char c = getchar();
    for(; !isdigit(c); c = getchar()) if(c == -) f = -1;
    for(; isdigit(c); c = getchar()) nm = nm * 10 + c -0;
    return nm * f;
}


int main() {
    int n = read(), m = read();
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            if(i == j) continue;
            else f[i][j] = inf;
    for(int i = 1; i <= m; i++) {
        int vi = read(), vj = read();
        f[vi][vj] = f[vj][vi] = 1;
    }
    for(int i =1; i <= n; i++)
        for(int j = 1; j <= n; j++) {
            if(i == j) continue;
            if(f[i][j] == 1) d[i][j] = inf;
            else d[i][j] = 1;
        }
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++) {
                f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
    if(f[1][n] == inf || d[1][n] == inf) return puts("-1");
    cout << max(f[1][n], d[1][n]);
    return 0;
}

 

以上是关于cf601a The Two Routes的主要内容,如果未能解决你的问题,请参考以下文章

The Two Routes CodeForces - 601A(水最短路)

CodeForces - 601A The Two Routes

CF 602C The Two Routes(dij+邻接矩阵)

CF1005C Summarize to the Power of Two 暴力 map

CF 1005C Summarize to the Power of Two hash/STL-map

CF912E Prime Gift(Meeting In The Middle+Two pointers)