Codeforces 667D World Tour 最短路
链接
题意
给你一个有向稀疏图,3000个点,5000条边。 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大。
思路
枚举中间两个点,端点就是不与这三个点重复的最大的那个点来更新答案。因为是稀疏图,可以做n遍spfa来维护两两之间的最短路。
代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <string>
#define LL long long
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN 3005
#define MAXM 5005
using namespace std;
struct Edge{
int to, next;
}edges[MAXM];
int head[MAXN];
int tot;
bool vis[MAXN];
int d[MAXN][MAXN];
int res[5];
vector<pair<int, int> > dis1[MAXN], dis2[MAXN];
void add_edge(int x, int y){
edges[tot].to = y;
edges[tot].next = head[x];
head[x] = tot++;
}
void spfa(int s){
queue<int> Q;
memset(vis, 0, sizeof(vis));
Q.push(s);
d[s][s] = 0;
while (!Q.empty()){
int x = Q.front(); Q.pop();
for (int i = head[x]; i != -1; i = edges[i].next){
int y = edges[i].to;
if (d[s][x] + 1 < d[s][y]){
d[s][y] = d[s][x] + 1;
}
if (vis[y]) continue;
vis[y] = true;
Q.push(y);
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int n, m;
scanf("%d%d", &n, &m);
memset(head, -1, sizeof(head));
tot = 0;
int x, y;
for (int i = 1; i <= m; ++i){
scanf("%d%d", &x, &y);
add_edge(x, y);
}
memset(d, INF, sizeof(d));
for (int i = 1; i <= n; ++i){
spfa(i);
}
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= n; ++j){
if (d[i][j] != INF){
dis1[i].push_back(make_pair(d[i][j], j));
dis2[j].push_back(make_pair(d[i][j], i));
}
}
}
for (int i = 1; i <= n; ++i){
sort(dis1[i].rbegin(), dis1[i].rend());
sort(dis2[i].rbegin(), dis2[i].rend());
}
int ans = 0;
memset(res, 0, sizeof(res));
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= n; ++j){
if (i == j) continue;
if (d[i][j] == INF) continue;
for (int k = 0; k < min(3, (int)dis2[i].size()); ++k){
if (i == dis2[i][k].second || j == dis2[i][k].second) continue;
for (int l = 0; l < min(3, (int)dis1[j].size()); ++l){
if (j != dis1[j][l].second && i != dis1[j][l].second && dis2[i][k].second != dis1[j][l].second){
int temp = d[i][j] + dis2[i][k].first + dis1[j][l].first;
if (temp > ans){
ans = temp;
res[0] = dis2[i][k].second, res[1] = i, res[2] = j, res[3] = dis1[j][l].second;
}
}
}
}
}
}
//printf("%d\n", ans);
for (int i = 0; i < 4; ++i){
printf("%d ", res[i]);
}
printf("\n");
}
Codeforces 667D World Tour 最短路的更多相关文章
- Codeforces 667D World Tour【最短路+枚举】
垃圾csdn,累感不爱! 题目链接: http://codeforces.com/contest/667/problem/D 题意: 在有向图中找到四个点,使得这些点之间的最短距离之和最大. 分析: ...
- codeforces 667D D. World Tour(最短路)
题目链接: D. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard ...
- World Tour CodeForces - 667D (bfs最短路)
大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大
- 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 最短路+暴力枚举
题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
- CodeForces 666B World Tour(spfa+枚举)
B. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard input ...
- Codeforces 543B Destroying Roads(最短路)
题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...
- Codeforces 490F. Treeland Tour 暴力+LIS
枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...
- Codeforces 490F Treeland Tour 树形dp
Treeland Tour 离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快.. #include<bits/stdc++.h> #define LL long ...
随机推荐
- Qt-信号和槽-多对多
前言:介绍1对多,多对1以及多对多的案例. 一.1对多 演示内容:在QLineEdit输入时,同步label,text browser以及调试输出板同步显示. 1.1 新建工程 1.2 添加部件 拖入 ...
- Spring《四-一》解决自动装配的问题
自动化装配使得研发减少了响应的指配工作,但是使得响应的检查难以完成. 解决方法: simple模式: <bean autowire="autodetect" dependen ...
- Ubuntu14.04下初步使用MongoDB
不多说,直接上干货! Ubuntu14.04下Mongodb(在线安装方式|apt-get)安装部署步骤(图文详解)(博主推荐) shell命令模式 输入mongo进入shell命令模式,默认连接的数 ...
- PHP 环境搭建工具
PHP环境搭建工具 一键集成工具 直接安装后部署到相关目录即可浏览 phpStudy 下载地址:https://pan.baidu.com/s/1i6C3Ph7
- js数组及数组对象的遍历
一 数组遍历 方法一:for循环 方法二:forEach遍历 forEach遍历数组 性能低于for循环,且不可使用break中断循环,也不能使用return返回外层函数 arr.forEach(fu ...
- 使用http-server开启一个本地服务器
前言 在写前端页面中,经常会在浏览器运行HTML页面,从本地文件夹中直接打开的一般都是file协议,当代码中存在http或https的链接时,HTML页面就无法正常打开,为了解决这种情况,需要在在本地 ...
- CSS实现栅格布局
CSS实现栅格布局 设置容器container: .grid-container { width: 100%; max-width: 1200px; } 清除浮动: .row:before, .row ...
- 编码介绍(ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE)
转载:http://blog.jobbole.com/30526/(前面内容)和http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf ...
- ZBrush中Magnify膨胀笔刷介绍
Magnify膨胀笔刷是ZBrush®笔刷中经常使用的,利用该笔刷可绘制中心向四周膨胀的效果.本文内容向大家介绍ZBrush®中膨胀笔刷以便大家熟悉它的用法和特性. Magnify膨胀笔刷 Magni ...
- tcpsock for Golang
前记:本文所述的 tcpsock 库托管在 Github. Golang 中的 net 标准库已对 TCP 网络编程作了简洁(却很不简单)的封装,基本上,可直接通过引用其提供的相关接口开发简易的网络应 ...