Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举
题目链接:
http://www.codeforces.com/contest/666/problem/B
题意:
给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一个满足条件的四个点。
题解:
首先预处理出任意两点的最短距离,用队列优化的spfa跑:O(n*n*logn)
现依次访问四个点:v1,v2,v3,v4
我们可以枚举v2,v3,然后求出v2的最远点v1,v3的最远点v4,为了保证这四个点的不同,直接用最远点会错,v1,v4相同时还要考虑次最远点来替换,反正解肯定是在离v2最远的三个点里面,在离v3最远的三个点里面,这个可以暴力枚举(3*3)。
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn = 3e3 + ;
const int INF = 0x3f3f3f3f; vector<int> G[maxn];
int mat[maxn][maxn]; int n, m; int dis[maxn], inq[maxn];
void spfa(int s) {
queue<int> Q;
memset(inq, , sizeof(inq));
for (int i = ; i < n; i++) dis[i] = INF;
dis[s] = ; inq[s] = ; Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = false;
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i];
if (dis[v] > dis[u] + ) {
dis[v] = dis[u] + ;
if (!inq[v]) {
Q.push(v); inq[v] = true;
}
}
}
}
for (int i = ; i < n; i++) {
mat[s][i] = dis[i];
}
} int ma_out[maxn][];
int ma_in[maxn][];
//处理离i最远的三个顶点,要考虑两种情况,即进入i的点和从i出去的点
void pre_ma() {
for (int i = ; i < n; i++) {
ma_out[i][] = ma_out[i][] =ma_out[i][]= -;
for (int j = ; j < n; j++) {
if (mat[i][j] >= INF) continue;
if (ma_out[i][]==-||mat[i][ma_out[i][]] < mat[i][j]) {
ma_out[i][] = ma_out[i][];
ma_out[i][] = ma_out[i][];
ma_out[i][] = j;
}
else if (ma_out[i][]==-||mat[i][ma_out[i][]] < mat[i][j]) {
ma_out[i][] = ma_out[i][];
ma_out[i][] = j;
}
else if (ma_out[i][]==-||mat[i][ma_out[i][]] < mat[i][j]) {
ma_out[i][] = j;
}
}
}
for (int i = ; i < n; i++) {
ma_in[i][] = ma_in[i][] = ma_in[i][] = -;
for (int j = ; j < n; j++) {
if (mat[j][i] >= INF) continue;
if (ma_in[i][]==-||mat[ma_in[i][]][i] < mat[j][i]) {
ma_in[i][] = ma_in[i][];
ma_in[i][] = ma_in[i][];
ma_in[i][] = j;
}
else if (ma_in[i][]==-||mat[ma_in[i][]][i] < mat[j][i]) {
ma_in[i][] = ma_in[i][];
ma_in[i][] = j;
}
else if (ma_in[i][]==-||mat[ma_in[i][]][i] < mat[j][i]) {
ma_in[i][] = j;
}
}
}
} void init() {
for (int i = ; i < n; i++) G[i].clear();
} int main() {
while (scanf("%d%d", &n, &m) == && n) {
init();
for (int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v); u--, v--;
G[u].push_back(v);
}
for (int i = ; i < n; i++) spfa(i);
pre_ma();
int ans = -;
int nds[] = { };
//枚举中间两个点,再算旁边两个点
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
if (i == j) continue;
if (mat[i][j] >= INF) continue;
for (int k = ; k < ; k++) {
for (int l = ; l < ; l++) {
if (ma_in[i][k] == - || ma_out[j][l] == -) continue;
if (ma_in[i][k] == i || ma_in[i][k] == j || ma_in[i][k] == ma_out[j][l]) continue;
if (ma_out[j][l] == i || ma_out[j][l] == j) continue;
if (ans < mat[ma_in[i][k]][i] + mat[i][j] + mat[j][ma_out[j][l]]) {
ans = mat[ma_in[i][k]][i] + mat[i][j] + mat[j][ma_out[j][l]];
nds[] = ma_in[i][k];
nds[] = i;
nds[] = j;
nds[] = ma_out[j][l];
}
}
}
}
}
printf("%d %d %d %d\n", nds[] + , nds[] + , nds[] + , nds[] + );
}
return ;
}
Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举的更多相关文章
- 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] ...
- Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路
B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...
- Codeforces Round #298 (Div. 2) B. Covered Path 物理题/暴力枚举
B. Covered Path Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/probl ...
- Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路
D. World Tour A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wid ...
- Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)
A. Mike and palindrome time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- Codeforces Round #130 (Div. 2) C - Police Station 最短路+dp
题目链接: http://codeforces.com/problemset/problem/208/C C. Police Station time limit per test:2 seconds ...
- Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划
A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...
- Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp
题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...
- Codeforces Round #349 (Div. 2) C. Reberland Linguistics (DP)
C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...
随机推荐
- c#中Split函数的使用介绍
平时经常用到split,在这里做一个系统的总结. Split函数 作用 返回一个下标从零开始的一维数组,它包含指定数目的子字符串. 语法 Split(expression[, ...
- 常规轮询请求,客户端用Ajax调webservice的方法
服务端发布webservice,下图方框中的一定要有 客户端代码 <script type="text/javascript"> $(document).ready(f ...
- event.preventDefault()
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 使用DBCP时发生AbstractMethodError异常
使用DBCP时发生AbstractMethodError异常,错误描述: Exception in thread "main" java.lang.AbstractMethodEr ...
- PHP文件上传错误类型及说明
从 PHP 4.2.0 开始,PHP 将随文件信息数组一起返回一个对应的错误代码.该代码可以在文件上传时生成的文件数组中的 error 字段中被找到,也就是 $_FILES['userfile'][' ...
- 个人代码管理--svn
通常开发中遇到自己电脑和公司电脑代码共享的问题.比如一些通用的库,图片等项目中基本通用. 一些项目库如google code, github内地访问又挺困难的,常常无法连接,或者慢死..还有就是必须开 ...
- 【转】Doscommand-操作Cmd的第三方控件
核心用法: Doscommand1.CommandLine := 'cmd /c' +[命令];Doscommand1.OutputLines :=Memo1.Lines;Doscommand1.Ex ...
- 使用DataGridView数据窗口控件,构建用户快速输入体验
在"随风飘散" 博客里面,介绍了一个不错的DataGridView数据窗口控件<DataGridView数据窗口控件开发方法及其源码提供下载>,这种控件在有些场合下,还 ...
- linuxok6410的I2C驱动分析---用户态驱动
3 i2c-dev 3.1 概述 之前在介绍I2C子系统时,提到过使用i2c-dev.c文件在应用程序中实现我们的I2C从设备驱动.不过,它实现的是一个虚拟,临时的i2c_client,随着设备文件 ...
- [terry笔记]Oracle数据泵-schema导入导出
数据泵是10g推出的功能,个人倒数据比较喜欢用数据泵. 其导入的时候利用remap参数很方便转换表空间以及schema,并且可以忽略服务端与客户端字符集问题(exp/imp需要排查字符集). 数据泵也 ...