HDU 5441 Travel (带权并查集 + 离线操作)

手把手教你用Verilog实现AT24C64的I2C读写(附完整仿真流程) 本文详细介绍了如何使用Verilog HDL设计AT24C64 EEPROM的I2C控制器,并配合ModelSim完成完整的仿真验证流程。内容涵盖I2C协议解析、Verilog状态机设计、AT24C64读写操作实现,以及仿真波形分析和调试技巧,为FPGA开发者提供了一套完整的EEPROM控制解决方案。 阅读详情

Travel

Problem Description
Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There aren cities and m bidirectional roads connecting the cities. Jack hates waiting too long on the bus, but he can rest at every city. Jack can only stand staying on the bus for a limited time and will go berserk after that. Assuming you know the time it takes to go from one city to another and that the time Jack can stand staying on a bus is x minutes, how many pairs of city (a,b) are there that Jack can travel from city a to b without going berserk?
 

Input
The first line contains one integerT,T≤5, which represents the number of test case.

For each test case, the first line consists of three integers n,m and q where n≤20000,m≤100000,q≤5000. The Undirected Kingdom has n cities and m bidirectional roads, and there are q queries.

Each of the following m lines consists of three integers a,b and d where a,b∈{1,...,n} and d≤100000. It takes Jack d minutes to travel from city a to city b and vice versa.

Then q lines follow. Each of them is a query consisting of an integer x where x is the time limit before Jack goes berserk.

 

Output
You should print q lines for each test case. Each of them contains one integer as the number of pair of cities(a,b) which Jack may travel from a to b within the time limit x.

Note that (a,b) and (b,a) are counted as different pairs and a and b must be different cities.
 

Sample Input
1 5 5 3 2 3 6334 1 5 15724 3 5 5705 4 3 12382 1 3 21726 6000 10000 13000
 

Sample Output
2 6 12
 
【思路分析】
   参照别人的思路写的。。。这类题一定要多加训练。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int maxn = 2e4 + 50;
const int maxm = 1e5 + 50;
const int maxq = 5e3 + 50;
int n,m,q,sum;
int sets[maxn],ans[maxn],number[maxn];

struct Edge
{
    int u,v,w;
    Edge()
    {

    }
    Edge(int u,int v,int w) : u(u),v(v),w(w)
    {

    }
    bool operator < (const Edge &e) const
    {
        return w < e.w;
    }

}edge[maxm];
struct Query
{
    int val,index;
    bool operator < (const Query &q) const
    {
        return val < q.val;
    }
}query[maxq];

void init()
{
    sum = 0;
    scanf("%d %d %d",&n,&m,&q);
    for(int i = 1;i <= n;i++)
    {
        sets[i] = i;
        number[i] = 1;
    }
    for(int i = 1;i <= m;i++)
    {
        scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w);
    }
    for(int i = 1;i <= q;i++)
    {
        scanf("%d",&query[i].val);
        query[i].index = i;
    }
    sort(edge + 1,edge + m + 1);
    sort(query + 1,query + q + 1);
}
int finding(int x)
{
    return sets[x] = (sets[x] == x ? x : finding(sets[x]));
}
void merging(int a,int b)
{
    int p1 = finding(a);
    int p2 = finding(b);
    if(p1 != p2)
    {
        int x = number[p1];
        int y = number[p2];
        sum -= x * (x - 1);//即x个结点两两之间边的权值满足条件的点对数
        sum -= y * (y - 1);
        sets[p1] = p2;
        number[p2] += number[p1];
        number[p1] = 0;//以上三步将集合a并入集合b
        sum += (number[p2]) * (number[p2] - 1);
    }
}
void solve()
{
    int num = 1;
    for(int i = 1;i <= q;i++)
    {
        while(num <= m && edge[num].w <= query[i].val)//权值不大于q值时进行合并
        {
            merging(edge[num].u,edge[num].v);
            num++;
        }
        ans[query[i].index] = sum;
    }
    for(int i = 1;i <= q;i++)
        printf("%d\n",ans[i]);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        solve();
    }
    return 0;
}


logisim实验六:MIPS运算器设计 实验目的 学生理解算术逻辑运算单元(ALU)的基本构成,掌握 Logisim 中各种运算组件的使用方法,熟悉多路选择器的使用,能利用前述实验完成的 32 位加法器、 Logisim 中的运算组件构造指定规格的 ALU 单元。 设计要求 利用前面实验封装好的 32 位加法器以及 logisim 平台中现有运算部件构建一个 32 位算术逻辑运算单元(禁用 Logisim 系统自带的加法器,减法器),可支持算术加、减、乘、除,逻辑与、或、非、异或运算、逻辑左移、逻辑右移、算术右移运算,支持常用程序状态标志(有符号 阅读详情

相关推荐

COMSOL解的继承

COMSOL解的继承1. 应用场景2. 实现方法2.1. 添加研究步骤2.2. 引用研究 根据参考文献整理而来,原文见: http://blog.sina.com.cn/s/blog_4a0a8b5d010126w5.html https://www.zhihu.com/question/371867999/answer/1309194094 个人学习笔记,如有疏漏,欢迎补充 1. 应用场景 在COMSOL模拟中,一次求解之后往往遇到以下几种情况需要用到解的继承: 1)瞬态求解的继续运算。例如初始设定求解1

jessica0307的博客 2万+

Travel HDU - 5441(技巧题,并查集)

题目 Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities and m bidirectional roads connecting the cities. Jack hate...

sunmaoxiang的博客 492

ReportStudio入门教程(四十一) - 固定报表列宽

我们继续上一篇博客,让他看起来

什么是江湖? 3521

【HDU 5441】Travel

Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There arenncities andmmbidirectional roads connecting the cities. Jack hat...

词庸 550

别再只会调速度了!用STM32的定时器中断精准控制伺服电机转角(避坑指南)

本文详细介绍了如何利用STM32的定时器中断和PWM输出实现伺服电机的精准转角控制,避免常见的速度控制误区。通过硬件配置、脉冲计数逻辑和优化技巧的实战讲解,帮助开发者掌握高精度位置控制方法,适用于工业自动化和机器人控制等场景。

weixin_42531163的博客 423

北京联通IPTV组播实战:OpenWRT软路由配置全解析

本文详细解析了在北京联通IPTV环境下,利用OpenWRT软路由和udpxy工具实现组播转单播的实战配置。内容涵盖网络接口设置、防火墙规则、udpxy安装配置、直播源M3U文件处理以及全平台播放指南,帮助用户无需机顶盒即可在家庭局域网内任何设备上流畅观看电视直播。

weixin_29313547的博客 1437

HDU - 5441(并查集)

TravelJack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are nn cities and mm bidirectional roads connecting the cities. Jack hate

siyu 365

历史预测+LLM双杀!DriveGPT4-V2闭环驾驶配置避坑指南

本文深入探讨了DriveGPT4-V2在工程化部署中的核心挑战与解决方案。通过融合历史轨迹预测与大语言模型,该模型旨在实现更可靠的端到端自动驾驶。文章详细解析了从环境配置、多模态Token对齐、轻量控制头设计到训练优化与闭环仿真验证的全流程,为开发者提供了一份详实的避坑指南。

transformer2023的博客 387

hdu 5441 Travel(带权并查集)

Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities and m bidirectional roads connecting t

ZAQ的博客 2873

3D点云修复实战:用PCN网络快速补全缺失的激光雷达数据(附代码)

本文详细介绍了PCN(Point Completion Network)网络在3D点云修复领域的实战应用。针对激光雷达数据常见的缺失问题,PCN通过端到端的深度学习架构,直接处理原始点云,实现由粗到精的快速补全。文章深入解析了其核心思想与编码器-解码器架构,并提供了完整的环境搭建、数据准备、模型实现、训练评估及在真实场景下的优化策略与代码示例,为自动驾驶、机器人等领域的开发者提供了高效的数据修复解决方案。

weixin_29041195的博客 399

嵌入式工程师必学(9):USB 3.0协议理解

USB3.0和USB2.0相比,在线缆上从4根线(Vbus、GND、D+、D-)变为了8根线(Vbus、GND、D+、D-、SSRX+\SSRX-\SSTX+\SSTX-),可见增加的4根线都是和超速相关的,新增加的超速有两类,分别为:超速Gen1(5.0Gbps),超速+Gen2(10Gbps),还兼容USB2.0的3类速率:low-speed(1.5Mbps),full-speed(12Mbps),andhigh-speed(480Mbps)。从设计可以看出,USB3....

码农17年的博客 1万+

离线+并查集 Portal

做这题学到了什么是“离线算法”的概念。所谓“离线”,就是把所有的数据都输入之后再计算,“在线”就是边输入边计算。 用在这题中,是因为输入中的“询问部分”,有Q 个问,每个L可以有多少种不同路径。由于大的L必定会包含到小的L, 所以把所有问题都输入,再从大到小排序,再计算,可以减少很多计算量。 Description ZLGG found a magic theory

bingbingsyr的专栏 731

Open3d (C++) RANSAC拟合圆柱(详细过程版)

实现了一种基于RANSAC算法的圆柱拟合方法。

点云侠的博客 245

HDU 5441 Travel 并查集

HDU 5441 Travel   题意:一张无向图,q个查询,对于每个x,有多少对点之间的路径中最长的一条路不大于x。 思路:比赛时王秋平写的,我补下题。这题也比较简单,将边排序,从小到大加到并查集,对查询也排序,从小到大对于每个查询把不大于x的边加到并查集,用cnt[y]记录以y为根的连通块有多少节点,那么在连通块发生 变化时,ans=2 * cnt[x] * cnt[y]  

Macan - 生活点滴 377

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

hdu 5441 Travel(生成树)

题目链接:hdu 5441 Travel 类似与最小生成树的算法,将边和询问分别按照权值排序,每次枚举一个询问,将小于该询问的边全部链接,并且计算ans。 #include #include #include #include using namespace std; typedef long long ll; const int maxn = 20005; con

不慌不忙、不急不躁 1088

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

hdu 5441 Travel 排序 并查集

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2149    Accepted Submission(s): 740 Problem Description Jack likes to trav

G.D.Retop的专栏 500

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

hdu5441 Travel

题目TravelProblem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities and m bidirectional roads connecting

痕 1200

HDU 5441 Travel (带权并查集 离线)

题目 HDU 5441 Travel 题意 n个点,m条边,每条边权值val q次询问,每次有一个值v,将图中所有小于v的边标记出来组成新图,求里面有多少个点对可以相互到达(a -> b 和 b -> a 算两个) 解法 离线并查集,记录每个集合里面点的个数,每次合并两个点数为x, y的集合,对ans的贡献为 \((x+y)\*(x+y-1) - x\*(x-1) - y\*(y-1)...

weixin_30642305的博客 122

HDU 5441 Travel

HDU 5441 Travel #include #include #include #include #include #include #include #define maxn 100005 #define INF 0x3fffffff #define ll __int64 #define mod 10000007 #define N 20005 #define M 100005 usin

mitic的博客 419

HDU 5441 Travel(离线操作,并查集)

题目链接:HDU 5441 Travel 题目大意: 有一个n个点的无向图,给出m条边的边权,给出q次询问,每次给出一个值,求用到所有边权不大于这个值的边的情况下,能够互相到达的点对的个数(自己到自己不算) (a,b) (b,a) 不同当且仅当a!=b 当时做的时候思路很快就想出来了,就是每次把满足条件的点分成不同的集合,每个集合的点互相可达,每个集合正好构成无向完全图

Winjourn的专栏 652

hdu 5441 Travel(带权并查集)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1586    Accepted Submission(s

theArcticOcean 765

hdu 5441 Travel 离线带权并查集

转自:http://www.cnblogs.com/qscqesze/p/4805266.html题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5441 Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is trave

行云出岫 248

hdu 5441 Travel(Kruskal+离线)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意: 给定N个顶点,M条边的一个无向图,Q个询问。 对于每个询问x,从a,b的路径上各边的最大权值小于x,可以记为有序对, 求这个图里面有多少个这样的有序对。 解题思路:kruskal思想+离线。首先把边从小到大排序,对于某个限制limit,如果边小于等于limit,则表示这条边是

hexianhao的博客 618

离线+并查集 hdu5441 Travel

传送门:点击打开链接 题意:给n个点,m条有权值的无向边,求存在多少个点(a,b) (a!=b),使得a到b至少有一条路径上的边最大权值小于等于x 思路:将边读入排序,再先将x排序,初始化并查集,用并查集维护一个联通分量中的点的个数,让ans等于0 按照边的权值从小到大添加边到图中,首先考虑如何更新ans 添加的某条边如果是(u,v),如果u和v本身就连通了,那么再添加这条边并不会改变an

qwb的博客 995

hdu-5441-Travel-带权并查集

题意  给你n个城市m个边,每条边有一权值,表示路费 如果给出一笔钱,钱大于路费则表示该条路可以通过 通过这条路 则代表 点 (a,b)  和(b,a)是合法的点,合法的对数为2 k次查询,每次查询给出一笔钱,求该笔钱能走过的所有点的合法对数 对输入的边按权值排序,对输入的钱数按权值排序 对第一笔钱,把权值比钱小的边 全都加到一个并查集, 设两端点为t1,t2 i

缺氧 825

hdu 5441 长春区域赛网络赛 1005 Travel(并查集)

题目链接:hdu 5441题目大意:有一个n个点的无向图,给出m条边的边权,给出q次询问,每次给出一个值,求用到所有边权不大于这个值的边的情况下,能够互相到达的点对的个数(自己到自己不算)题目分析: 首先我们对于边按照边权从小到大排序,对于询问按照值从小到大排序。 枚举每次询问,从前到后扫描边,如果下一条边的边权大于当前的询问的值,那么停止,利用并查集记录每个联通块的点的个数。 每次如果某两个联通块

llin-黎辰 2411
上一篇: POJ 3130 How I Mathematician Wonder What You Are!(半平面交求多边形的核)
下一篇: UVA 10881 Piotr's Ants
Spectacules
博客等级 码龄12年 2粉丝 69原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值