首页 > 其他分享 >ZOJ 3195 Design the city (在线LCA,4级)

ZOJ 3195 Design the city (在线LCA,4级)

时间:2023-02-24 10:33:45浏览次数:63  
标签:city rmq int ZOJ vis 3195 printf bit dis


J - Design the city


Crawling in process...

Crawling failed

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu


​Submit​​​ ​​​Status​


System Crawler (2013-05-30)



Description



Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.

In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.

Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.



Input



The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.

Process to the end of file.



Output



Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.

Output a blank line between each test cases.



Sample Input




4
0 1 1
0 2 1
0 3 1
2
1 2 3
0 1 2
5
0 1 1
0 2 1
1 3 1
1 4 1
2
0 1 2
1 0 3




Sample Output




3
2

2
2



思路:就是个模板的水LCA,依次求出两两点间的最短路加和除2就是答案,最短路用LCA求。



失误点:虽然只有n个元素,求bit时也要求到bit[n],我居然求到bit[n-1],看了半天也没看出问题,居然把如此简单LCA给写搓了,好伤心,sad,给个调试的搓代码



提醒下自己。




#include<iostream>
#include<cstring>
#include<cstdio>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
#define ll(x) (1<<x)
using namespace std;
const int mm=2e5+9;
int rmq[mm][25];
int N,head[mm],dfs_clock,to[mm],vis[mm],dis[mm];
int bit[mm],edge;
class Edge
{
public:int v,next,w;
}e[mm];
void data()
{
clr(head,-1);edge=0;
}
void add(int u,int v,int w)
{
e[edge].v=v;e[edge].w=w;e[edge].next=head[u];head[u]=edge++;
}
void dfs(int u,int dep)
{ int v;
dis[u]=dep;
to[dfs_clock]=u;
vis[u]=dfs_clock++;
for(int i=head[u];~i;i=e[i].next)
{
v=e[i].v;
if(vis[v]==-1)
{
dfs(v,dep+e[i].w);
to[dfs_clock++]=u;
}
}
}

void initRMQ()
{ bit[0]=-1;
int n=dfs_clock;
//FOR(i,1,n-1)bit[i]=(i&(i-1))==0?bit[i-1]+1:bit[i-1];不包括n错了。
FOR(i,1,n)bit[i]=(i&(i-1))==0?bit[i-1]+1:bit[i-1];
FOR(i,0,n-1)rmq[i][0]=dis[ to[i] ];
/*FOR(i,0,n-1)
{
printf("qw=%d %d %d %d\n",i,to[i],dis[to[i]],rmq[i][0]);
}*/
//puts("+++++++++++++++");
//printf("bit=%d %d %d %d\n",n,bit[2],bit[n],ll(1));
FOR(i,1,bit[n])
for(int j=0;j+ll(i)-1<n;++j)
{rmq[j][i]=min(rmq[j][i-1],rmq[j+ll(i-1)][i-1]);
//printf("rmq %d %d %d\n",j,i,rmq[j][i]);
}
}
void find_bcc()
{
clr(vis,-1);
dfs_clock=0;
dfs(0,0);
initRMQ();
/* FOR(i,0,N-1)
printf("dis=%d %d\n",i,dis[i]);
FOR(i,0,N-1)
printf("h=%d %d\n",i,vis[i]);
FOR(i,0,dfs_clock-1)
printf("to=%d %d\n",i,to[i]);
*/
}
int RMQ(int l,int r)
{ //printf("t=%d %d %d %d ",to[l],to[r],l,r);
if(l>r)l^=r,r^=l,l^=r;
int t=bit[r-l+1];
r-=ll(t)-1;
int z=min(rmq[l][t],rmq[r][t]);
// printf("%d\n",z);
return z;
}
int main()
{ int a,b,c,cas=0;
while(~scanf("%d",&N))
{ if(cas)printf("\n");++cas;
data();
FOR(i,2,N)
scanf("%d%d%d",&a,&b,&c),add(a,b,c),add(b,a,c);
find_bcc();
int Q;
scanf("%d",&Q);
while(Q--)
{
scanf("%d%d%d",&a,&b,&c);
int ans=0;
/*ans+=dis[a]+dis[b]-2*RMQ(a,b);
ans+=dis[a]+dis[c]-2*RMQ(a,c);
ans+=sia[b]+dis[c]-2*RMQ(b,c);
*/ans=dis[a]+dis[b]+dis[c]-RMQ(vis[a],vis[b])-RMQ(vis[b],vis[c])-RMQ(vis[a],vis[c]);
printf("%d\n",ans);
}
}
}



标签:city,rmq,int,ZOJ,vis,3195,printf,bit,dis
From: https://blog.51cto.com/u_15953788/6082907

相关文章

  • ZOJ 1004 Anagrams by Stack(dfs堆栈)
    AnagramsbyStackTimeLimit:2Seconds     MemoryLimit:65536KBHowcananagramsresultfromsequencesofstackoperations?Therearetwosequenc......
  • ChatGPT is at capacity right now.ChatGPT Plus subscriber login Add your email fo
    原因你的ip地区用的人过多。解决办法在右侧页面填入邮箱,能收到邮件立刻就能登录,如果没有,就换地区试试,不行就等等吧邮件示例:......
  • selenium结合tenacity的retry实现验证码失败重试
    说在前面验证码登录的demo后续可以单独讲解,VIP学员对这部分应该都是掌握的,此处不再赘述本文假设了一个场景你通过OCR识别的验证码是有一定的错误几率的本文是通过识......
  • BZOJ 4145 [AMPPZ2014]The Prices (状压DP)
    题意你要购买商店,你到第i家商店的路费为,在第家商店购买第种物品的费用为,求最小总费用。分析很容易定义出状态,表示到第行,买的物品情况为的最小费用。按照往常的套路是转移时......
  • [bzoj 4176] Lucas的数论 (杜教筛 + 莫比乌斯反演)
    题面设为的约数个数,给定,求题目分析有这样一个结论这道题就是下面这道题的数据增强版,那么这个结论的证明就不再赘述,请自行查看下面的(蒟蒻)博客​​传送门:[SDOI2015][bz......
  • [bzoj 2693] jzptab & [bzoj 2154] Crash的数字表格 (莫比乌斯反演)
    题目描述组数据,给出,,求题目分析直接开始变换,假设N<M总算推完了…此时只需要线性筛出,然后处理的前缀和而可以出利用整除分块优化,时间复杂度为ACcode([bzoj2693]j......
  • [bzoj 1471] 不相交路径 (容斥原理)
    题目描述给出一个结点的有向无环简单图。给出个不同的点,,,,定义不相交路径为两条路径,两条路径的起点分别为和,对应的两条路径的终点为和,要求满足这两条路径不相交,即两条路径上没......
  • [Sdoi2013] [bzoj 3198] spring (hash+容斥原理)
    题目描述给出个维坐标,求有多少对点对满足恰好个位置相等坐标数值在以内题目分析这道题一看就是hash容斥原理,用个位置对应相等个位置对应相等个位置对应相等的…但是不能......
  • [bzoj 3701] Olympic Games (莫比乌斯反演)
    题目描述给出表示一个的格点图,求能够互相看见的点对个数对取模的值.能互相看见定义为此两点连线上没有其他的格点且欧氏距离在[l,r]范围内题目分析首先我们将上下左右相邻......
  • [bzoj 2393] Cirno的完美算数教室 (容斥原理+dfs剪枝)
    题目描述发现了一种数,这种数呢只含有和两种数字现在想知道中有多少个数能被数整除  1<L<R<10^{10}题目分析由于R<10^{10},最大只有10位的数可以对答案造成贡献,每一位只能......