题目链接:

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. wpf仿QQ之窗体翻转

    很久以前在网上找过窗体翻转的Demo,但看得不是很明白,大多是内容的翻转,研究了下,现在分享给大家. 利用UIElement.RenderTransform 属性就能实现元素的呈现位置的转换,因此只需 ...

  2. Summarize code for the three presentation experiments

    Image Picker Controller @IBAction func experiment() { let controller = UIImagePickerController() sel ...

  3. C#中的委托、事件和设计模式(转载)

    引言 委托和事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去的人 ...

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

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

  5. PHP学习之路 (2)

    我们如果坚持用windows做PHP开发,就会错误不断,所以,无论如何都要学习Linux的使用和安装.说实话<鸟哥的linux私房菜>是一本非常不错的书,但是如果你想照着书上面一步一步安装 ...

  6. jquery中的mouseenter实现理解

    说在前面:首先说一下两者之间的区别,假设当前元素为element,mouseover事件具有冒泡特性,也就是说无论鼠标是从别的元素移动到element或者是从element的子元素移动到element ...

  7. jQuery基础知识--选择器与效果

    $(this).hide()-----隐藏当前元素 $("p").hide()------隐藏所有段落 $(".test").hide()--隐藏所有class ...

  8. 用delphiXE7 dbExpress Framework提供的功能获取数据表信息

    uses +  Data.DBXMetaDataNames procedure TMainForm.Button2Click(Sender: TObject);var  Cmd: TDBXComman ...

  9. ASCII码详解

    ASCII码详解 ASCII码表 ASCII码大致可以分作三部分組成.第一部分是:ASCII非打印控制字符: 第二部分是:ASCII打印字符: 第三部分是:扩展ASCII打印字符. 第一部分:ASCI ...

  10. SIMATIC IT HISTORIAN在烟用二醋酸纤维素生产中应用

    原文转载自:http://www.soft6.com/tech/5/54287.html 本文介绍了西门子MES核心产品SIMATIC IT HISTORIAN实时数据库及客户端工具在流程生产中的具体 ...