Codeforces 667D World Tour【最短路+枚举】
垃圾csdn,累感不爱!
题目链接:
http://codeforces.com/contest/667/problem/D
题意:
在有向图中找到四个点,使得这些点之间的最短距离之和最大。
分析:
最简单的Bellman求最短路复杂度太高。可以对每个点进行一次bfs,获得所有连通的点之间的最短距离。
点数最多3000,枚举中间两个点\(i,j\),对于点\(i\)考虑反向边的最远距离,对于点\(j\)考虑正向边的最远距离。
由于题目说点不同,所以对于每个点我们保存前三个远的点并枚举求得最远距离即可。这样枚举下来时间复杂度\(O(n^2)\)。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 3e3 + 5, oo = 0x3f3f3f3f;
int d[maxn][maxn];
int n, m;
typedef pair<int, int>p;
vector<p>zz[maxn], rzz[maxn];
vector<int>G[maxn];
void bfs()
{
for(int i = 1; i <= n; i++){
queue<int>q;
q.push(i);
while(!q.empty()){
int u = q.front(); q.pop();
for(int j = 0; j <G[u].size(); j++){
int w = G[u][j];
if(d[i][w] != oo) continue;
d[i][w] = d[i][u] + 1;
q.push(w);
}
}
}
}
int main (void)
{
scanf("%d%d", &n, &m);
memset(d, 0x3f, sizeof(d));
for(int i = 1; i <= n; i++) d[i][i] = 0;
int u, v;
for(int i = 0; i < m; i++){
scanf("%d%d", &u, &v);
G[u].push_back(v);
}
bfs();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(i == j) continue;
if(d[i][j] != oo) zz[i].push_back(p(d[i][j], j));
if(d[j][i] != oo) rzz[i].push_back(p(d[j][i], j));
}
sort(zz[i].begin(), zz[i].end());
sort(rzz[i].begin(), rzz[i].end());
}
int r1, r2, r3, r4;
int maxx = 0;
int res;
for(int i = 1; i <= n; i++){
int a = zz[i].size();
for(int j = 1; j <= n; j++){
int b = rzz[j].size();
if(i == j || d[j][i] == oo) continue;
for(int k= a - 1; k >= a - 3 && k >= 0; k--){
if(zz[i][k].second == j) continue;
for(int y = b - 1; y >= b - 3 && y >= 0; y--){
if(zz[i][k].second == rzz[j][y].second) continue;
if(rzz[j][y].second == i) continue;
res = d[j][i] + rzz[j][y].first + zz[i][k].first;
if(res > maxx){
maxx = res;
r1 = rzz[j][y].second, r2 = j, r3 = i, r4 = zz[i][k].second;
}
}
}
}
}
//cout<<maxx<<endl;
printf("%d %d %d %d\n", r1, r2, r3, r4);
}
Codeforces 667D World Tour【最短路+枚举】的更多相关文章
- Codeforces 667D World Tour 最短路
链接 Codeforces 667D World Tour 题意 给你一个有向稀疏图,3000个点,5000条边. 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大. 思 ...
- hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- POJ 1161 Walls(最短路+枚举)
POJ 1161 Walls(最短路+枚举) 题目背景 题目大意:题意是说有 n个小镇,他们两两之间可能存在一些墙(不是每两个都有),把整个二维平面分成多个区域,当然这些区域都是一些封闭的多边形(除了 ...
- CJOI 05新年好 (最短路+枚举)
CJOI 05新年好 (最短路+枚举) 重庆城里有n个车站,m条双向公路连接其中的某些车站.每两个车站最多用一条公路连接,从任何一个车站出发都可以经过一条或者多条公路到达其他车站,但不同的路径需要花费 ...
- 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举
2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举 ...
- Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举
题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
- codeforces 667D D. World Tour(最短路)
题目链接: D. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard ...
- World Tour CodeForces - 667D (bfs最短路)
大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大
- Codeforces Round #349 (Div. 2) D. World Tour (最短路)
题目链接:http://codeforces.com/contest/667/problem/D 给你一个有向图,dis[i][j]表示i到j的最短路,让你求dis[u][i] + dis[i][j] ...
随机推荐
- C++ static关键字
一.面向过程中的static 1.修饰全局变量(静态全局变量) (1)静态全局变量在全局数据区分配内存: (2)未经初始化的静态全局变量会被程序自动初始化为0: (3)静态全局变量在申明它的整个文件是 ...
- bootstrap历练实例:按钮作为输入框组前缀或后缀
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- GIMP模板选区操作
选择方法有很多种,这里我就新学的方法记录一下,主要是通过小剪刀和Toggle Quick Mask 相结合的运用. 选择Scissors Select Tool工具 设置基本的属性:Antialisa ...
- django(django项目创建,数据库迁移)
Django项目的创建与介绍 安装:pip3 install django==1.11 查看版本号:django-admin --version 新建项目: 1.切到目标目录 2.django-adm ...
- bat 获取命令执行后的多个返回值,并取最后一个
最近在使用bat,遇到了这样的问题,获取adb shell cat命令之后的所有返回值,查了很长时间,才找到,现分享给大家. 举例如下: @for /f "tokens=*" %% ...
- HDU 3507 斜率优化 DP Print Article
在kuangbin巨巨博客上学的. #include <iostream> #include <cstdio> #include <cstring> #includ ...
- bounds 和frame区别
仔细看下这个图就知道了
- [转]构建Python+Selenium2自动化测试环境(一)
很久没有了解自动化了,最近发现项目中沉淀了很多东西,回归测试效 率很低,所以必须要考虑构建自动化来提供各个环节的小效率.由于忙于需求以及产品的流程规范,现在对于测试技术方面的研究也相对少了很多.不过不 ...
- Clojure基础
最近看了一段clojure,下面是从书上摘下来的一下语言基础的精华部分 ;函数的基本形式 (defn average [numbers] (/ (apply + numbers) (count num ...
- Nmap手册
转自:http://drops.xmd5.com/static/drops/tips-4333.html 0x00:说明 只是一个快速查询手册,理论的东西都没有补充,欢迎大家积极在评论区补充自己常用的 ...