题目链接:

D. 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
  1. 8 9
    1 2
    2 3
    3 4
    4 1
    4 5
    5 6
    6 7
    7 8
    8 5
output
  1. 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.

题意

给一个有向图,选取4个点,使dis[s1][s2]+dis[s2][s3]+dis[s3][s4]最大;

思路

先找出所有点对之间的最短距离,再暴力枚举s2,s3,对于s1和s4一定是选到s2最远的点和s3能最远到的点

AC代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define Riep(n) for(int i=1;i<=n;i++)
  4. #define Riop(n) for(int i=0;i<n;i++)
  5. #define Rjep(n) for(int j=1;j<=n;j++)
  6. #define Rjop(n) for(int j=0;j<n;j++)
  7. #define mst(ss,b) memset(ss,b,sizeof(b));
  8. typedef long long LL;
  9. const LL mod=1e9+;
  10. const double PI=acos(-1.0);
  11. const int inf=0x3f3f3f3f;
  12. const int N=1e5+;
  13. int n,m;
  14. int p[][],vis[];
  15. vector<int>ve[];
  16. struct node
  17. {
  18. int num,dis;
  19. };
  20. vector<node>to[],from[];
  21. queue<int>qu;
  22. int bfs(int x)
  23. {
  24. while(!qu.empty())qu.pop();
  25. memset(vis,,sizeof(vis));
  26. qu.push(x);
  27. vis[x]=;
  28. while(!qu.empty())
  29. {
  30. int fr=qu.front();
  31. qu.pop();
  32. vis[fr]=;
  33. int len=ve[fr].size();
  34. Riop(len)
  35. {
  36. if(p[x][ve[fr][i]]>p[x][fr]+)
  37. {
  38. p[x][ve[fr][i]]=p[x][fr]+;
  39. if(!vis[ve[fr][i]])
  40. {
  41. vis[ve[fr][i]]=;
  42. qu.push(ve[fr][i]);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. int cmp(node x,node y)
  49. {
  50. return x.dis>y.dis;
  51. }
  52. int main()
  53. {
  54. scanf("%d%d",&n,&m);
  55. Riep(n)
  56. Rjep(n)
  57. if(i!=j)p[i][j]=inf;
  58. else p[i][j]=;
  59. int u,v;
  60. Riep(m)
  61. {
  62. scanf("%d%d",&u,&v);
  63. ve[u].push_back(v);
  64. }
  65. Riep(n)bfs(i);
  66. node x;
  67. Riep(n)
  68. {
  69. Rjep(n)
  70. {
  71. if(p[i][j]!=inf&&i!=j)
  72. {
  73. x.num=i,x.dis=p[i][j];
  74. to[j].push_back(x);
  75. x.num=j;
  76. from[i].push_back(x);
  77. }
  78. }
  79. }
  80.  
  81. Riep(n)sort(to[i].begin(),to[i].end(),cmp),sort(from[i].begin(),from[i].end(),cmp);
  82. int dis=,ans1,ans2,ans3,ans4,s1,s2,s3,s4;
  83. Riep(n)
  84. {
  85. s2=i;
  86. Rjep(n)
  87. {
  88. s3=j;
  89. if(i==j||p[i][j]==inf)continue;
  90. for(int k=;k<&&k<to[i].size();k++)
  91. {
  92. if(i==to[i][k].num||j==to[i][k].num)continue;
  93. s1=to[i][k].num;
  94. for(int d=;d<&&d<from[j].size();d++)
  95. {
  96. if(i==from[j][d].num||j==from[j][d].num||s1==from[j][d].num)continue;
  97. s4=from[j][d].num;
  98. if(p[s1][s2]+p[s2][s3]+p[s3][s4]>dis)
  99. {
  100. dis=p[s1][s2]+p[s2][s3]+p[s3][s4];
  101. ans1=s1,ans2=s2,ans3=s3,ans4=s4;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. printf("%d %d %d %d\n",ans1,ans2,ans3,ans4);
  108.  
  109. return ;
  110. }

codeforces 667D D. World Tour(最短路)的更多相关文章

  1. Codeforces 667D World Tour 最短路

    链接 Codeforces 667D World Tour 题意 给你一个有向稀疏图,3000个点,5000条边. 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大. 思 ...

  2. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

  3. Codeforces 667D World Tour【最短路+枚举】

    垃圾csdn,累感不爱! 题目链接: http://codeforces.com/contest/667/problem/D 题意: 在有向图中找到四个点,使得这些点之间的最短距离之和最大. 分析: ...

  4. World Tour CodeForces - 667D (bfs最短路)

    大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大

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

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

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

  7. Codeforces 545E. Paths and Trees 最短路

    E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...

  8. Codeforces 543 B. World Tour

    http://codeforces.com/problemset/problem/543/B 题意: 给定一张边权均为1的无向图. 问至多可以删除多少边,使得s1到t1的最短路不超过l1,s2到t2的 ...

  9. Codeforces 666 B. World Tour

    http://codeforces.com/problemset/problem/666/B 题意: 给定一张边权均为1的有向图,求四个不同的点A,B,C,D,使得dis[A][B]+dis[B][C ...

随机推荐

  1. postman 快捷方式--启动图标

    下载,解压,安装,(此安装位置在/opt) 1.创建全局变量,也就是在任何地方都可以执行postman,不用去到安装目录,执行 : sudo ln -s /opt/postman/Postman /u ...

  2. P1576 最小花费 洛谷

    https://www.luogu.org/problem/show?pid=1576 题目背景 题目描述 在n个人中,某些人的银行账号之间可以互相转账.这些人之间转账的手续费各不相同.给定这些人之间 ...

  3. Jetson TK1 三:项目相关安装

    ROS.QT.pyserial2.7.罗技手柄驱动.navigation.slam和rviz等 激光雷达IP设置,tk1对应的IP设置,tk1串口设置 一.安装ros参见官网 二.安装QT 百度QT官 ...

  4. linux 系统命令----修改主机名

    1. hostname    ***** 2.修改/etc/sysconfig/network 3./etc/hosts 最后第三步没有必要!

  5. influxdb的python操作

    1.先安装依赖:pip install influxdb 2.

  6. 深入理解javascript之设计模式

    设计模式 设计模式是命名.抽象和识别对可重用的面向对象设计实用的的通用设计结构. 设计模式确定类和他们的实体.他们的角色和协作.还有他们的责任分配. 每个设计模式都聚焦于一个面向对象的设计难题或问题. ...

  7. BUPT复试专题—Special 数(2017)

    题目描述 设一个正整数既是平方数乂是立方数时,称为Special数. 输入 输入包含多组测试数据,笫1行输入测试数据的组数,接下来在后续每行输入n(n<=1000000000) 输出 输出1到n ...

  8. python异常捕获异常堆栈输出

    python异常捕获异常堆栈输出 学习了:https://blog.csdn.net/chris_grass/article/details/77927902 import traceback def ...

  9. 【数据库摘要】6_Sql_Inner_Join

    INNER JOIN 操作符 INNER JOIN keyword在表中存在至少一个匹配时返回行. SQL INNER JOIN 语法 SELECT column_name(s) FROM table ...

  10. 使用网络监视器(IRSI)捕捉和分析协议数据包

    转载请注明原地址. 实验名称:  理解子网掩码.网关和ARP协议的作用             一.实验目的和要求 (1) 熟悉IRIS的使用 (2) 验证各种协议数据包格式 (3) 学会捕捉并分析各 ...