[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\) 故 ...
随机推荐
- windows 安装 python3
安装python------------------------------------------------------------ 1,打开连接https://www.python.org/do ...
- PHP:POST OR GET 请求
文章来源:http://www.cnblogs.com/hello-tl/p/7685216.html /** * 模拟提交参数,支持https提交 可用于各类api请求 * @param strin ...
- Python之条件判断
Python之条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age ...
- 语法,if,while循环,for循环
目录 一.语法 二.while循环 三.for循环 一.语法 if: if判断其实是在模拟人做判断.就是说如果这样干什么,如果那样干什么.对于ATM系统而言,则需要判断你的账号密码的正确性. if 条 ...
- json pickle shelve hashlib collections time
import json # Json模块提供了四个功能:dumps.dump.loads.load dic = {'k1':'v1','k2':'v2','k3':'v3'} str_dic = js ...
- 数据库SQL实战练习
http://blog.csdn.net/iamyvette/article/details/77151925
- 1016-Prime Ring Problem,素数环,深搜!
Prime Ring Problem ...
- 1001. A+B Format (20) (%0nd)
1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...
- acm 一年总结
首先是大一的一段简短历史,和其他人不太一样,刚上大一的我等于是刚刚接触电脑,开始下载程序啦,安装系统了,电脑出个小问题啦自己都不会解决,然后大一还开了一门叫做c语言的课程,顿时傻逼了,当时也不用功,大 ...
- csu - 1537: Miscalculation (模拟题)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1537 因为给出的式子是必定合法的,只要用两个栈分别保存符号和数字.算出答案后和从左至右算的答案比对 ...