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

CodeForces 666B World Tour(spfa+枚举)

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

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 删除。

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
CodeForces 24A Ring road(dfs)
A. Ring road time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams
ShenduCC
2018/04/27
6780
【CodeForces 567E】President and Roads(最短路)
Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s ≠ t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer.
饶文津
2020/06/02
7290
最短路专题2 | CodeForces 449B - SPFA算法
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city to (and vise versa) using the -th road, the length of this road is . Finally, there are k train routes in the country. One can use the -th train route to go from capital of the country to city (and vise versa), the length of this route is . J是A国的总统,这个国家有n个城市。1是首都,有m条公路连接这些城市。然后,有k个火车线。城市到首都1的距离是。
ACM算法日常
2019/10/21
7230
CodeForces Roads not only in Berland(并查集)
H - Roads not only in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 25D Description Berland Government decided to improve relations with neighboring countries. First of all, i
ShenduCC
2018/04/26
9040
ZOJ 3332 Strange Country II
Strange Country II ---- Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge ---- You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 to n. The unique way to travel in the country is taking plan
ShenduCC
2018/04/26
6220
Codeforces Round #547 (Div. 3)G. Privatization of Roads in Treeland
Treeland consists of n cities and n−1 roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right — the country's topology is an undirected tree.
glm233
2020/09/28
3790
CodeForces 25C(Floyed 最短路)
F - Roads in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 25C Description There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Eac
ShenduCC
2018/04/26
5710
K - Highway Project  ZOJ - 3946 【 SPFA 求最小时间下最小距离】
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.
Lokinli
2023/03/09
2710
hdu 4081 Qin Shi Huang's National Road System (次小生成树)
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3843    Accepted Submission(s): 1336 Problem Description During the Warring States Period of ancient China(476
Gxjun
2018/03/26
1K0
hdu  4081   Qin Shi Huang's National Road System   (次小生成树)
The 2019 Asia Nanchang First Round Online Programming Contest B Fire-Fighting Hero(阅读理解)
This is an era of team success, but also an era of heroes. Throughout the ages, there have been numerous examples of using the few to defeat the many. There are VV (Numbers 11 to VV) fire-fighting points in ACM city. These fire-fighting points have EE roads to communicate with each other. Among them, there is a fire-fighting hero in the SS fire-fighting point, and the fire-fighting team is distributed in K fire-fighting points. If a fire-fighting point needs to be put out, the fire-fighting hero or the fire-fighting team must arrive as soon as possible, that is, to choose the shortest route to arrive.
风骨散人Chiam
2020/10/28
2810
hdu2377Bus Pass(构建更复杂的图+spfa)
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 667 Accepted Submission(s): 271
全栈程序员站长
2022/07/05
2590
hdu2377Bus Pass(构建更复杂的图+spfa)
ZOJ 3946 Highway Project(Dijkstra)
Highway Project ---- Time Limit: 2 Seconds      Memory Limit: 65536 KB ---- Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the h
ShenduCC
2018/04/26
5370
图论-多源最短路径(Floyd算法)
分析: 给定若干双向正值边和单向负值边,问是否存在负圈(使其时光倒流回到原点)。 所以在第二重循环,求完第i个结点后判断。
唔仄lo咚锵
2020/10/16
6660
图论-多源最短路径(Floyd算法)
逮捕罪犯(树)- HDU 3069
Country ALPC has n cities, and the cities are connected by undirected roads. Furthermore, there is exactly one route between each pair of cities. Now some criminals break the prison and the police do not know where they are. And the criminals can stay in a city or move on the roads. The police office has made a decision to send policemen to arrest the breakers. If a police and a criminal at a same point at the same time the criminal is under arrest, no matter in a city or on a road. The police office wants to minimize the number of policemen to arrest all the criminals. Now the map of Country ALPC is given, please tell the police office the least number of policemen required.
ACM算法日常
2018/08/07
2710
逮捕罪犯(树)- HDU 3069
PAT 甲级 1003Emergency(Dijkstra最短路)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some
ShenduCC
2018/04/26
6560
航班Flight(SPFA+dp)- HDU 3499
Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him reduce the total cos
ACM算法日常
2018/08/07
4070
ACMSGURU 114 - Telecasting station
Every city in Berland is situated on Ox axis. The government of the country decided to build new telecasting station. After many experiments Berland scientists came to a conclusion that in any city citizens displeasure is equal to product of citizens amount in it by distance between city and TV-station. Find such point on Ox axis for station so that sum of displeasures of all cities is minimal.
Reck Zhang
2021/09/01
5980
Codeforces Round #277.5 (Div. 2)-D「建议收藏」
题意:求该死的菱形数目。直接枚举两端的点。平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2.
全栈程序员站长
2022/07/08
3110
Codeforces Round #277.5 (Div. 2)-D「建议收藏」
PAT (Advanced Level) Practice 1003 Emergency (25 分)
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
glm233
2020/09/28
3210
hdu 3001 Travelling (TSP问题 )
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3905 Accepted Submission(s): 1234
全栈程序员站长
2022/07/06
3650
相关推荐
CodeForces 24A Ring road(dfs)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档