Travel

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2404    Accepted Submission(s): 842

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 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 integer T,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 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.

 
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
新方法,带权的并查集,在并查集集合里面根节点记录了该树的节点个数num,我们每次都把下标较小的点作为根节点。
题目大意:
  给定n个城市,以及m条城市之间的道路。每条道路都有一个权值val,给定一个q,求用到所有边权不大于这个值的的情况下,能够互相到达的点对的个数。
思路:
  对边权值,询问值q排序从小到大,然后将小于q的边的两端点合并,下标小的点设为根节点,记录树的规模。
  每次合并,ans+=2*num[u]*num[v];
 #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长春区域赛 带权并查集)的更多相关文章

  1. hdu 1829-A Bug's LIfe(简单带权并查集)

    题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...

  2. HDU 5176 The Experience of Love 带权并查集

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  3. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  4. 2017乌鲁木齐区域赛I(带权并查集)

    #include<bits/stdc++.h>using namespace std;int f[200010];//代表元long long rl[200010];//记rl[i]为结点 ...

  5. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  6. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

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

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

  9. 【带权并查集】HDU 3047 Zjnu Stadium

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...

随机推荐

  1. EF查询

    public ActionResult AllSettings(DataSourceRequest command, Framework.Kendoui.Filter filter = null, S ...

  2. 递归实现全排列序列C语言实现

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下鄙人用递归回溯的办法,采用C语言实现了全排列序列,用以某些优化方案的原始方案的给定 #incl ...

  3. CART剪枝

    与上篇文章中提到的ID3算法和C4.5算法类似,CART算法也是一种决策树分类算法.CART分类回归树算法的本质也是对数据进行分类的,最终数据的表现形式也是以树形的模式展现的,CART与ID3,C4. ...

  4. 设置EntityFramework 在开发时自动更新数据库

    1. NuGet 下载EntityFramework. 2. 定义Context 和 打开NuGet 命令 执行 Enable-Migrations , Libaray.DAL.Migrations. ...

  5. 微软官方的Unity支持组件

    https://unity.codeplex.com/ http://www.nuget.org/packages/Unity.Interception/ http://www.nuget.org/p ...

  6. linux下strace命令详解

    简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核 ...

  7. JavaScript面向对象之类的继承

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. C#关闭显示屏,使显示屏处于待机状态

    class Program { private const uint WM_SYSCOMMAND = 0x112; //系统消息 private const int SC_MONITORPOWER = ...

  9. Uiviewcontroller 控制器的生命周期

    这是一个ViewController完整的声明周期,其实里面还有好多地方需要我们注意一下: 1:initialize函数并不会每次创建对象都调用,只有在这个类第一次创建对象时才会调用,做一些类的准备工 ...

  10. All X(思维)

    All X Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Subm ...