首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >CodeForces 666B World Tour(spfa+枚举)

CodeForces 666B World Tour(spfa+枚举)

作者头像
ShenduCC
发布于 2018-04-26 09:14:43
发布于 2018-04-26 09:14:43
58802
代码可运行
举报
文章被收录于专栏:算法修养算法修养
运行总次数:2
代码可运行

B. World Tour

time limit per test

5 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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

Floyed求最短路回超时,用spfa然后再枚举4个点中的中间两个点,头尾两个点

只需要取最大的就好了,当然不能有重复

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>


using namespace std;
const int INF=1000000;
#define MAX 3000
vector < pair<int,int> > a[MAX+5];
vector < pair<int,int> > b[MAX+5];
vector <int> c[MAX+5];
int d[MAX+5][MAX+5];
bool vis[MAX+5];
int res[5];
int n,m;
void spfa(int i)
{
   memset(vis,false,sizeof(vis));
   queue<int> q;
   q.push(i);vis[i]=1;
   while(!q.empty())
   {
       int x=q.front();q.pop();
       vis[x]=0;
       for(int j=0;j<c[x].size();j++)
       {
           int next=c[x][j];
           if(d[i][next]>d[i][x]+1)
           {
               d[i][next]=d[i][x]+1;
               if(!vis[next])
               {
                   vis[next]=1;
                   q.push(next);
               }
           }
       }
   }
}
int main()
{
    scanf("%d%d",&n,&m);
    int x,y;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
              if(i==j) d[i][j]=0;
              else d[i][j]=INF;
        }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        c[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]!=INF) a[i].push_back(make_pair(d[i][j],j));
            if(d[j][i]!=INF) b[i].push_back(make_pair(d[j][i],j));
        }
        sort(a[i].begin(),a[i].end());
        sort(b[i].begin(),b[i].end());
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        int nn=b[i].size();
        for(int j=1;j<=n;j++)
        {
            if(i==j) continue;
			if(d[i][j]==INF) continue;
            int mm=a[j].size();
            for(int k=nn-1;k>=0&&k>=nn-3;k--)
            {
                int kk=b[i][k].second;
                if(kk==j||kk==i) continue;
                for(int p=mm-1;p>=0&&p>=mm-3;p--)
                {
                    int pp=a[j][p].second;
                    if(pp==i||pp==j||pp==kk) continue;
                    if(ans<d[kk][i]+d[i][j]+d[j][pp])
                    {
                        ans=d[kk][i]+d[i][j]+d[j][pp];
                        res[1]=kk;res[2]=i;res[3]=j;res[4]=pp;
                    }
                }
            }
        }
    }
	//printf("%d\n",ans);
    for(int i=1;i<=4;i++)
    {
        if(i==4)
            printf("%d\n",res[i]);
        else
            printf("%d ",res[i]);
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-05-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
原来学习Python面相对象编程这么简单
在编程中,有两个特别重要的编程思想,大家应该都听说过,一个种事面向过程变成,另外一种就是今天这篇文章的重点“面向对象编程”。
小白的大数据之旅
2025/07/03
1070
原来学习Python面相对象编程这么简单
python ——面向对象进阶
1.staticmethod和classmethod staticmethod  静态方法: 让类里的方法,直接被类调用,就像正常的函数一样 宝宝,男 博博,女 海娇,男 海燕,女 海东,男 海峰,男 class Student: # f = open('student', encoding='utf-8') def __init__(self): pass @staticmethod def show_student_info(): f
用户1214487
2018/01/23
6410
Python 面向对象学习整理 (看这一篇就足够了)
学完了 Python 基础之后,当我想要把自己的一些小项目通过 Python OOP 的方式来编写的时候,却发现很难很难,于是这次重新回过头来重新学习 Python 中面向对象的思想
Gorit
2021/12/08
9.5K2
Python 面向对象学习整理 (看这一篇就足够了)
Python基础(7)——类
定义类使用class关键字,class 后面紧跟着类名称,类名称通常首字母大写,类名称后面(object)代表当前的类的继承自object类。类主要包含属性和方法
羊羽shine
2019/05/28
3940
Python面向对象之封装(04)
封装是面向对象编程的一大特点,面向对象编程的第一步将属性和方法封装到一个抽象类中,外界使用类创建对象然后让对象调用方法,对象方法的的细节都被封装在类的内部。
PM小王
2019/07/02
5650
24. 企业级开发基础5:面向对象特征(封装)
在我们程序开发过程中,定义好类型之后就可以通过类型来创建对象 如:我们定义一个中华人民共和国公民的类型
大牧莫邪
2018/08/27
2770
Python 面向对象编程
二类:由抽象信息或者动作组成的集合,代表一类事物,抽象名词实例(对象):具象的,是一类事物中某一个具体的事物
用户7886150
2020/12/19
4720
Python每天五分钟-面向对象编程之类
面向对象编程简称oop(Object Oriented Programming),是一种程序的设计架构。共有三大特性:封装、继承、多态。五大原则:单一职责原则、开放封闭原则、替换原则、依赖原则、接口分离原则。
用户2475223
2019/12/17
4220
Python3 与 C# 扩展之~基础拓展
看着小张准备回家换衣服了,小明有点失落,又有点孤单,于是说道:“逗逼张,你还要听吗?我准备讲类相关的知识了,这些可是我课后自学的哦~”
逸鹏
2018/07/23
1.5K0
Python3 与 C# 扩展之~基础拓展
Python升级之路( Lv7 ) 面向对象深入
第一章 Python 入门 第二章 Python基本概念 第三章 序列 第四章 控制语句 第五章 函数 第六章 面向对象基础 第七章 面向对象深入
时间静止不是简史
2022/06/15
5300
Python升级之路( Lv7 ) 面向对象深入
Python进阶教程笔记(一)面向对象编程
在Python中,通过class关键字定义一个类,比如我们需要定义一个人的类。按照 Python 的编程习惯,类名以大写字母开头。因此可以这样定义:
Lemon黄
2020/10/30
4220
Python: 面向对象编程(类和对象)
文章背景: 最近在学习课程Python-Core-50-Courses,其中有个章节是面向对象编程,涉及的内容是类(class)和对象。下面对所学的内容进行相应的整理。
Exploring
2022/09/20
5970
Python 面向对象
# Python 面向对象 # 编程思想 编程届的两大阵营 面向过程 面向对象 区别 实物比较简单,可以用线性的思想去解决 事物比较复杂,使用简单的线性思维无法解决 共同点 面向过程和面向对象都是解决实际问题的一种思维方式 二者相辅相成,并不是对立的,解决复杂问题,通过面向对象方式便于我们从宏观上把握事物之间的复杂的关系。方便我们分析整个系统,具体到微观操作,任然使用面向过程方式来处理 # 类与对象 类 类别,分门别类,物以类聚,人类,鸟类,动物类,植物类... 类是多个类似事物组成的群体的
用户9615083
2022/12/25
3790
Python 面向对象
python基础复习:面向对象之继承
__name 表示私有 : 访问对象的私有属性 stu_Student__name ; 不能使用stu.__name访问私有属性
李福春
2025/07/30
810
python基础复习:面向对象之继承
Python面向对象编程:深入理解类、对象、继承和多态
面向对象编程(Object-Oriented Programming,OOP)是一种强大的编程范式,它将数据和操作数据的方法组织成对象。Python是一门多范式的编程语言,支持面向对象编程,本文将深入探讨Python中的OOP概念,包括类、对象、继承、多态等,以帮助你更好地理解和应用面向对象编程。
海拥
2023/09/19
7210
Python面向对象编程:深入理解类、对象、继承和多态
python高级-面向对象特性(12)
在现实生活中,继承一般指的是子女继承父辈的财产,在程序中,继承描述的是事物之间的所属关系,例如猫和狗都属于动物,程序中便可以描述为猫和狗继承自动物;同理,波斯猫和巴厘猫都继承自猫,而沙皮狗和斑点狗都继承足够,如下如所示:
Se7eN_HOU
2019/09/11
5290
python高级-面向对象特性(12)
面向对象
面向对象编程(Object Oriented Programming,OOP,面向对象程序设计)
周小董
2019/03/25
6720
面向对象
[Python基础13]面向对象特征封装|继承|多态
在我们程序开发过程中,定义好类型之后就可以通过类型来创建对象 如:我们定义一个中华人民共和国公民的类型
周小董
2022/04/12
6610
【愚公系列】2021年12月 Python教学课程 20-面向对象编程-类和对象
类,英文名字 Class,有“类别”,“分类”,“聚类”的意思。必须牢记类是抽象 的模板,用来描述具有相同属性和方法的对象的集合,比如 Animal 类。而实例是根据 类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可 能不同。
愚公搬代码
2021/12/15
2810
【愚公系列】2021年12月 Python教学课程 20-面向对象编程-类和对象
Python|面向对象
#一、类、对象定义及使用 #定义类语法:class 类名(父类):代码块注意:()可有可无 #class Student: #class Student(): #创建对象(实例)语法:对象名=类名() 注意:Java语言在实例化对象是,采用new关键字,而Python语言不需要。 #student=Student() #isinstance()语法:isinstance()是检测一个对象是否是一个类的实例,语法格式:isinstance(对象,类),结果返回True和False # class Stu
py3study
2020/01/19
5010
相关推荐
原来学习Python面相对象编程这么简单
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档