ZOJ3195 Design the city

目标检测 RT-DETR(2023)详细解读 在使用了这个技巧后,显然训练过程中,类别的标签不再是此前的0和1离散值,而是0~1的连续值。关于 Query Selection(查询向量选择),大家应该并不陌生,这个方法可谓在DETR领域大杀四方,如DAB-DETR对查询向量进行重构理解,将其解释为Anchor,DN-DETR通过查询降噪来应对匈牙利匹配的二义性所导致的训练时间长的问题,DINO提出从Encoder中选择Top-k特征进行学习等一系列方法,这都无疑向我们证明,查询向量很重要,选择好的Query能够让我们事半功倍。在RT-DETR中, 阅读详情

Design the city

Time Limit: 1 Second Memory Limit: 32768 KB
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就是答案了,就是dist[x1]+dist[x2]+dist[x3]-dist[lca[x1,x2]]-dist[lca[x1,x3]]-dist[lca[x2,x3]]。

dist[x1]+dist[x2]-2*dist[lca[x1,x2]].....dist[x2]+dist[x3]-2*dist[x2,x3].....dist[x1]+dist[x3]-2*dist[x1,x3]....这三个式子相加除以2,就是上面的式子。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=50010;
const int MAXE=140010;
struct EDGE
{
    int v,next;
    int dis;
}edge[MAXN<<1];
struct QUEST
{
    int v,next;
    int num;
}q[MAXE*3];
int head1[MAXN],head2[MAXN],size;
bool vis[MAXN];
int fa[MAXN],dist[MAXN],ans1[MAXE>>1],ans2[MAXE>>1],ans3[MAXE>>1],lca[MAXE*3];
void init()
{
    memset(head1,-1,sizeof(head1));
    memset(head2,-1,sizeof(head2));
    memset(dist,0,sizeof(dist));
    memset(vis,0,sizeof(vis));
    size=0;
}
void add_edge(int u,int v,int dis)
{
    edge[size].v=v;
    edge[size].dis=dis;
    edge[size].next=head1[u];
    head1[u]=size++;
}
void add_q(int u,int v,int i)
{
    q[size].v=v;
    q[size].num=i;
    q[size].next=head2[u];
    head2[u]=size++;
}
int find(int x)
{
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
void tarjan(int u)
{
    fa[u]=u;
    vis[u]=1;
    for(int i=head2[u];i!=-1;i=q[i].next)
    {
        int v=q[i].v;
        if(vis[v])
        {
            lca[q[i].num]=find(v);
        }
    }
    for(int i=head1[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!vis[v])
        {
            dist[v]=dist[u]+edge[i].dis;
            tarjan(v);
            fa[v]=u;
        }
    }
}
int main()
{
    int n,m,i,flag=1;
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n)==1)
    {
        if(!flag)
            printf("\n");
        flag=0;
        init();
        int u,v,c;
        for(i=0;i<n-1;i++)
        {
            scanf("%d%d%d",&u,&v,&c);
            add_edge(u,v,c);
            add_edge(v,u,c);
        }
        scanf("%d",&m);
        int x1,x2,x3;
        memset(lca,0,sizeof(lca));
        size=0;
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&x1,&x2,&x3);
            add_q(x1,x2,i);
            add_q(x2,x1,i);
            add_q(x1,x3,i+m);
            add_q(x3,x1,i+m);
            add_q(x2,x3,i+2*m);
            add_q(x3,x2,i+2*m);
            ans1[i]=x1;
            ans2[i]=x2;
            ans3[i]=x3;
        }
        tarjan(1);
        for(i=0;i<m;i++)
        {
            printf("%d\n",dist[ans1[i]]+dist[ans2[i]]+dist[ans3[i]]-dist[lca[i]]-dist[lca[i+m]]-dist[lca[i+2*m]]);
        }
    }
    return 0;
}


Qt/C++ 音视频开发 - FFmpeg 安卓版 通过将 FFmpeg 集成到 Qt/C++ 项目中,我们可以实现强大且灵活的音视频处理功能。本文介绍了在安卓平台上使用 FFmpeg 的基本步骤,包括初始化、解码、处理和编码等。实际应用代码示例展示了如何在安卓设备上播放视频。 阅读详情

相关推荐

嵌入式开发必备:UART、RS232、RS485通信协议实战对比(附代码示例)

本文从嵌入式开发实战角度,深度对比了UART、RS232和RS485三种核心串行通信协议。详细解析了它们的工作原理、硬件连接、代码配置及典型应用场景,并提供了基于通信距离、节点数量和电磁环境的三维度选型指南,帮助开发者根据项目需求做出最优技术决策。

i0j1k2l3m的博客 474

zoj 3195

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3320 离线算法RE了.. #include<stdio.h> #include<iostream> #include<string.h> #include<math.h> #include<algorithm...

weixin_30551963的博客 189

量化策略开发步骤系列(5)前向测试和统计意义

我们在第 4 部分,展示了我们策略的样本外测试并讨论了其结果。用于开发模型的数据是 2018 年至 2021 年的数据,测试数据是 2021 年至 2022 年的数据。但有一种方法可以更好地利用我们的数据。

码上助君的专栏 1846

zoj 3195 Design the city LCA

Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic j

Azarath Metrion Zinthos 933

Kali下RTL8812AU网卡5G抓包精准适配指南

WiFi监听模式是无线安全测试的基础能力,其核心在于网卡驱动对802.11a/n/ac协议栈的完整支持。RTL8812AU系列USB无线网卡因成本低、兼容性广被广泛用于渗透测试,但其芯片版本碎片化(如VA/VC/ACM)与Linux内核演进(6.1~6.5)存在严重适配断层,导致5GHz频段Beacon帧捕获失败、信道切换异常、注入功能残缺等典型问题。技术价值在于突破‘即插即用’幻觉,建立从USB设备ID识别、固件版本校验到射频寄存器级补丁的全链路可控流程。典型应用场景包括CTF无线赛题实战、红蓝对抗中的W

weixin_33783283的博客 762

VMware 17+Win10 22H2全栈配置指南|UEFI优化+GPU直通实战

​VMware下载:下载官网版本的Wmare 17 Pro (点击直达链接 )企业用户可选永久许可证,个人学习也可以直接安装使用,Win10镜像:微软官网下载22H2版ISO文件(附官方纯净版渠道 )

waicsdn_haha的博客 4019

zoj3195(一个关于LCA的应用,三个节点)

Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffi

小周的专栏 1236

ATE测试基础——直流参数测试

芯片测试在发展过程中诞生了许多术语,初次接触的人大部分都会被绕晕。因此为方便广大读者理解,在开始介绍测试项之前,需要对测试领域的名词进行解释。还有部分较为抽象的内容,需要略作展开。DUT——Device Under Test,正在被测试的器件Pin——器件引脚,信号引脚分为输入,输出,三态和双向四类,负责与外界进行信息交互;电源引脚分为电源和地两类,结构与信号引脚不同,负责给器件内部运行和外部输出提供电平。输入:在外部信号和器件内部逻辑之间起缓冲作用的信号输入通道;

你骄傲地飞远的博客 1万+

zoj 3195 Design the city

题意:找无向树上三个点间的距离 题解:在线lca,开始是想分情况讨论,后来发现可以通过求3对两点间距离除以2来求得答案,感觉是一种尝试将问 题规模变小的思想吧。 总结:这个题因为小错改了好久好久才改出来,以后规定自己纯改错时间不得超过20分钟! 下面贴出两种lca转rmq的实现方法 #include #include #include #include using namespace s

c337134154的专栏 819

MATLAB实战:手把手教你实现Gardner环路位同步(附完整代码)

本文通过MATLAB实战,详细讲解了Gardner环路位同步算法的原理与实现。内容涵盖插值滤波器、定时误差检测、环路滤波器与数控振荡器等核心模块,并提供完整代码和调试技巧,帮助读者掌握数字通信中关键的符号定时同步技术。

weixin_29048775的博客 174

【个人记录】CentOS7升级glibc至2.28

由于CentOS7系统已经停止支持, 虽然大部分业务已经迁移至OpenEuler,但仍然有部分业务系统跑在CentOS7上。在尝试部署新业务环境时经常会提示如下错误(以code-server运行需要的node 20为例)node -v为了保障业务可正常运行,通常采用Docker继承好的node环境进行部署,但部分场景还是需要宿主机使用新版glibc版本。本文记录本人一次成功升级glibc(2.17=>2.28)的步骤。

喵呜 3883

ZOJ 3195 Design the city 【LCA】

传送门 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 rea...

qq_42211531的博客 206

第 0 篇|开篇:为什么我们要从 RNN 讲到 Transformer?

本文介绍了NLP模型的发展历程,从早期规则式方法到如今Transformer模型的演进脉络。文章强调理解NLP技术需要从基础模型入手,重点分析了传统机器学习的局限性(无法处理变长有序输入和深层语义关联),以及RNN如何通过循环结构首次实现顺序依赖捕捉。作者指出Transformer之所以成为主流,是因为它通过自注意力机制解决了RNN的核心痛点(低效串行处理和长距离依赖问题)。专栏将采用"从问题到解决方案"的讲解方式,帮助读者建立完整的NLP知识体系,避免孤立学习和盲目追新。全文旨在为初学

记录 IT 领域经验与见解的博客 73

ZOJ 3195 Design the city 题解

    这个题目大意是:     有N个城市,编号为0~N-1,给定N-1条无向带权边,Q个询问,每个询问求三个城市连起来的最小权值。     多组数据 每组数据 1 < N < 50000 1 < Q < 70000;     一道多源最短路的题目,注意题目数据:N、Q都很大     不能考虑Floyd、SPFA、Dijkstra、Bellman-F...

diaoluo1817的博客 250

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

ZOJ3195(Design the city)

题目传送门 Design the city 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 distribu

weixin_43808717的博客 338

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

zoj 3195 Design the city LCA Tarjan

题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 那么最短距离就是 d = ( d1 + d2 + d3 ) / 2 要注意每个数组的范围大小,因为这个问题手抖敲错,TLE+RE一整页/(ㄒoㄒ)/~~ 用前向星来保存边和询问,空...

ddqb3235的博客 180

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

ZOJ 3195 Design the city

倍增法在线LCA..... ZOJ Problem Set - 3195 Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZhou. As you may know, the traffic system of thi

码代码的猿猿 1317

ZOJ 3195 Design the city LCA

题目:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3195 题意:给定一个树,求三点之间的距离 思路:假定三点为u, v, w,那么(dist[u,v] + dist[v,w] + dist[u,w]) / 2就是答案 总结:wa时不要灰心,也许你离ac不远了 #include #includ

弱智就要多努力 3791

Zoj 3195 Design the city (数据结构_LCA)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3320 题目大意:给定n个节点的树,要求我们查询树上三个点之间的距离。n 解题思路:LCA好题,和普通的LCA不同的是本题查询的是三个点。一开始我的做法很诡异,找三个点的关系,然后离线Tarjan求解。我共找到4个关系,但是总能找到反例拓展这些关系

154

ZOJ 3195 Design the city(LCA变形)

题意:给定一棵树,求连接三点所需的最短距离。 思路:LCA变形,连接三点的最短距离可以转化为求任意两点距离之和的和再除以二。 #include #include #include #include #include #include #include #include #include #include #include #include #includ

GODSPEED 631

zoj3195Design the city

Design the cityTime Limit: 1 Second      Memory Limit: 32768 KBCerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everyw...

Oops的博客 218

[ZOJ3195]Design the city

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...

四叔的博客 261

Design the city ZOJ - 3195 LCA倍增

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...

why932346109的博客 165
上一篇: HDU2489 Minimal Ratio Tree
下一篇: HDU2586 How far away ?
eeeaaaaa
博客等级 码龄12年 2粉丝 155原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值