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 ...
随机推荐
- CentOS 5.x版本升级Mysql
#-----------------------------CentOS 5.x版本升级Mysql ------------------#! /bin/sh #1.关闭selinuxcp -rp /e ...
- Stop PeopleCode Processing with Error
A blunt, but useful method for debugging PeopleCode is to use the inbuilt "Error" function ...
- Webservice SOAP传输序列化总结 以及webservice之序列化以及反序列化实例
一.所有Webservice中传递的对象都必须能够序列化,这个是作为在网络之间传输的必要条件.XML WebService和SOAP标准支持的数据类型如下: 1.基本数据类型. 标准类型,如:int ...
- asp.net过滤HTML标签的几个函数
以下是引用片段: ----- /**/ /// <summary> /// 去除HTML标记 /// </summary> /// <param name="N ...
- nginx+php与apache+php性能对比
测试工具http_load相同的动态页面测试,相同的硬件资源,相同并发,相同请求数量的前提下,nginx+php比apache+php的性能要 差,而且如果请求的压力大于硬件资源的承受能力,nginx ...
- DevExpress 表中数据导出
gridView1.ExportToXlsx("SampleStock.xlsx"); if (true) { DevExpress.XtraEditors.XtraMessage ...
- asp.net mvc razor html encoding
HTML Encoding 为了跨站点的脚本攻击,Razor 语法会直接将脚本代码编码输出. @{string message = "<script>alert('haacked ...
- MapReduce实现的Join
MapReduce Join 对两份数据data1和data2进行关键词连接是一个很通用的问题,如果数据量比较小,可以在内存中完成连接. 如果数据量比较大,在内存进行连接操会发生OOM.mapredu ...
- 008-python基础-数据类型
一.基本数据类型: 数字 int 字符串 str 布尔值 bool 真或假 1或0 列表 list 元组 tuple (不可变列表) 字典 dict (无序)
- MongoDB 学习笔记(二)—— MongoDB Shell
MongoDB自带一个JavaScript shell 可以从命令行中与MongoDB交互,功能非常强大.如在上一节最后一张图所看到,可以执行JavaScript程序. 运行Shell 前提是启动Mo ...