【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)之城, 相当好看... 广二好大! 哈三 ...
随机推荐
- 53. Maximum Subarray@python
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- 【思维题 kmp 构造】bzoj4974: [Lydsy1708月赛]字符串大师
字符串思博题这一块还是有点薄弱啊. Description 一个串T是S的循环节,当且仅当存在正整数k,使得S是T^k(即T重复k次)的前缀,比如abcd是abcdabcdab的循环节 .给定一个长度 ...
- Spring源码剖析依赖注入实现
Spring源码剖析——依赖注入实现原理 2016年08月06日 09:35:00 阅读数:31760 标签: spring源码bean依赖注入 更多 个人分类: Java 版权声明:本文为博主原 ...
- 无需上传附件到服务器,Servlet读取Excel(二)
package com.str; import java.io.File;import java.io.FileInputStream;import java.io.IOException; impo ...
- selenium2等待元素加载
1.硬性等待 Thread.sleep(8000); 所谓的硬性等待就是,执行完相应操作就等待我设置的8s.无论网速快与慢,网速快的话,也许5s就打开网页了,可是程序必须接着等待剩下的3秒. 网速慢的 ...
- UVa 579 Clock Hands
水题.. 求任意时刻时针和分针的夹角,其结果在0°到180°之间. 这里又一次用到了sscanf()函数,确实很方便. 思路:我们分别求出时针和分针转过的角度,然后大的减小的,如果结果ans大于180 ...
- Ubuntu 档案权限
Linux文件属性:查看指令是:ls -al ls是list的意思,重点在显示档案的文件名与相关属性.而选项-al则表示列出所有的档案详细的权限与属性.
- eureka显示ip地址的参数
eureka.instance.prefer-ip-address=trueeureka.instance.instance-id=${#spring.cloud.client.ipAddress}: ...
- webdriver高级应用- 操作日期控件
1. 通过点击的方式操作日期控件 #encoding=utf-8 from selenium import webdriver import unittest, time, traceback fro ...
- python - 读取配置文件
# -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: read_config.py @ide: PyCharm Commun ...