D. World Tour
 

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 uiand 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.

Example
input
8 9
1 2
2 3
3 4
4 1
4 5
5 6
6 7
7 8
8 5
output
2 1 8 7
Note

Let d(x, y) be the shortest distance between cities x and y. Then in the example d(2, 1) = 3, d(1, 8) = 7, d(8, 7) = 3. The total distance equals 13.

题意:

  给你一个有向图,有环,让你找出四个不同点,使得d[a,b]+d[b,c]+d[c,d]之值最大

题解:

  

#include<bits/stdc++.h>
using namespace std;
const int N = 3e3+, M = 1e6+, mod = 1e9+, inf = 1e9+;
typedef long long ll; int dist[N][N], vis[N], a , b ,n , m ,a1,a2,a3,a4;
vector<int > G[N];
vector<pair<int,int > > fd[N],d[N];
void add(int x,int y) {G[x].push_back(y);}
int main() {
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++) scanf("%d%d",&a,&b), add(a,b);
for(int i=;i<=n;i++) {
for(int j=;j<=n;j++) dist[i][j] = inf, vis[j] = ;
queue<int >q;
q.push(i);
vis[i] = ;
dist[i][i] = ;
while(!q.empty()) {
int k = q.front();
q.pop();
vis[k] = ;
for(int j=;j<G[k].size();j++) {
int to = G[k][j];
if(dist[i][to]>dist[i][k] + ) {
dist[i][to] = dist[i][k] + ;
if(!vis[to]) {
q.push(to);
vis[to] = ;
}
}
}
}
for(int j=;j<=n;j++) {
if(dist[i][j]>=inf) continue;
d[i].push_back(make_pair(dist[i][j],j));
fd[j].push_back(make_pair(dist[i][j],i));
}
sort(d[i].begin(),d[i].end());
} for(int i=;i<=n;i++) sort(fd[i].begin(),fd[i].end());
int sum = ,ans1,ans2,ans3,ans4;
for(int i=;i<=n;i++) {
for(int j=;j<=n;j++) {
if(i == j) continue;
ans2 = i;
ans3 = j;
int tmp = dist[ans2][ans3];//cout<<1<<endl;
if(tmp>=inf) continue;
int siz = d[ans3].size();
if(siz==) continue;
if(d[ans3][siz-].second == ans2||d[ans3][siz-].second==ans3) {
if(siz==) continue;
ans4 = d[ans3][siz-].second;
tmp += d[ans3][siz-].first;
}
else {
ans4 = d[ans3][siz-].second;
tmp += d[ans3][siz-].first;
}
int f = ;
siz = fd[ans2].size();
for(int k=siz-;k>=;k--) {
int now = fd[ans2][k].second;
int value = fd[ans2][k].first;
if(ans4!=now&&ans2!=now&&ans3!=now) {
ans1 = now;
tmp += value;f = ;
break;
}
} if(tmp>sum&&f) {
a1 = ans1;
a2 = ans2;
a3 = ans3;
a4 = ans4;
sum = tmp;
}
}
}
// cout<<dist[2][1]<<" "<<dist[1][8]<<" "<<dist[8][7]<<endl;
// cout<<dist[a1][a2]<<" "<<dist[a2][a3]<<" "<<dist[a3][a4]<<endl;
// cout<<sum<<endl;
cout<<a1<<" "<<a2<<" "<<a3<<" "<<a4<<endl;
return ;
}

Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路的更多相关文章

  1. Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路

    B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...

  2. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  3. 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] ...

  4. Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划

    A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...

  5. Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp

    题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...

  6. 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 ...

  7. Codeforces Round #349 (Div. 1)E. Forensic Examination

    题意:给一个初始串s,和m个模式串,q次查询每次问你第l到第r个模式串中包含\(s_l-s_r\)子串的最大数量是多少 题解:把初始串和模式串用分隔符间隔然后建sam,我们需要找到在sam中表示\(s ...

  8. Codeforces Round #349 (Div. 2)

    第一题直接算就行了为了追求手速忘了输出yes导致wa了一发... 第二题技巧题,直接sort,然后把最大的和其他的相减就是构成一条直线,为了满足条件就+1 #include<map> #i ...

  9. Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set

    C. Reberland Linguistics     First-rate specialists graduate from Berland State Institute of Peace a ...

随机推荐

  1. A - High School: Become Human

    Problem description Year 2118. Androids are in mass production for decades now, and they do all the ...

  2. Obsolete---标记方法 类过期

    最近做一个接口的修改,由于是很老的接口,不太了解外部有多少地方引用了它. 但是内部的方法由于业务发展已经不太适合现在的需求,想改又不该动.所以想到了如果设置为过期. Obsolete 属性将某个程序实 ...

  3. swift-delegate(代理)或者block传值

    1:delegate或者block传值 import UIKit class ViewController: UIViewController,TestDelegatePassValueDelegat ...

  4. 网站html代码解析

    1.什么是HTML文件?HTML中文叫做“超文本标记语言”,一个HTML文件不仅包含文本内容,还包含一些标记,一个HTML文件的后缀名是.htm或者是.html.用文本编辑器(Dreamweaver) ...

  5. android黑科技系列——Xposed框架实现拦截系统方法详解

    一.前言 关于Xposed框架相信大家应该不陌生了,他是Android中Hook技术的一个著名的框架,还有一个框架是CydiaSubstrate,但是这个框架是收费的,而且个人觉得不怎么好用,而Xpo ...

  6. java编程基础篇-------> 从键盘输入一位整数,代表月份,编程判断指定月份属于一年中的哪个季度。如果是 12 月、1 月、2 月,就属于冬季。

    从键盘输入一位整数,代表月份,编程判断指定月份属于一年中的哪个季度.如果是 12月.1 月.2 月,就属于冬季:如果是 3 月.4 月.5 月,就属于春季:如果是 6 月.7 月.8 月,就属于夏季: ...

  7. Unity引擎GUI之Image

    UGUI的Image等价于NGUI的Sprite组件,用于显示图片. 一.Image组件: Source Image(图像源):纹理格式为Sprite(2D and UI)的图片资源(导入图片后选择T ...

  8. WEB笔记-3、盒子模型+定位+显示

      3.1 盒子模型 边距控制 margin/padding:上 右 下 左:   padding:内容和边距之间的空间 margin:”盒子“外撑开的空间,两个相邻标签外边距会出现重叠和累加的现象, ...

  9. 部署国密SSL证书,如何兼容国际主流浏览器?

    国密算法在主流操作系统.浏览器等客户端中,还没有实现广泛兼容.因此,在面向开放互联网的产品应用中,国密算法无法得到广泛应用.比如,在SSL证书应用领域,由于国际主流浏览器不信任国密算法,如果服务器部署 ...

  10. Lua操作系统库、流、文件库

    Lua操作系统库.流.文件库 1.Lua中所有的操作系统库函数 (1)os.clock() --功能:返回执行该程序cpu花费的时钟秒数 (2)os.time(...) --按参数的内容返回一个时间值 ...