[Codeforces 666B] World Tour
[题目链接]
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的更多相关文章
- CodeForces 666B World Tour(spfa+枚举)
B. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard input ...
- Codeforces 667D World Tour 最短路
链接 Codeforces 667D World Tour 题意 给你一个有向稀疏图,3000个点,5000条边. 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大. 思 ...
- Codeforces 490F. Treeland Tour 暴力+LIS
枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...
- Codeforces 490F Treeland Tour 树形dp
Treeland Tour 离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快.. #include<bits/stdc++.h> #define LL long ...
- Codeforces 490F Treeland Tour(离散化 + 线段树合并)
题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using name ...
- Codeforces 667D World Tour【最短路+枚举】
垃圾csdn,累感不爱! 题目链接: http://codeforces.com/contest/667/problem/D 题意: 在有向图中找到四个点,使得这些点之间的最短距离之和最大. 分析: ...
- Codeforces 1137C Museums Tour (强连通分量, DP)
题意和思路看这篇博客就行了:https://www.cnblogs.com/cjyyb/p/10507937.html 有个问题需要注意:对于每个scc,只需要考虑进入这个scc的时间即可,其实和从哪 ...
- Codeforces 490F Treeland Tour 树上的最长上升子序列
题目链接:点击打开链接 题意: 给定n个点的树. 以下n个数表示点权. 以下n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条 ...
- codeforces选做
收录了最近本人完成的一部分codeforces习题,不定期更新 codeforces 1132E Knapsack 注意到如果只使用某一种物品,那么这八种物品可以达到的最小相同重量为\(840\) 故 ...
随机推荐
- Python之“Hello World”
Python之“Hello World” 了解Python: 编译型和解释型 编译:把明文代码执行前,先转换成二进制,在执行.这个过程叫编译 解释器:将明文代码转成二进制的 Linux中,gcc编译, ...
- Spider-Python爬虫之聚焦爬虫与通用爬虫的区别
为什么要学习爬虫? 学习爬虫,可以私人订制一个搜索引擎. 大数据时代,要进行数据分析,首先要有数据源. 对于很多SEO从业者来说,从而可以更好地进行搜索引擎优化. 什么是网络爬虫? 模拟客户端发送网络 ...
- UVa Sculpture(离散化 floodfill)
题意: 给定n个立方体的一个顶点坐标和3边长度, 问这些立方体组成的雕塑的表面积和体积, 坐标都是整数,n最大为50, 最大为500, 边长最大也是500. 分析: 继UVa221后又一道离散 ...
- source insight中的快捷键总结
1.快捷键 1,Shift+F8高亮显示指定字符. 2,Ctrl+F找出来的结果用F4,F3前进后退查找. 3,Alt+,后退alt+.前进查找关键字. 4,Alt+G或者F5跳转到某个固定的行号. ...
- Mvc Action可以通过jsonp方式调取
jsonp其实是一种特殊的数据获取格式,所以在Aicton直接调取的时候肯定会出现问题,下面代码是对于jsonp调取做的处理 protected virtual ActionResult Create ...
- Jmeter-接口测试实例讲解
一.测试需求描述 1. 本次测试的接口为http服务端接口 2. 接口的主要分成两类,一类提供给查询功能接口,一类提供保存数据功能接口,这里我们举例2个保存数据的接口,因为这两个接口有关联性,比较有代 ...
- HDU4463-Outlets,简单最小生成树。1A水过~~
Outlets ...
- Automation 的 ReportFlow
ReportFlow: // click the Grid icon and switch to grid page public void changeToGrid() // click the A ...
- [BZOJ2667][cqoi2012]模拟工厂
[BZOJ2667][cqoi2012]模拟工厂 试题描述 有一个称为“模拟工厂”的游戏是这样的:在时刻0,工厂的生产力等于1.在每个时刻,你可以提高生产力或者生产商品.如果选择提高生产力,在下一个时 ...
- bzoj1007 [HNOI2008]水平可见直线 - 几何 - hzwer.com
Description Input 第一行为N(0 < N < 50000),接下来的N行输入Ai,Bi Output 从小到大输出可见直线的编号,两两中间用空格隔开,最后一个数字后面也必 ...