[题目链接]

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][],g[MAXN][],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 = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
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 = ; i <= m; i++)
{
int u , v;
read(u); read(v);
addedge(u,v);
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++) dist[i][j] = inf;
queue< int > q;
q.push(i);
dist[i][i] = ;
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] + < dist[i][v])
{
dist[i][v] = dist[i][cur] + ;
q.push(v);
}
}
}
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (i == j || dist[i][j] == inf) continue;
int pos = j;
for (int k = ; k < ; k++)
{
if (f[i][k] == || dist[i][pos] > dist[i][f[i][k]])
swap(pos,f[i][k]);
}
}
for (int j = ; j <= n; j++)
{
if (i == j || dist[j][i] == inf) continue;
int pos = j;
for (int k = ; k < ; k++)
{
if (g[i][k] == || dist[pos][i] > dist[g[i][k]][i])
swap(pos,g[i][k]);
}
}
}
int ans = ;
for (int b = ; b <= n; b++)
{
for (int c = ; c <= n; c++)
{
if (b == c || dist[b][c] == inf) continue;
for (int t1 = ; t1 < ; t1++)
{
int a = g[b][t1];
if (a == || a == c) continue;
for (int t2 = ; t2 < ; t2++)
{
int d = f[c][t2];
if (d == || 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 ; }

[Codeforces 666B] World Tour的更多相关文章

  1. CodeForces 666B World Tour(spfa+枚举)

    B. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard input ...

  2. Codeforces 667D World Tour 最短路

    链接 Codeforces 667D World Tour 题意 给你一个有向稀疏图,3000个点,5000条边. 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大. 思 ...

  3. Codeforces 490F. Treeland Tour 暴力+LIS

    枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...

  4. Codeforces 490F Treeland Tour 树形dp

    Treeland Tour 离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快.. #include<bits/stdc++.h> #define LL long ...

  5. Codeforces 490F Treeland Tour(离散化 + 线段树合并)

    题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using name ...

  6. Codeforces 667D World Tour【最短路+枚举】

    垃圾csdn,累感不爱! 题目链接: http://codeforces.com/contest/667/problem/D 题意: 在有向图中找到四个点,使得这些点之间的最短距离之和最大. 分析: ...

  7. Codeforces 1137C Museums Tour (强连通分量, DP)

    题意和思路看这篇博客就行了:https://www.cnblogs.com/cjyyb/p/10507937.html 有个问题需要注意:对于每个scc,只需要考虑进入这个scc的时间即可,其实和从哪 ...

  8. Codeforces 490F Treeland Tour 树上的最长上升子序列

    题目链接:点击打开链接 题意: 给定n个点的树. 以下n个数表示点权. 以下n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条 ...

  9. codeforces选做

    收录了最近本人完成的一部分codeforces习题,不定期更新 codeforces 1132E Knapsack 注意到如果只使用某一种物品,那么这八种物品可以达到的最小相同重量为\(840\) 故 ...

随机推荐

  1. react初探索--react + react-router + ant-design 后台管理系统配置

    首先确认安装了node环境,Node >= 6. 如果对react 及 ant-design 一无所知,建议去阅读下api文档,react 可以在 codePen 在线练习. react Api ...

  2. Linux忘记root密码,密码找回,图片展示

    忘记root密码 CentOS 7参考地址如下:https://www.baidu.com/s?wd=CentOS7+%E6%89%BE%E5%9B%9Eroot%E5%AF%86%E7%A0%81& ...

  3. 树莓派 -- 输入设备驱动 (key)

    输入设备(如按键,键盘,触摸屏等)是典型的字符设备,其一般工作原理是底层在按键或触摸等动作发生时产生一个中断,然后CPU通过SPI,I2C总线读取键值. 在这些工作中之后中断和读键值是与设备相关的,而 ...

  4. uva 10596 - Morning Walk

    Problem H Morning Walk Time Limit 3 Seconds Kamal is a Motashota guy. He has got a new job in Chitta ...

  5. stark组件之添加、修改页面内容搭建(七)

    如何快速的进行数据的添加以及修改呢?modelform来实现是可以达到效果的,在这里就是应用了modelform,每一个表都不同,所以需要创建不同的modelform. def get_model_f ...

  6. Python之Pycharm安装及介绍

    在学习Python之前,先安装好编程所需的编译环境也就是IDE,在安装PycharPm之前先安装最新版本的anaconda根据不同的系统选择不同的版本,安装好anaconda以后再安装Pycharm, ...

  7. list嵌套,int与str的用法,replace

    #*************************replace(待改,改动值),返回很重要 A = [['libai',89]] A[0][0]=A[0][0].replace('a','af') ...

  8. LeetCode(56)Merge Intervals

    题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6], ...

  9. flash-热风焊盘的制作

    计算部分: 热风焊盘的内径(ID)等于钻孔直径+20mil, 外径(OD)等于Anti-pad的直径,通常是比焊盘的直径大20mil. 开口宽度等于(OD-ID)/2+10mil,保留整数位. 如果焊 ...

  10. 洛谷P1710地铁涨价

    题目背景 本题开O2优化,请注意常数 题目描述 博艾市除了有海底高铁连接中国大陆.台湾与日本,市区里也有很成熟的轨道交通系统.我们可以认为博艾地铁系统是一个无向连通图.博艾有N个地铁站,同时有M小段地 ...