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,T5, which represents the number of test case.

For each test case, the first line consists of three integers n,m and q where n20000,m100000,q5000. 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 d100000. 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
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
原创粉丝点击