垃圾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【最短路+枚举】的更多相关文章

  1. Codeforces 667D World Tour 最短路

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

  2. 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 ...

  3. POJ 1161 Walls(最短路+枚举)

    POJ 1161 Walls(最短路+枚举) 题目背景 题目大意:题意是说有 n个小镇,他们两两之间可能存在一些墙(不是每两个都有),把整个二维平面分成多个区域,当然这些区域都是一些封闭的多边形(除了 ...

  4. CJOI 05新年好 (最短路+枚举)

    CJOI 05新年好 (最短路+枚举) 重庆城里有n个车站,m条双向公路连接其中的某些车站.每两个车站最多用一条公路连接,从任何一个车站出发都可以经过一条或者多条公路到达其他车站,但不同的路径需要花费 ...

  5. 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 -二分+最短路+枚举 ...

  6. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  7. codeforces 667D D. World Tour(最短路)

    题目链接: D. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard ...

  8. World Tour CodeForces - 667D (bfs最短路)

    大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大

  9. 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] ...

随机推荐

  1. Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'userController' method

    在使用SpringMVC的时候遇到了这个问题 问题原因:  在指定方法所对应的url地址的时候重复了, 也就是@RequestMapping("url")中, 两个方法使用了同一个 ...

  2. 标准C中字符串分割方法

    ◆ 使用strtok函数分割. 原型:char *strtok(char *s, char *delim); strtok在s中查找包含在delim中的字符并用NULL('\0')来替换,直到找遍整个 ...

  3. Web字节码(WebAssembly) Emscripten编译器安装

    首先你需要提前安装 git python 环境并且Ctrl+R输入cmd在windows的dos界面下能够运行 第一步: 在github上downloade下来emsdk git clone http ...

  4. OpenCV2.4.11+VS2012的环境配置+“fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突”的问题解决

    本来OpenCV环境配置的问题是个基础问题,但是步骤有点小烦,所以几乎每次都要百度一下,加上这次遇到的“fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突 ...

  5. css3 filter(滤镜)属性汇总与使用介绍,来源W3C

    实例 修改所有图片的颜色为黑白 (100% 灰度): img { -webkit-filter: grayscale(%); /* Chrome, Safari, Opera */ filter: g ...

  6. day12-图

  7. mat 和IPIImage之间的转换

    opencv2.3.1 Mat::operator IplImageCreates the IplImage header for the matrix.C++: Mat::operator IplI ...

  8. LeetCode(9)Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  9. Verilog学习笔记基本语法篇(五)········ 条件语句

    条件语句可以分为if_else语句和case语句两张部分. A)if_else语句 三种表达形式 1) if(表达式)          2)if(表达式)               3)if(表达 ...

  10. OpenGLES2.0着色器语言glsl

    OpenGLES2.0中是强制使用可编程的渲染管线的,使用的是glsl着色器语言,因为着色器语言是使用的GPU,即图形处理单元,而不是CPU,这样可以使CPU从繁重的几何计算和像素的处理中解脱出来了. ...