[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 用 PIL 模块 画验证码
PIL 简单绘画 def get_code_img(request): from PIL import Image, ImageDraw, ImageFont import random def ra ...
- input输入框的readonly属性-----http://www.w3school.com.cn/tags/tag_input.asp
http://www.w3school.com.cn/tags/tag_input.asp input输入框的readonly属性 查询方法: 1.先找官方的文档,api 2.官方的有看不懂的再百度相 ...
- Java语言编写TPL语言词法分析器
程序实现原理: 将TXT文本中的数据读出,并按照其类别的不同,将关键字.数字以及运算符识别出来. 一.词法分析实验步骤 1. 熟悉TPL语言 2. 编写TPL语言程序,至少3个,一个简单,一个复杂的( ...
- Ubuntu中Hadoop环境搭建
Ubuntu中Hadoop环境搭建 JDK安装 方法一:通过命令行直接安装(不建议) 有两种java可以安装oracle-java8-installer以及openjdk (1)安装oracle-ja ...
- POJ 1191 DP+DFS棋盘分割问题
题目大意: Description 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形 ...
- 光纤通信(codevs 1955)
题目描述 Description 农民John 想要用光纤连通他的N (1 <= N <= 1,000)个牲口棚(编号1..N).但是,牲口棚位于一个大池塘边,他仅可以连通相邻的牲口棚.J ...
- VS调试STL问题总结
---恢复内容开始--- 以前写代码总觉用自己写的东西比较牛逼,vector?stack?为什么不自己实现.后来才认识到这是个幼稚的想法!首先每次都自己实现是一种重复劳动:其次,自己写的话很难保证没有 ...
- Codeforces 761E(DFS)
E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Binary Tree Postorder Traversal(各种非递归实现,完美利用栈结构模拟)
1.后序遍历的非递归实现.(左右根) 难点:后序遍历的非递归实现是三种遍历方式中最难的一种.因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来 ...
- Memcached与Spring集成的方式(待实践)
主要是基于这几种方式http://www.cnblogs.com/EasonJim/p/7624822.html去实现与Spring集成,而个人建议使用Xmemcached去集成好一些,因为现在官方还 ...