【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,所以应该多一个备用,也就是三个点

  1 var c,d:array[1..3000,1..3,1..2]of longint;
  2     head,vet,next,b,flag,q,x,y:array[1..10000]of longint;
  3     dis:array[1..3000]of longint;
  4     save:array[1..3000,1..3000]of longint;
  5     n,m,tot,i,s1,s2,s3,s4,a1,b1,c1,d1,ans,j,k,s,p1,q1:longint;
  6
  7 procedure add(a,b:longint);
  8 begin
  9  inc(tot);
 10  next[tot]:=head[a];
 11  vet[tot]:=b;
 12  head[a]:=tot;
 13 end;
 14
 15 procedure bfs(x:longint);
 16 var t,w,e,u,v:longint;
 17 begin
 18  fillchar(b,sizeof(b),0);
 19  fillchar(dis,sizeof(dis),0);
 20  t:=1; w:=1; q[1]:=x; b[x]:=1; dis[x]:=0;
 21  while t<=w do
 22  begin
 23   u:=q[t]; inc(t);
 24   e:=head[u];
 25   while e<>0 do
 26   begin
 27    v:=vet[e];
 28    if b[v]=0 then
 29    begin
 30     dis[v]:=dis[u]+1;
 31     b[v]:=1;
 32     inc(w); q[w]:=v;
 33    end;
 34    e:=next[e];
 35   end;
 36  end;
 37 end;
 38
 39 begin
 40  //assign(input,‘1.in‘); reset(input);
 41  //assign(output,‘1.out‘); rewrite(output);
 42  readln(n,m);
 43  for i:=1 to m do
 44  begin
 45   readln(x[i],y[i]);
 46   add(x[i],y[i]);
 47  end;
 48
 49  for i:=1 to n do
 50  begin
 51   bfs(i);
 52   k:=1; s:=0;
 53   fillchar(flag,sizeof(flag),0);
 54   for j:=1 to n do
 55    if (flag[j]=0)and(dis[j]>s) then
 56    begin
 57     k:=j; s:=dis[j];
 58    end;
 59   flag[k]:=1; c[i,1,1]:=k; c[i,1,2]:=s;
 60   k:=1; s:=0;
 61   for j:=1 to n do
 62    if (flag[j]=0)and(dis[j]>s) then
 63    begin
 64     k:=j; s:=dis[j];
 65    end;
 66   flag[k]:=1; c[i,2,1]:=k; c[i,2,2]:=s;
 67   k:=1; s:=0;
 68   for j:=1 to n do
 69    if (flag[j]=0)and(dis[j]>s) then
 70    begin
 71     k:=j; s:=dis[j];
 72    end;
 73   flag[k]:=1; c[i,3,1]:=k; c[i,3,2]:=s; // x---->c[i] 1,2,3
 74   for j:=1 to n do save[i,j]:=dis[j];
 75  end;
 76
 77  fillchar(head,sizeof(head),0);
 78  tot:=0;
 79  for i:=1 to m do add(y[i],x[i]);
 80
 81  for i:=1 to n do
 82  begin
 83   bfs(i);
 84   k:=1; s:=0;
 85   fillchar(flag,sizeof(flag),0);
 86   for j:=1 to n do
 87    if (flag[j]=0)and(dis[j]>s) then
 88    begin
 89     k:=j; s:=dis[j];
 90    end;
 91   flag[k]:=1; d[i,1,1]:=k; d[i,1,2]:=s;
 92   k:=1; s:=0;
 93   for j:=1 to n do
 94    if (flag[j]=0)and(dis[j]>s) then
 95    begin
 96     k:=j; s:=dis[j];
 97    end;
 98   flag[k]:=1; d[i,2,1]:=k; d[i,2,2]:=s;
 99   k:=1; s:=0;
100   for j:=1 to n do
101    if (flag[j]=0)and(dis[j]>s) then
102    begin
103     k:=j; s:=dis[j];
104    end;
105   flag[k]:=1; d[i,3,1]:=k; d[i,3,2]:=s; // d[i] 1 2 3 ---->i
106  end;
107
108  s1:=1; s2:=2; s3:=3; s4:=4; ans:=-maxlongint;
109  for b1:=1 to n do
110   for c1:=1 to n do
111    if save[b1,c1]>0 then
112    for p1:=1 to 3 do
113     for q1:=1 to 3 do
114     begin
115      d1:=c[c1,p1,1];
116      a1:=d[b1,q1,1];     //a1-->b1-->c1-->d1
117      if (a1<>b1)and(a1<>c1)and(a1<>d1)and(b1<>c1)and(b1<>d1)and(c1<>d1) then
118       if d[b1,q1,2]+c[c1,p1,2]+save[b1,c1]>ans then
119       begin
120        s1:=a1; s2:=b1; s3:=c1; s4:=d1;
121        ans:=d[b1,q1,2]+c[c1,p1,2]+save[b1,c1];
122       end;
123     end;
124  writeln(s1,‘ ‘,s2,‘ ‘,s3,‘ ‘,s4);
125
126  //close(input);
127  //close(output);
128 end.

时间: 2024-08-02 08:37:11

【CF666B】World Tour的相关文章

【POJ】【1637】Sightseeing tour

网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可以贡献一个出度出来,对于一条边u->v: 连S->edge cap=1; 如果是有向边,就连 edge->v cap=1; 否则(无向边)连edge->u cap=1, edge->v cap=1; 然后每个点的总度数我们是知道的……那么它最后的[出度]就等于 总度数/2.(这个

【POJ1637】Sightseeing tour

Description The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visited exactly once.

【SPOJ】【1825】Free Tour 2

点分治 点分治的例题2(本题代码结果为TLE……) 强烈谴责卡时限QAQ,T了无数次啊无数次…… 不过在N次的静态查错中倒是加深了对点分治的理解……也算因祸得福吧(自我安慰一下) 1 //SPOJ 1825 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<iostream> 6 #include<algorithm> 7 #define rep(i,n)

【bzoj3060】[Poi2012]Tour de Byteotia 并查集

题目描述 给定一个n个点m条边的无向图,问最少删掉多少条边能使得编号小于等于k的点都不在环上. 输入 第一行三个整数n,m,k: 接下来m行每行两个整数ai,bi,表示ai和bi之间有一条无向边. 输出 一个整数,表示最少的删边数量. 样例输入 11 13 5 1 2 1 3 1 5 3 5 2 8 4 11 7 11 6 10 6 9 2 3 8 9 5 9 9 10 样例输出 3 题解 并查集 先把不包含编号小于等于k的点的边连上,用并查集维护图的连通性.易知如果删除这些边则一定不是最优解,

【POJ1637】Sightseeing tour 混合图求欧拉回路存在性 网络流、

题意:多组数据,最后的0/1表示0无向1有向. 问是否存在欧拉回路. 题解:无向边给它任意定个向. 首先欧拉回路中点入度=出度. 然后发现每个无向边如果修改个方向,原来的入点的入度+1,出度-1,出点反之. 然后我们不妨对入度和出度不同的点跟源汇中之一连边,容量为入出度差一半(每改一条边差-2) 然后原来的无向边联系图中各点,容量1,最后check if(maxflow==sum差/4). 这都没看懂的弱菜们不妨出度多的连源,入度多的连汇,容量为入出度差一半,然后按无向边的定向给它在网络流图中定

【POJ】1739 Tony&#39;s Tour

http://poj.org/problem?id=1739 题意:n×m的棋盘,'#'是障碍,'.'是空白,求左下角走到右下角且走过所有空白格子的方案数.(n,m<=8) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; #define BIT(a,b) ((a)<<((b)<<1)) #

HDU1224 Free DIY Tour 【SPFA】

Free DIY Tour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3939    Accepted Submission(s): 1262 Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulf

【POJ】【1739】Tony&#39;s Tour

插头DP 楼教主男人八题之一! 要求从左下角走到右下角的哈密顿路径数量. 啊嘞,我只会求哈密顿回路啊……这可怎么搞…… 容易想到:要是把起点和重点直接连上就变成一条回路了……那么我们就连一下~ 我们可以在整张图下面加两行:(例:3*5) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 红色的是原来的起点和终点,下面紫色的路径将这两个点连了起来= =然后这题瞬间就变回了Formula 1那题……求哈密顿回路数量,so easy有没有~(其实不这样

【分享】近4000份数学学习资源免费分享给大家

一直以来喜欢收集数学类的教程资源,于是费了好大劲从万千合集站上扒拉了下来,总结归类了一下,一共有将近4000本电子书.经测试,均可免费下载,可能会弹出小广告,可不必理会之.[仅供学术学习和交流,请无用于商业用途.]另外,如有可能,还请尽量支持正版纸质书.   数学史(54)     数学史.rar 55.6 MB   数学的起源与发展.rar 4.3 MB   费马大定理—一个困惑了世间智者358年的谜.pdf 9.5 MB   通俗数学名著译丛14-无穷之旅:关于无穷大的文化史.pdf 14.