题目链接https://nanti.jisuanke.com/t/28852

题目大意是 h*w 的平面,每两个点有且仅有一条路径,类似于封闭的联通空间,然后在这h*w个点中选取(标记为1~N)N个点(给了坐标),求从1号点按1~N的顺序走到N号点的路程。

练习赛的时候没有思路,队友说可以建树,但还是不清不楚没写出来。

做法是LCA。

将封闭的联通空间建树(点的位置与字符的位置有点麻烦),然后按顺序求两点的最近的公共祖先求深度得距离,最后得路程,算是一道LCA的模板。

 #include <bits/stdc++.h>
#define fir first
#define sec second
#define EPS 1e-12
using namespace std; typedef long long LL;
typedef pair<int , int > pii;
const int MAXN=+;
const int DEG=; struct Edge{
int to,nxt;
}edge[MAXN*];
int head[MAXN],tot; void addEdge(int u,int v){
edge[tot].to=v;
edge[tot].nxt=head[u];
head[u]=tot++;
}
void init(){
tot=;
memset(head,-,sizeof(head));
}
int fa[MAXN][DEG];
int deg[MAXN]; void BFS(int root){
queue< int > que;
deg[root]=;
fa[root][]=root;
que.push(root);
while(!que.empty()){
int tmp=que.front();que.pop();
for(int i=;i<DEG;++i)
fa[tmp][i]=fa[fa[tmp][i-]][i-];
for(int i=head[tmp];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(v==fa[tmp][]) continue;
deg[v]=deg[tmp]+;
fa[v][]=tmp;
que.push(v);
}
}
} int LCA(int u,int v){
if(deg[u]>deg[v]) swap(u,v);
int hu=deg[u],hv=deg[v];
int tu=u,tv=v;
for(int det=hv-hu,i=;det;det>>=,++i)
if(det&) tv=fa[tv][i];
if(tu==tv) return tu;
for(int i=DEG-;i>=;--i){
if(fa[tu][i]==fa[tv][i])
continue;
tu=fa[tu][i];
tv=fa[tv][i];
}
return fa[tu][];
} char maze[][];
int H,W; void Judge(int cur,int xx,int yy){
int i=xx,j=(yy+)/;
if(maze[xx-][yy]!='_'){
int to=(i-)*W+j;
addEdge(cur,to);
}
if(maze[xx][yy]!='_'){
int to=(i)*W+j;
addEdge(cur,to);
}
if(maze[xx][yy-]!='|'){
int to=(i-)*W+j-;
addEdge(cur,to);
}
if(maze[xx][yy+]!='|'){
int to=(i-)*W+j+;
addEdge(cur,to);
}
} LL caldist(int u,int v){
int ace=LCA(u,v);
return deg[u]+deg[v]-*deg[ace];
} int main()
{
init();
scanf("%d%d%*c",&H,&W);
for(int i=;i<=H;++i){
scanf("%[^\n]%*c",maze[i]);
}
for(int i=;i<=H;++i){
for(int j=;j<=W;++j){
int tmp=(i-)*W+j;
int xx=i,yy=*j-;
Judge(tmp,xx,yy);
}
}
BFS();
LL ans=;
int N,xi,yi;
scanf("%d%d%d",&N,&xi,&yi);
int old=(xi-)*W+yi,aft;
for(int i=;i<N;++i){
scanf("%d%d",&xi,&yi);
aft=(xi-)*W+yi;
ans+=caldist(old,aft);
old=aft;
}
printf("%lld\n",ans);
return ;
}

German Collegiate Programming Contest 2018​ A. Attack on Alpha-Zet的更多相关文章

  1. German Collegiate Programming Contest 2018​ B. Battle Royale

    Battle Royale games are the current trend in video games and Gamers Concealed Punching Circles (GCPC ...

  2. German Collegiate Programming Contest 2018​ C. Coolest Ski Route

    John loves winter. Every skiing season he goes heli-skiing with his friends. To do so, they rent a h ...

  3. German Collegiate Programming Contest 2018​

    // Coolest Ski Route #include <iostream> #include <cstdio> #include <cstring> #inc ...

  4. 2018 German Collegiate Programming Contest (GCPC 18)

    2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <al ...

  5. (寒假GYM开黑)2018 German Collegiate Programming Contest (GCPC 18)

    layout: post title: 2018 German Collegiate Programming Contest (GCPC 18) author: "luowentaoaa&q ...

  6. ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018

    ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syr ...

  7. (寒假开黑gym)2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017)

    layout: post title: (寒假开黑gym)2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017) au ...

  8. German Collegiate Programming Contest 2015 计蒜课

    // Change of Scenery 1 #include <iostream> #include <cstdio> #include <algorithm> ...

  9. 2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017)(9/11)

    $$2017-2018\ ACM-ICPC\ German\ Collegiate\ Programming\ Contest (GCPC 2017)$$ \(A.Drawing\ Borders\) ...

随机推荐

  1. github二次认证接收短信的问题

    这两天登陆github都被提示说我账号的密码已经被归类,提醒我修改密码. 改密码的时候发现GitHub有个二次认证的功能,分别是app和sms短信认证.app这里就不讲了,我们讲一下github的短信 ...

  2. 在xcode 上调试c程序

    打开xcode 选择 Create a new Xcode project 选择Command Line Tool 给你的项目起个名,选择c语言 点击next 选择存储位置,就会制动生成一个项目,在项 ...

  3. [转]python中pandas库中DataFrame对行和列的操作使用方法

    转自:http://blog.csdn.net/u011089523/article/details/60341016 用pandas中的DataFrame时选取行或列: import numpy a ...

  4. 【python】脚本连续发送QQ邮件

    今天习得用python写一个连续发送QQ邮件的脚本,经过测试,成功给国内外的服务器发送邮件,包括QQ邮箱.163邮箱.google邮箱,香港科技大学的邮箱和爱丁堡大学的邮箱.一下逐步解答相关技巧. 首 ...

  5. 安装JVCL/JCL组件

    在安装的时候,注意要先安装JCL,我试图直接安装JVCL,提示找不到文件,先安装JCL后再安装就不存在这个问题.安装到组件面板上的安装包以D结尾,可以Install,以R结尾的只要编译就可以了. 安装 ...

  6. Java中用字符串常量赋值和使用new构造String对象的区别

    String str1 = "ABC"; String str2 = new String("ABC"); String str1 = “ABC”;可能创建一个 ...

  7. ubuntu crontab python 定时任务备记

    crontab -e 写入: # at a.m every week with: # * * tar -zcf /var/backups/home.tgz /home/ # # For more in ...

  8. centos7+nginx+rtmp+ffmpeg搭建流媒体服务器(保存流目录与http目录不要随意配置,否则有权限问题)

    搭建nginx-http-flv-module升级代替rtmp模块,详情:https://github.com/winshining/nginx-http-flv-module/blob/master ...

  9. 删除文件夹下各级子目录中的.svn文件

    建立一个文本文件,取名为removeSvn.reg(扩展名由txt改为reg),内容如下 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHIN ...

  10. 支付宝 net