HDU 5441.Travel【2015 ACM/ICPC Asia Regional Changchun Online】【并查集】9月18
来源:互联网 发布:深圳市冰川网络 编辑:程序博客网 时间:2024/06/15 23:22
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 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 integersn,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 followingm 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.
Thenq lines follow. Each of them is a query consisting of an integer x where x is the time limit before Jack goes berserk.
For each test case, the first line consists of three integers
Each of the following
Then
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.
Note that
Sample Input
15 5 32 3 63341 5 157243 5 57054 3 123821 3 2172660001000013000
Sample Output
2612
给定n个节点,m个连通,q和询问。问不超过d的节点有多少两两连通。数据大了,所以说搜索是不行的。并查集,代码如下:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;int node[20010],ans[5005],num[20010];//num数组记录根节点为i的连通块的节点个数struct ss{ int fr,to,len;}c[100010];bool cmp1(ss x,ss y){ if(x.len<y.len) return true; else return false;}struct Ask{ int s,id;}ask[5010];bool cmp2(Ask x,Ask y){ if(x.s<y.s) return true; else return false;}int fi(int x){//寻找x节点的根节点 if(node[x]==x) return x; else return node[x]=fi(node[x]);}int main(){ int T,n,m,q; scanf("%d",&T); while(T--){ scanf("%d%d%d",&n,&m,&q); for(int i=1;i<=n;i++){//初始化 node[i]=i; num[i]=1; } for(int i=0;i<m;i++) scanf("%d%d%d",&c[i].fr,&c[i].to,&c[i].len); sort(c,c+m,cmp1); for(int i=0;i<q;i++){ scanf("%d",&ask[i].s); ask[i].id=i; } sort(ask,ask+q,cmp2); int j=0,cnt=0; for(int i=0;i<q;i++){ while(j<m&&c[j].len<=ask[i].s){ int x=fi(c[j].fr); int y=fi(c[j].to); if(x!=y){ node[y]=x;//将其连通,根节点为x cnt+=(num[x]*num[y]); num[x]+=num[y]; } j++; } ans[ask[i].id]=2*cnt; } for(int i=0;i<q;i++) printf("%d\n",ans[i]); } return 0;}
0 0
- HDU 5441.Travel【2015 ACM/ICPC Asia Regional Changchun Online】【并查集】9月18
- hdu 5441 Travel 并查集 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5441 Travel(离线 + 带权并查集)——2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5441 Travel(2015 ACM/ICPC Asia Regional Changchun Online)
- hdu 5438 Ponds 拓扑排序+并查集 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5438.Ponds【2015 ACM/ICPC Asia Regional Changchun Online】【DFS】9月13
- 2015 ACM/ICPC Asia Regional Changchun Online hdu 5438
- hdu 5438 Ponds 2015 ACM/ICPC Asia Regional Changchun Online
- 2015 ACM/ICPC Asia Regional Changchun Online
- 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5437.Alisha’s Party【2015 ACM/ICPC Asia Regional Changchun Online】【优先队列】9月14
- HDU 5444.Elven Postman【2015 ACM/ICPC Asia Regional Changchun Online】【二叉树建立与遍历】9月14
- 2013 ACM/ICPC Asia Regional Changchun Online hdu Theme Section
- 2012 ACM/ICPC Asia Regional Changchun Online
- 2013 ACM/ICPC Asia Regional Changchun Online
- 2013 ACM/ICPC Asia Regional Changchun Online
- 2013 ACM/ICPC Asia Regional Changchun Online
- 2015 ACM/ICPC Asia Regional Changchun Online(1002)
- Android Studio系列教程四--Gradle基础
- F-score
- MapReduce中的两表join几种方案简介
- (2.2.2.8)Java并发编程:线程池的使用
- oracle经验
- HDU 5441.Travel【2015 ACM/ICPC Asia Regional Changchun Online】【并查集】9月18
- Lua中的常用函数库汇总
- Read_only
- CommonJS规范和Nodejs模块机制
- Maven的安装、配置及使用入门
- qqreg
- Makefile选项CFLAGS,LDFLAGS,LIBS
- Spring事务管理只对出现运行期异常进行回滚
- AngularJS中ng-if/ng-switch时找不到scope下的定义的变量