Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路
B. World Tour
题目连接:
http://www.codeforces.com/contest/666/problem/B
Description
A famous sculptor Cicasso goes to a world tour!
Well, it is not actually a world-wide. But not everyone should have the opportunity to see works of sculptor, shouldn't he? Otherwise there will be no any exclusivity. So Cicasso will entirely hold the world tour in his native country — Berland.
Cicasso is very devoted to his work and he wants to be distracted as little as possible. Therefore he will visit only four cities. These cities will be different, so no one could think that he has "favourites". Of course, to save money, he will chose the shortest paths between these cities. But as you have probably guessed, Cicasso is a weird person. Although he doesn't like to organize exhibitions, he likes to travel around the country and enjoy its scenery. So he wants the total distance which he will travel to be as large as possible. However, the sculptor is bad in planning, so he asks you for help.
There are n cities and m one-way roads in Berland. You have to choose four different cities, which Cicasso will visit and also determine the order in which he will visit them. So that the total distance he will travel, if he visits cities in your order, starting from the first city in your list, and ending in the last, choosing each time the shortest route between a pair of cities — will be the largest.
Note that intermediate routes may pass through the cities, which are assigned to the tour, as well as pass twice through the same city. For example, the tour can look like that: . Four cities in the order of visiting marked as overlines: [1, 5, 2, 4].
Note that Berland is a high-tech country. So using nanotechnologies all roads were altered so that they have the same length. For the same reason moving using regular cars is not very popular in the country, and it can happen that there are such pairs of cities, one of which generally can not be reached by car from the other one. However, Cicasso is very conservative and cannot travel without the car. Choose cities so that the sculptor can make the tour using only the automobile. It is guaranteed that it is always possible to do.
Input
In the first line there is a pair of integers n and m (4 ≤ n ≤ 3000, 3 ≤ m ≤ 5000) — a number of cities and one-way roads in Berland.
Each of the next m lines contains a pair of integers ui, vi (1 ≤ ui, vi ≤ n) — a one-way road from the city ui to the city vi. Note that ui and vi are not required to be distinct. Moreover, it can be several one-way roads between the same pair of cities.
Output
Print four integers — numbers of cities which Cicasso will visit according to optimal choice of the route. Numbers of cities should be printed in the order that Cicasso will visit them. If there are multiple solutions, print any of them.
Sample Input
8 9
1 2
2 3
3 4
4 1
4 5
5 6
6 7
7 8
8 5
Sample Output
2 1 8 7
Hint
题意
给你一个有向图,你需要选出四个点出来,使得dis[i][j]+dis[j][t]+dis[t][k]最大
当然这四个点一定得是可达的
题解:
暴力枚举j,t这两个点,然后我们去选择i和k这两个点就好了
这个我们可以一开始n^2logn去预处理两两之间的最短路
然后去暴力枚举就好了,i和k这俩显然就只可能是最大的那3个中的一个。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e3+7;
int n,m;
int d[maxn][maxn];
int inq[maxn];
vector<int>E[maxn];
vector<pair<int,int> >fi[maxn];
vector<pair<int,int> >se[maxn];
void spfa(int x)
{
memset(inq,0,sizeof(inq));
for(int i=1;i<=n;i++)
d[x][i]=1e9;
d[x][x]=0;
queue<int> Q;
Q.push(x);
inq[x]=1;
while(!Q.empty())
{
int now = Q.front();
Q.pop();
inq[now]=0;
for(int i=0;i<E[now].size();i++)
{
int next = E[now][i];
if(d[x][next]>d[x][now]+1)
{
d[x][next]=d[x][now]+1;
if(!inq[next])
{
inq[next]=1;
Q.push(next);
}
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
E[x].push_back(y);
}
for(int i=1;i<=n;i++)spfa(i);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)continue;
if(d[i][j]!=1e9)fi[i].push_back(make_pair(d[i][j],j));
if(d[j][i]!=1e9)se[i].push_back(make_pair(d[j][i],j));
}
sort(fi[i].begin(),fi[i].end());
reverse(fi[i].begin(),fi[i].end());
sort(se[i].begin(),se[i].end());
reverse(se[i].begin(),se[i].end());
}
long long ans=0,ans1=0,ans2=0,ans3=0,ans4=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)continue;
if(d[j][i]==1e9)continue;
for(int k=0;k<3&&k<fi[i].size();k++)
{
if(i==fi[i][k].second)continue;
if(j==fi[i][k].second)continue;
for(int t=0;t<3&&t<se[j].size();t++)
{
if(fi[i][k].second==se[j][t].second)continue;
if(i==se[j][t].second)continue;
if(j==se[j][t].second)continue;
long long tmp = d[j][i]+fi[i][k].first+se[j][t].first;
if(tmp>ans)
{
ans = tmp;
ans1 = se[j][t].second;
ans2 = j;
ans3 = i;
ans4 = fi[i][k].second;
}
}
}
}
}
cout<<ans1<<" "<<ans2<<" "<<ans3<<" "<<ans4<<endl;
}
Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路的更多相关文章
- 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 #349 (Div. 1) B. World Tour 最短路+暴力枚举
题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
- 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) 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 ...
- Codeforces Round #349 (Div. 1)E. Forensic Examination
题意:给一个初始串s,和m个模式串,q次查询每次问你第l到第r个模式串中包含\(s_l-s_r\)子串的最大数量是多少 题解:把初始串和模式串用分隔符间隔然后建sam,我们需要找到在sam中表示\(s ...
- Codeforces Round #349 (Div. 2)
第一题直接算就行了为了追求手速忘了输出yes导致wa了一发... 第二题技巧题,直接sort,然后把最大的和其他的相减就是构成一条直线,为了满足条件就+1 #include<map> #i ...
- Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set
C. Reberland Linguistics First-rate specialists graduate from Berland State Institute of Peace a ...
随机推荐
- Vagrant 无法校验手动下载的 Homestead Box 版本
起因 4年前电脑,配置不太好了,现有的 Homestead 运行起来太吃内存.在修改了 Homestead.yaml 文件里 memory 选项的内存配置为 1024 后,应用最新配置重启失败. 索性 ...
- C# 执行固定个数任务自行控制进入线程池的线程数量,多任务同时但是并发数据限定
思路来源:http://bbs.csdn.NET/topics/390819824,引用该页面某网友提供的方法. 题目:我现在有100个任务,需要多线程去完成,但是要限定同时并发数量不能超过5个. 原 ...
- c++语言知识点汇总
c++ primer version-5 的整理 section 1: 内置类型和自定义类型: main函数的返回值:指示状态.0:成功:1:系统定义. unix和win系统中,执行完程序可以使用ec ...
- mysql高可用架构 -> MHA简介-01
作者简介 松信嘉範:MySQL/Linux专家2001年索尼公司入职2001年开始使用oracle2004年开始使用MySQL2006年9月-2010年8月MySQL从事顾问2010年-2012年 D ...
- Autofac Named命名和Key Service服务
参考:http://www.cnblogs.com/wolegequ/archive/2012/06/03/2532605.html
- 使用html+css+js实现倒计时,开启你痛苦的倒计时吧
使用html+css+js实现倒计时,开启你痛苦的倒计时吧 效果图: 这是我痛苦的倒计时,呜呜呜 好啦,再痛苦还是要分享代码,代码如下,复制即可使用: <!DOCTYPE html> &l ...
- JS实现全选、反选、不选
JS实现全选.反选.不选 效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- (转载)使用SQL-Server创建一个银行数据管理系统Ⅰ
首先,要创建一个完整的数据管理系统,不是一蹴而就的,一定要要一步一步的来,不断完善,最终方能达到自己想要的结果,所以我在这里也是一点一点分步来做的. 创建数据库,数据库属性在这里用的是默认(不推荐使用 ...
- Kubernetes监控:部署Heapster、InfluxDB和Grafana
本节内容: Kubernetes 监控方案 Heapster.InfluxDB和Grafana介绍 安装配置Heapster.InfluxDB和Grafana 访问 grafana 访问 influx ...
- 一步一步学习IdentityServer3 (10)
在某些服务器环境下 identityserver3 会闹情绪, 比如在google浏览器下授权失败(陷入死循环) 查了很多资料好像然并卵 Microsoft.Owin.Security.Notific ...