Portal
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1930 Accepted Submission(s): 961
Problem Description
ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.
Input
There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).
Output
Output the answer to each query on a separate line.
Sample Input
10 10 10 7 2 1 6 8 3 4 5 8 5 8 2 2 8 9 6 4 5 2 1 5 8 10 5 7 3 7 7 8 8 10 6 1 5 9 1 8 2 7 6
Sample Output
36 13 1 13 36 1 36 2 16 13
题意:在给定的权值下,满足点对的数目的情况。所谓的要求就是,给出两点,之间有很多路径,这个点对的最小花费就是
众多路径中,最小花费的路径,路径长度就是路径上最长边的长度。
解析:很容易想到离线化输出(离线化不是离散化,离线化是指将所有数据获得,然后一次性输出结果,就像不是在线解决的一样。)认真观察可以看出,两点可以连线的条件就是之前的边都小于给出的定值。于是,利用最小生成树的算法构造边,如果两个集合a,b因为一条边的加入,那么增加的点对数位sum[a]*sum[b](这点自行理解,画图就可以看出来的),构造边利用到最小
生成树,则需要将对边排序。
#include<bits/stdc++.h> using namespace std; const int maxn=50005; int n,m,q; int f[maxn],sum[maxn]; struct node { int v,u,l; }e[maxn]; struct Q { int l,id,ans; }que[10005]; bool cmp(node a,node b) { return a.l<b.l; } bool cmp1(Q a,Q b) { return a.l<b.l; } bool cmp2(Q a,Q b) { return a.id<b.id; } void init() { for(int i=1; i<=n; i++) { f[i]=i; sum[i]=1; } } int Find(int x) { while(x!=f[x]) x=f[x]; return f[x]; } int join(int a,int b) { int fa=Find(a); int fb=Find(b); if(fa!=fb) { f[fb]=fa; int t=sum[fa]*sum[fb]; sum[fa]+=sum[fb]; return t; } return 0; } int main() { while(~scanf("%d%d%d",&n,&m,&q)) { init(); que[0].ans=0; for(int i=1; i<=m; i++)scanf("%d%d%d",&e[i].v,&e[i].u,&e[i].l); for(int i=1; i<=q; i++) { scanf("%d",&que[i].l); que[i].ans=0; que[i].id=i; } sort(e+1,e+m+1,cmp); sort(que+1,que+q+1,cmp1); int cnt=1; for(int i=1; i<=q; i++) { que[i].ans=que[i-1].ans; while(e[cnt].l<=que[i].l&&cnt<=m) { que[i].ans+=join(e[cnt].v,e[cnt].u); cnt++; } } sort(que+1,que+q+1,cmp2); for(int i=1; i<=q; i++) printf("%d\n",que[i].ans); } return 0; }