【CF666B】World Tour(贪心,最短路)
题意:给你一张有向图,叫你给出四个点的序列a,b,c,d,使得这四个点依次间的最短路之和最大。(4 ≤ n ≤ 3000, 3 ≤ m ≤ 5000)
思路:O(n4)可用来对拍
我们需要O(n2)级别的算法
若枚举c,d,预处理出x到b比较远的3个x,d到y比较远的3个y,时间复杂度O(9n2)
为什么是3个而不是2个?
abc已枚举,若d的备选是ab就要GG,所以应该多一个备用,也就是三个点
var c,d:array[..,..,..]of longint;
head,vet,next,b,flag,q,x,y:array[..]of longint;
dis:array[..]of longint;
save:array[..,..]of longint;
n,m,tot,i,s1,s2,s3,s4,a1,b1,c1,d1,ans,j,k,s,p1,q1:longint; procedure add(a,b:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
head[a]:=tot;
end; procedure bfs(x:longint);
var t,w,e,u,v:longint;
begin
fillchar(b,sizeof(b),);
fillchar(dis,sizeof(dis),);
t:=; w:=; q[]:=x; b[x]:=; dis[x]:=;
while t<=w do
begin
u:=q[t]; inc(t);
e:=head[u];
while e<> do
begin
v:=vet[e];
if b[v]= then
begin
dis[v]:=dis[u]+;
b[v]:=;
inc(w); q[w]:=v;
end;
e:=next[e];
end;
end;
end; begin
//assign(input,'1.in'); reset(input);
//assign(output,'1.out'); rewrite(output);
readln(n,m);
for i:= to m do
begin
readln(x[i],y[i]);
add(x[i],y[i]);
end; for i:= to n do
begin
bfs(i);
k:=; s:=;
fillchar(flag,sizeof(flag),);
for j:= to n do
if (flag[j]=)and(dis[j]>s) then
begin
k:=j; s:=dis[j];
end;
flag[k]:=; c[i,,]:=k; c[i,,]:=s;
k:=; s:=;
for j:= to n do
if (flag[j]=)and(dis[j]>s) then
begin
k:=j; s:=dis[j];
end;
flag[k]:=; c[i,,]:=k; c[i,,]:=s;
k:=; s:=;
for j:= to n do
if (flag[j]=)and(dis[j]>s) then
begin
k:=j; s:=dis[j];
end;
flag[k]:=; c[i,,]:=k; c[i,,]:=s; // x---->c[i] ,,
for j:= to n do save[i,j]:=dis[j];
end; fillchar(head,sizeof(head),);
tot:=;
for i:= to m do add(y[i],x[i]); for i:= to n do
begin
bfs(i);
k:=; s:=;
fillchar(flag,sizeof(flag),);
for j:= to n do
if (flag[j]=)and(dis[j]>s) then
begin
k:=j; s:=dis[j];
end;
flag[k]:=; d[i,,]:=k; d[i,,]:=s;
k:=; s:=;
for j:= to n do
if (flag[j]=)and(dis[j]>s) then
begin
k:=j; s:=dis[j];
end;
flag[k]:=; d[i,,]:=k; d[i,,]:=s;
k:=; s:=;
for j:= to n do
if (flag[j]=)and(dis[j]>s) then
begin
k:=j; s:=dis[j];
end;
flag[k]:=; d[i,,]:=k; d[i,,]:=s; // d[i] ---->i
end; s1:=; s2:=; s3:=; s4:=; ans:=-maxlongint;
for b1:= to n do
for c1:= to n do
if save[b1,c1]> then
for p1:= to do
for q1:= to do
begin
d1:=c[c1,p1,];
a1:=d[b1,q1,]; //a1-->b1-->c1-->d1
if (a1<>b1)and(a1<>c1)and(a1<>d1)and(b1<>c1)and(b1<>d1)and(c1<>d1) then
if d[b1,q1,]+c[c1,p1,]+save[b1,c1]>ans then
begin
s1:=a1; s2:=b1; s3:=c1; s4:=d1;
ans:=d[b1,q1,]+c[c1,p1,]+save[b1,c1];
end;
end;
writeln(s1,' ',s2,' ',s3,' ',s4); //close(input);
//close(output);
end.
【CF666B】World Tour(贪心,最短路)的更多相关文章
- CF666B. World Tour
CF666B. World Tour 题意: 给定一张边权为 1 的有向图,求四个不同点 A, B, C, D 使得 dis(A, B) + dis(B, C) + dis(C, D) 取最大值,di ...
- Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路
B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...
- [CSP-S模拟测试]:午餐(贪心+最短路)
题目传送门(内部题115) 输入格式 第一行两个正整数$n,m$. 接下来$m$行,每行$4$个正整数$u_j,v_j,L_j,R_j$. 接下来一行$n$个数,若第$i$个数为$1$,则$i$号同学 ...
- ZOJ 3946 Highway Project 贪心+最短路
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...
- Codeforces 667D World Tour【最短路+枚举】
垃圾csdn,累感不爱! 题目链接: http://codeforces.com/contest/667/problem/D 题意: 在有向图中找到四个点,使得这些点之间的最短距离之和最大. 分析: ...
- Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路
D. World Tour A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wid ...
- cf1076d 贪心最短路
#include<bits/stdc++.h> #include<queue> using namespace std; #define maxn 300005 #define ...
- CF 360 E Levko and Game —— 贪心+最短路
题目:http://codeforces.com/contest/360/problem/E 首先,每条边不是选 \( l[i] \) 就是选 \( r[i] \): 做法就是先把边权都设成 \( r ...
- Codeforces 360E 贪心 最短路
题意及思路:https://blog.csdn.net/huanghongxun/article/details/49846927 在假设所有边都是最大值的情况下,如果第一个人能比第二个人先到,那就缩 ...
- WC2019游记 && 课件
WC2019 游记 课件 wc2019.zip_免费高速下载|百度网盘-分享无限制 提取码: un6z day 0 打飞机去广州... 在飞机上刷了爱乐(le)之城, 相当好看... 广二好大! 哈三 ...
随机推荐
- C Library - <limits.h>
Introduction The limits.h header determines various properties of the various variable types. The ma ...
- Java(面试题):字符串截取
在Java中,字符串“abcd”与字符串“ab你好”的长度是一样,都是四个字符. 但对应的字节数不同,一个汉字占两个字节. 定义一个方法,按照指定的字节数来取子串. 如:对于“ab你好”,如果取三个字 ...
- Dede常用标记
http://fontawesome.dashgame.com/ 字体图标使用方法 http://www.iconfont.cn/ 阿里的图标库 https://icomoon.io/ 字体制作 时间 ...
- c++ 定义一个结构体student,输入多个student的信息并以三种方式显示
#include <iostream> #include <string> using namespace std; const int slen = 30; struct s ...
- 【主席树 启发式合并】bzoj3123: [Sdoi2013]森林
小细节磕磕碰碰浪费了半个多小时的时间 Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M ...
- (72)zabbix监控日志文件 MySQL日志为例
一般情况下,日志最先反映出应用当前的问题,在海量日志里面找到我们异常记录,然后记录下来,并且根据情况报警,大家可以监控系统日志.nginx.Apache.业务日志. 这边我拿常见的MySQL日志做监控 ...
- linux :没有找到 ifconfig netstat
linux :没有找到 ifconfig netstat ubuntu sudo apt install net-tools -y centos yum install net-tools
- MYSQL不能显示中文字,显示错误“ERROR 1366 (HY000): Incorrect string value: '\xE5\xBC\xA0\xE4\xB8\x89'”
或者建表时带上编码utf8 CREATE TABLE `students`( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR( ...
- python入门:输出1-10以内除去7的所有数(简)
#!/usr/bin/env python # -*- coding:utf-8 -*- #输出1-10以内除去7的所有数(简) """ 给变量kaishi赋值1,whi ...
- MySQL查询时,查询结果如何按照where in数组排序
MySQL查询时,查询结果如何按照where in数组排序 在查询中,MySQL默认是order by id asc排序的,但有时候需要按照where in 的数组顺序排序,比如where in的id ...