题目链接:

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 最短路+暴力枚举的更多相关文章

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

  2. Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路

    B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...

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

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

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

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

  7. Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划

    A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...

  8. Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp

    题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...

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

随机推荐

  1. input 文本框和 img 验证码对齐问题

    input 文本框和 img 验证码对齐问题 在网页制作中,常将 input 和 img 放在同一行,img标签总是比input高出一个头,非常难看. CCS实现input和img水平对齐的方法 同时 ...

  2. 利用curl抓取远程页面内容

    最基本的操作如下 $curlPost = 'a=1&b=2';//模拟POST数据$cookie_file    =    tempnam('./temp','kie');//可选,保存ses ...

  3. 快速清理Visual Studio起始页最近打开项目

    清除vs2008起始页最近打开项目 第一种:最简单的方式: 把以下内容保存为.bat批处理文件 @echo off@REG Delete HKCU\Software\Microsoft\VisualS ...

  4. jquery二级目录选中当前页的样式

    <div class="navlist clear"> <span><a href="/M/Page_32.html" title ...

  5. 《第一行代码--Android》阅读笔记之数据持久化

    1.升级数据库 为了避免手工清空数据(或卸载重装APP),重写SQLiteOpenHelper里面的onUpgrade()方法   引用自http://blog.csdn.net/longvslove ...

  6. 各种数据处理方案(SQL,NoSQL,其他)的应用场景

    综合stackoverflow和linkin上的相关讨论,还有我个人的工作经验:   Redis应用场景(大部分场景下memcache可以用Redis代替,所以不单独讨论) 线上业务,读写的高性能要求 ...

  7. Centos 7.1+CDH5.7.2全部署流程

    前期准备: JDK环境 版本:jdk-8u101-linux-x64.rpm 下载地址:oracle官网 mysql rpm包:http://dev.mysql.com/get/Downloads/M ...

  8. php __clone需要注意的问题

      当一个对象的属性是另外一个对象时,当有一个对象复制该对象时,当复制到这个属性(一个对象)时,只复制这个属性(对象)的引用,而不复制引用的对象. class Account{ public $bal ...

  9. UCOS2_STM32F1移植详细过程(三)

    Ⅰ.概述 上一篇文章是讲述ST芯片相关的配置和OS裁剪相关的配置,接着上一篇文章来讲述关于UCOS的移植,该文主要针对uC/OS-II Ports下面os_cpu_a.asm.os_cpu_c.c和o ...

  10. PuTTY 中文教程

    PuTTY 中文教程 更新记录 2006-11-29初步完成想写的这些东西 2007-06-11PuTTY 的最新版本到了0.6:修改了一下 SSH 隧道:添加了 SSH 反向隧道:添加了用 SSH ...