HDU 5441.Travel【2015 ACM/ICPC Asia Regional Changchun Online】【并查集】9月18
2015-09-18 11:52
351 查看
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 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 band
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
Sample Output
给定n个节点,m个连通,q和询问。问不超过d的节点有多少两两连通。数据大了,所以说搜索是不行的。并查集,代码如下:
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 band
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
给定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; }
相关文章推荐
- 查找算法的实现 顺序和折半
- Ubuntu中Git服务器搭建
- VC下动态库DLL、静态库LIB的编写和使用方法
- STM32 IAP 在线升级详解
- Apache 负载均衡 session
- IOS单例
- ASP.NET窗体身份验证详解
- apache URL 区别大小写
- linux安装java
- 死亡通知与进程异常退出;ANR与进程重启后状态跟之前不对
- 将公用文件夹从Exchange2010迁移到Exchange 2013
- 你在人际交往方面吃过哪些亏
- 第三次作业
- [C#基础] 数据类型
- Android实现Service获取当前位置(GPS+基站)的方法
- Spring事务管理只对出现运行期异常进行回滚
- ubuntu完美搭建git服务器
- 最近常用到的Linux命令
- C++ typename 总结
- Item 30:理解inline函数的里里外外 Effective C++笔记