标签:its res wap 复杂 info getchar inline int namespace
[题目链接]
https://codeforces.com/contest/666/problem/B
[算法]
首先 , 用BFS求出任意两点的最短路径
然后 , 我们用f[i][0-2]表示从i出发到达的最远三点 , g[i][0-2]表示到i距离最远的三个点
枚举b和c , 然后在枚举3 * 3个点对 , 从中选出最优的a和d即可
时间复杂度 : O(N^2)
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 3010 #define MAXM 5010 const int inf = 2e9; int tot , n , m; int head[MAXN]; int f[MAXN][3],g[MAXN][3],dist[MAXN][MAXN]; struct info { int a , b , c , d; } res; struct edge { int to , nxt; } e[MAXM]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); } template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - ‘0‘; x *= f; } inline void addedge(int u,int v) { tot++; e[tot] = (edge){v,head[u]}; head[u] = tot; } int main() { read(n); read(m); for (int i = 1; i <= m; i++) { int u , v; read(u); read(v); addedge(u,v); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) dist[i][j] = inf; queue< int > q; q.push(i); dist[i][i] = 0; while (!q.empty()) { int cur = q.front(); q.pop(); for (int j = head[cur]; j; j = e[j].nxt) { int v = e[j].to; if (dist[i][cur] + 1 < dist[i][v]) { dist[i][v] = dist[i][cur] + 1; q.push(v); } } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j || dist[i][j] == inf) continue; int pos = j; for (int k = 0; k < 3; k++) { if (f[i][k] == 0 || dist[i][pos] > dist[i][f[i][k]]) swap(pos,f[i][k]); } } for (int j = 1; j <= n; j++) { if (i == j || dist[j][i] == inf) continue; int pos = j; for (int k = 0; k < 3; k++) { if (g[i][k] == 0 || dist[pos][i] > dist[g[i][k]][i]) swap(pos,g[i][k]); } } } int ans = 0; for (int b = 1; b <= n; b++) { for (int c = 1; c <= n; c++) { if (b == c || dist[b][c] == inf) continue; for (int t1 = 0; t1 < 3; t1++) { int a = g[b][t1]; if (a == 0 || a == c) continue; for (int t2 = 0; t2 < 3; t2++) { int d = f[c][t2]; if (d == 0 || d == b || d == a) continue; if (dist[a][b] + dist[b][c] + dist[c][d] > ans) { ans = dist[a][b] + dist[b][c] + dist[c][d]; res = (info){a,b,c,d}; } } } } } printf("%d %d %d %d\n",res.a,res.b,res.c,res.d); return 0; }
标签:its res wap 复杂 info getchar inline int namespace
原文地址:https://www.cnblogs.com/evenbao/p/9736720.html