博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路
阅读量:7060 次
发布时间:2019-06-28

本文共 4291 字,大约阅读时间需要 14 分钟。

B. World Tour

题目连接:

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
using namespace std;const int maxn = 3e3+7;int n,m;int d[maxn][maxn];int inq[maxn];vector
E[maxn];vector
>fi[maxn];vector
>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
Q; Q.push(x); inq[x]=1; while(!Q.empty()) { int now = Q.front(); Q.pop(); inq[now]=0; for(int i=0;i
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
ans) { ans = tmp; ans1 = se[j][t].second; ans2 = j; ans3 = i; ans4 = fi[i][k].second; } } } } } cout<
<<" "<
<<" "<
<<" "<
<

转载地址:http://czfll.baihongyu.com/

你可能感兴趣的文章
专业医疗仪器设备走入千家万户 安全引担忧
查看>>
不常见但是很有用的gcc命令行选项(二)
查看>>
闪存趋势可能导致用户回归硬盘
查看>>
未来物联网影响商业的三种表现
查看>>
降低大数据合规风险的三个要点
查看>>
100G及超100G数据中心光模块的技术选择
查看>>
如何整理磁盘碎片让Windows 7电脑运行更快?
查看>>
智能投影的“淘金热”
查看>>
红杉入股LIFX,智能你个灯泡
查看>>
微信小程序架构分析 (下)
查看>>
红帽Chris:软件定义带动开源存储发展
查看>>
如何设计未来的数据中心
查看>>
微软Surface Phone泄密:高通骁龙830芯片明年上市
查看>>
传日本NTT Data在竞购戴尔Perot Systems
查看>>
从追随到超越 本土电子测量仪器的“极限挑战”
查看>>
[原创]nginx写日志时机与tcp write写成功是否送达对端疑问解析
查看>>
微软心塞!Win10不升反降 Win7继续上升
查看>>
OPPO“反水”:联发科预计第四季度营收毛利双降
查看>>
欧盟计划2018年启动十亿欧元量子技术项目
查看>>
OA系统为何在企业立足
查看>>