本文主要是介绍hdu 5441 Travel(带权并查集),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441
Travel
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1586 Accepted Submission(s): 565
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 m bidirectional 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.
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 m bidirectional 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 .
br />Note that (a,b) and (b,a) are counted as different pairs and a and b must be different cities.
br />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
两个连通块连通后,增加的对数:(t1+t2)(t1+t2-1)-t1(t1-1)-t2(t2-1) = 2*t1*t2.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=2e4+10,N=1e5+10,M=5010;
struct node{int l,r,w;
}edge[N];
struct anode{int id,wid,val;
}ans[M];
int cnt,father[maxn],sum[maxn];
void addedge(int a,int b,int c){edge[cnt].l=a;edge[cnt].r=b;edge[cnt++].w=c;
}
int find(int x){if(x==father[x]) return x;return father[x]=find(father[x]);
}
bool cmp1(node a,node b){return a.w<b.w;
}
bool cmp2(anode a,anode b){return a.wid<b.wid;
}
bool cmp3(anode a,anode b){return a.id<b.id;
}
int main()
{//freopen("cin.txt","r",stdin);int t,n,m,q;cin>>t;while(t--){scanf($quot;%d%d%d",&n,&m,&q);for(int i=1;i<=n;i++){sum[i]=1;father[i]=i;}cnt=0;memset(ans,0,sizeof(ans));int a,b,c;for(int i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);addedge(a,b,c);}sort(edge,edge+cnt,cmp1);for(int i=0;i<q;i++){scanf("%d",&ans[i].wid);ans[i].id=i;ans[i].val=0;}sort(ans,ans+q,cmp2);int k=0,res=0;for(int i=0;i<q;i++){ans[i].val=ans[i-1].val;for(;k<cnt;k++){if(edge[k].w>ans[i].wid)break;int fx=find(edge[k].l),fy=find(edge[k].r);if(fx==fy) continue;father[fx]=fy;ans[i].val+=2*sum[fy]*sum[fx];sum[fy]+=sum[fx];}}sort(ans,ans+q,cmp3);for(int i=0;i<q;i++){printf("%d\n",ans[i].val);}}return 0;
}
这篇关于hdu 5441 Travel(带权并查集)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!