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
本题的意思是给你一个无向带权图,每次以一个L进行询问,问有多少对点使得从U到V的花费(指从U到V的路径中最长的一条边)小于L。
我们先保存下来每一次询问,然后对询问从小到大进行排序,然后对边排序,初始时每个集合就只有自己本身一个元素,然后从小到大一条边一条边往外拿(要保证小于当前询问值),然后将这条边的两个点对应的集合并在一起,此时加上这条边的点的个数为两个集合元素个数的乘积,然后依次累加。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
#define maxn 55000
int n,m,q;
int pre[maxn];
int ans[maxn];
int nu[maxn];//记录每个集合有多少点,存在下标根里
struct Edge
{
int u,v,w;
}edge[maxn];
struct query
{
int num,id;
}que[maxn];
bool cmp1(Edge a,Edge b)
{
return a.w<b.w;
}
bool cmp(query a,query b)
{
if(a.num == b.num)
{
return a.id<b.id;
}
return a.num<b.num;
}
int findroot(int num)
{
while(pre[num] != num)
{
num = pre[num];
}
return pre[num];
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&q))
{
for(int i = 1; i <= n; i++)
{
pre[i] = i;
nu[i] = 1;
}
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
}
sort(edge+1,edge+m+1,cmp1);
for(int i = 1; i <= q; i++)
{
que[i].id = i;
scanf("%d",&que[i].num);
}
sort(que+1,que+q+1,cmp);
int p = 1;
int sum = 0;
for(int i = 1; i <= q; i++)
{
int temp = que[i].num;
while(p <= m && edge[p].w <= temp)
{
int roota = findroot(edge[p].u);
int rootb = findroot(edge[p].v);
if(roota != rootb)
{
sum += nu[rootb]*nu[roota];
nu[rootb] += nu[roota];
//nu[roota] = 0;
pre[roota] = rootb;
}
p++;
}
ans[que[i].id] = sum;
}
for(int i = 1; i <= q; i++)
{
printf("%d\n",ans[i]);
}
}
}