Travel(HDU 5441 2015长春区域赛 带权并查集)
Travel
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2404 Accepted Submission(s): 842
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 mbidirectional 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.
Note that (a,b) and (b,a) are counted as different pairs and a and b must be different cities.
1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define Max 20000+5
using namespace std;
struct edge
{
int s,e,val;
}a[+];
struct ques
{
int d,id,res;
}p[];
int per[Max],num[Max];
int n,m,q;
bool cmp(ques a,ques b)
{
return a.d<=b.d;
}
bool cmp1(ques a,ques b)
{
return a.id<b.id;
}
void init()
{
for(int i=;i<=n;i++)
{
per[i]=i;
num[i]=;
}
for(int i=;i<=q;i++)
p[i].res=;
}
int find(int x)
{
if(x==per[x])
return x;
return per[x]=find(per[x]);
}
int unite(int a,int b)
{
a=find(a);
b=find(b);
if(a<b) //a最大
swap(a,b);
per[a]=b; //根节点下标最小,记录树的节点个数
num[b]+=num[a];
return ;
}
bool cmp2(edge a,edge b)
{
return a.val<b.val;
}
int main()
{
int T;
int i,j;
freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&q);
init();
for(i=;i<m;i++)
scanf("%d%d%d",&a[i].s,&a[i].e,&a[i].val);
for(i=;i<q;i++)
{
scanf("%d",&p[i].d);
p[i].id=i;
}
sort(a,a+m,cmp2);
sort(p,p+q,cmp);
j=;
int t1,t2,ans=;
for(i=;i<q;i++)
{
while(j<m&&a[j].val<=p[i].d)
{
t1=find(a[j].s);
t2=find(a[j].e);
j++;
if(t1!=t2)
{
ans+=*num[t1]*num[t2];
unite(t1,t2);
}
}
p[i].res=ans;
}
sort(p,p+q,cmp1);
for(i=;i<q;i++)
printf("%d\n",p[i].res);
}
}
Travel(HDU 5441 2015长春区域赛 带权并查集)的更多相关文章
- hdu 1829-A Bug's LIfe(简单带权并查集)
题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...
- HDU 5176 The Experience of Love 带权并查集
The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- 2017乌鲁木齐区域赛I(带权并查集)
#include<bits/stdc++.h>using namespace std;int f[200010];//代表元long long rl[200010];//记rl[i]为结点 ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- HDU 3047 Zjnu Stadium(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...
- HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]
传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- 【带权并查集】HDU 3047 Zjnu Stadium
http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...
随机推荐
- 如何在异步请求时设置RequestHeader
一.为何要用到setRequestHeader 通常在HTTP协议里,客户端像服务器取得某个网页的时候,必须发送一个HTTP协议的头文件,告诉服务器客户端要下载什么信息以及相关的参数.而 XMLHTT ...
- Java学习系列(一)Java的运行机制、JDK的安装配置及常用命令详解
俗话说:“十五的月亮十六圆”.那学习是不是也是如此呢?如果把月亮看成是我们的愿望,那十五便是我们所处的“高原期”,坚持迈过这个坎,我相信你的愿望终究会现实的.记得马云曾说:今天很残酷,明天更残酷,后天 ...
- Linux服务器挂死案例分析
问题现象: 在linux服务器上运行一个指定的脚本时,就会出现无数个相同进程的,而且不停的产生,杀也杀不掉,最后系统就陷入死循环,无法登陆,只能人工去按机器的电源键才可以.这够崩溃的吧? 问题分析过程 ...
- DoTween学习笔记(一)
DOTween是一个快速,高效,完全统一的类型安全的对象属性动画引擎,免费开源,大量的高级特性. DoTween兼容Unity4.5以上的版本,支持的平台: Win, Mac, Unity WebPl ...
- blockUI
组件主页 主要使用到 blockUI 组件实现 将jquery和组件的JS下载到本地 然后直接就可以实现遮罩层功能 显示遮罩层:$.blockUI(); 隐藏遮罩层:$.unblockUI(); 该网 ...
- [POJ 3734] Blocks (矩阵高速幂、组合数学)
Blocks Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3997 Accepted: 1775 Descriptio ...
- [Ext JS 4] 实战之Grid, Tree Gird编辑Cell
前言 本篇这里以稍微复杂一点的Tree Grid 来介绍. 在写编辑grid 之, 先来看一下 grid 的 selType 的配置. 先给一个简单的Tree grid 的例子: Ext.onRead ...
- poj 1523 SPF(tarjan求割点)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- [Hapi.js] Route parameters
Routing is a fundamental aspect of any framework. In this lesson, you'll learn how to use path param ...
- ERROR 1130: Host is not allowed to connect to this MySQL server
解决远程连接mysql错误1130代码的方法 今天在用远程连接Mysql服务器的数据库,不管怎么弄都是连接不到,错误代码是1130,ERROR 1130: Host 192.168.2.159 is ...