hdu5414(并查集+最小生成树)

ack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are nn cities and mm 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 xx minutes, how many pairs of city (a,b)(a,b) are there that Jack can travel from city aa to bb without going berserk?

Input

The first line contains one integer T,T≤5T,T≤5, which represents the number of test case. 

For each test case, the first line consists of three integers n,mn,m and qq where n≤20000,m≤100000,q≤5000n≤20000,m≤100000,q≤5000. The Undirected Kingdom has nn cities and mmbidirectional roads, and there are qq queries. 

Each of the following mm lines consists of three integers a,ba,b and dd where a,b∈{1,...,n}a,b∈{1,...,n} and d≤100000d≤100000. It takes Jack dd minutes to travel from city aa to city bb and vice versa. 

Then qq lines follow. Each of them is a query consisting of an integer xx where xx is the time limit before Jack goes berserk. 
 

Output

You should print qq lines for each test case. Each of them contains one integer as the number of pair of cities (a,b)(a,b) which Jack may travel from aa to bb within the time limit xx. 

Note that (a,b)(a,b) and (b,a)(b,a) are counted as different pairs and aa and bb 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

代码:
 

#include 
#include 
#include 
#include 

using namespace std;
typedef long long ll;

const int maxn = 20005;
const int maxm = 100005;
const int maxq = 5005;

struct Edge
{
    int u, v, w;
    Edge(int u = 0, int v = 0, int w = 0): u(u), v(v), w(w) {}
    bool operator < (const Edge& a) const
    {
        return w < a.w;
    }
} E[maxm], Q[maxq];

int N, M, K, F[maxn], S[maxn];
ll ans[maxn];

int find(int x)
{
    return x == F[x] ? x : F[x] = find(F[x]);
}

void init ()
{
    scanf("%d%d%d", &N, &M, &K);
    int u, v, w;
    for (int i = 0; i < M; i++)
    {
        scanf("%d%d%d", &u, &v, &w);
        E[i] = Edge(u, v, w);
    }
    for (int i = 0; i < K; i++)
    {
        scanf("%d", &w);
        Q[i] = Edge(i, 0, w);
    }
    sort(E, E + M);
    sort(Q, Q + K);
}

void solve ()
{
    for (int i = 1; i <= N; i++)
        F[i] = i, S[i] = 1;//初始化每个点的祖先是他自己,每个点的子孙都有一个。

    int p = 0;
    ll ret = 0;
    for (int i = 0; i < K; i++)
    {
        while (p < M && E[p].w <= Q[i].w)
        {
            int u = E[p].u, v = E[p].v, w = E[p].w;
            if (find(u) != find(v))
            {
                ret += 1LL * S[find(u)] * S[find(v)];
                S[find(v)] += S[find(u)];
                F[find(u)] = find(v);
            }
            p++;
        }

        ans[Q[i].u] = ret;
    }
    for (int i = 0; i < K; i++)
        printf("%lld\n", ans[i] * 2);
}

int main ()
{
    int cas;
    scanf("%d", &cas);
    while (cas--)
    {
        init();
        solve();
    }
    return 0;
}

 

你可能感兴趣的:(并查集)