码迷,mamicode.com
首页 > 其他好文 > 详细

cf601a The Two Routes

时间:2018-06-28 23:00:04      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:cstring   lse   getch   turn   stream   char   ios   ||   The   

/*
由于道路的互补性  最短路不会再同一时刻相交
所以实际上就是跑两个最短路  取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

标签:cstring   lse   getch   turn   stream   char   ios   ||   The   

原文地址:https://www.cnblogs.com/luoyibujue/p/9240942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!