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

    Xiangqi is one of the most popular two-player board games in China. The game represents a battle bet ...

  2. sgu Kalevich Strikes Back

    这道题就是求一个大矩形被n个矩形划分成n+1个部分的面积,这些矩形之间不会相交,可能包含.. #include <cstdio> #include <cstring> #inc ...

  3. GetSystemTime API可以得到毫秒级时间

    用Now返回的日期格式中年只有2位,即2000年显示为00, 这似乎不太令人满意. 此外Now和Time都只能获得精确到秒的时间,为了得到更精确的毫秒级时间,可以使用API函数GetSystemTim ...

  4. XPath <第四篇>

    .Net框架下的System.Xml.XPath命名空间提供了一系列的类,允许你应用XPath数据模式查询和展示XML文档数据. 一.XPath介绍 XPath有七种类型的节点:元素.属性.文本.命名 ...

  5. 一个简单的webserver

    用c语言写了一个web server,特别简单共计一个文件且不到200行. 当然目前的问题还有很多, 不支持php,对图片支持不好,日志功能还没有完善 这些后期都会加上! gcc server.c - ...

  6. yii第一个应用blog

    1. 连接到数据库 大多数 Web 应用由数据库驱动,我们的测试应用也不例外.要使用数据库,我们首先需要告诉应用如何连接它.修改应用的配置文件 WebRoot/testdrive/protected/ ...

  7. 【转】Android(4.2) Sensors 学习——G-sensor,Gyroscope驱动移植

    原文网址:http://blog.csdn.net/nxh_love/article/details/11804841 本人对驱动可谓是一点不懂,鉴于公司目前高驱动的人手不够,所以我也只能两眼一抹黑硬 ...

  8. 如何实现异步调用WCF

    在面向服务的.NET开发中,我们经常要调用WCF服务加载数据,这时候,如果使用同步调用,会阻止UI,影响用户体验UE/UX,而且当服务器ping不通或者网速特别烂的情况下,这时候基本上是处于卡死状态, ...

  9. pyqt listwidget下面创建多张图片

    def Photosvisi(self): i=0 self.lists.setIconSize(QtCore.QSize(70,70))#设置显示图片大小 self.lists.setResizeM ...

  10. The kth great number(set)

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...