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
给你一张无向图,求有多少条路径使得路径上的花费小于L,这里路径上的花费是这样规定的,a、b两点之间的多条路径中的最长的边最小值!
用在这题中,是因为输入中的“询问部分”,有q个问,每个L可以有多少种不同路径。由于大的L必定会包含到小的L, 所以把所有问题都输入,再从大到小排序,再计算,可以减少很多计算量。
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;
int Father[10005];
int sum[10005];
int Count[10005];
struct node
{
int a,b;
int w;
bool operator<(const node &t)const
{
return w<t.w;
}
};
struct Node
{
int No;
int L;
bool operator<(const Node &t)const
{
return L<t.L;
}
};
int Find(int x)
{
if(x!=Father[x])
{
Father[x]=Find(Father[x]);
}
return Father[x];
}
int Union(int x,int y)
{
x=Find(x);
y=Find(y);
if(x==y)
{
return 0;
}
int ans=sum[x]*sum[y]; 路线数
sum[y]+=sum[x]; //更新增加的路线数,为下次做准备
Father[x]=y;
return ans;
}
node num[50005];
Node num2[10005];
int main()
{
int n,m,q;
while (scanf("%d%d%d",&n,&m,&q)!=EOF)
{
for (int i=0; i<=n; i++)
{
Father[i]=i;
sum[i]=1;
}
for (int i=0; i<m; i++)
{
scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].w);
}
for (int i=0; i<q; i++)
{
scanf("%d",&num2[i].L);
num2[i].No=i;
}
sort(num, num+m);
sort(num2, num2+q);
int ans=0;
int j=0;
for (int i=0; i<q; i++)
{
for (;j<m && num[j].w<=num2[i].L; j++)
{
ans+=Union(num[j].a, num[j].b);
}
Count[num2[i].No]=ans;
}
for (int i=0; i<q; i++)
{
printf("%d\n", Count[i]);
}
}
return 0;
}