题目链接:

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. ios网络:应用一个请求的7个步骤

    Splitting big tasks into small tasks is often one of the best ways to solve a problem. Thus, in the ...

  2. CentOS用户权限管理--su与sudo

    Linux权限管理--su与sudo 1.su用来切换登录的用户,比如当前用户为chen,可以用su zhu,并输入用户zhu的登录密码,就可以切换到用户zhu.如果一个普通用户想切换到root用户, ...

  3. 微软必应·英雄会第三届在线编程大赛:几个bing?

    发布公司:微软亚太研发集团 有 效 期:2013-12-31至2014-02-01 难 度 等 级: 答 题 时 长:120分钟 编程语言要求:C C++ Java C# 悬赏详情 一等奖 : 价值2 ...

  4. mysql列属性auto(mysql笔记四)

    常见的的是一个字段不为null存在默认值 没值得时候才去找默认值,可以插入一个null到 可以为null的行里 主键:可以唯一标识某条记录的字段或者字段的集合 主键设置 主键不可为null,声明时自动 ...

  5. 解析 this.initialize.apply(this, arguments)

    一. 起因 那天用到prototype.js于是打开看看,才看几行就满头雾水,原因是对js的面向对象不是很熟悉,于是百度+google了一把,最后终于算小有收获,写此纪念一下^_^. prototyp ...

  6. 13)Java static

    1.static变量      按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的区别是:    ...

  7. delphi 基础之四 delphi 组织结构

    delphi 组织结构 在Delphi中,一个正在开发的应用程序可以被称作项目或者工程.一般地,一个项目主要由dpr(项目).pas(单元)和dfm(窗体)三种文件组成,另外还有一些附属文件,如res ...

  8. 利用HttpWebRequest访问WebApi

    WebApi现在越来越流行,下面给出利用HttpWebRequest访问WebApi的工具方法: 1.利用基准URL和参数字典生成完整URL /// <summary> /// 生成URL ...

  9. Ioc 控制反转 实例

    关于IOC 或者是DI 什么的真的很坑爹. 开始理解了这东西了然后闲的没事就又百度了一下,得  我又凌乱了.  看了两个大神的贴 尼玛啊 完全是反过来了. 纠结了半天.然后就想找个简单点不坑爹的原理代 ...

  10. Python学习教程(learning Python)--3.3.2 Python的关系运算

    如果if的condition不用布尔表达式来做条件判断而采用关系表达式,实际上关系表达式运算的结果要么是True要么是False.下面我们先了解一些有关关系运算符的基础知识,如下表所示. 做个小程序测 ...