【HDU 5441】Travel

 

 

Jack 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

 

 

 

 

离线并查集,每走过一个城市,若其并未在并查集中,便将其代表的点加入并查集,对于(u, v)

 

如果u和v没有连通,假设u所在连通分量的点个数是n1,v所在的连通分量的点个数是n2

对于第一个连通分量,合并后这个连通分量就不存在了,ans会减少n1*(n1-1)对点

对于第二个连通分量,合并后这个连通分量就不存在了,ans会减少n2*(n2-1)对点

合并后,多了一个合并的连通分量,ans会增加(n1+n2)*(n1+n2-1)对点

之后根据权值大小更新答案

AC代码:

 

#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
struct N1{
    int u, v, c;
    bool operator< (const N1 b) const{
        return c < b.c;
    }
}n1[100005];
struct N2{
    int x, id;
    bool operator< (const N2 b) const{
        return x < b.x;
    }
}n2[100005];
int n, m, q, par[100005], rank_[100005];
ll ans[100005];
int find_(int x)
{
    if(x == par[x])
        return x;
    else
        return par[x] = find_(par[x]);
}
inline void init()
{
    for(int i = 1; i <= n; i++)
    {
        par[i] = i;
        rank_[i] = 1;
    }
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d%d", &n, &m, &q);
        init();
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d%d", &n1[i].u, &n1[i].v, &n1[i].c);
        }
        for(int i = 1; i <= q; i++)
        {
            scanf("%d", &n2[i].x);
            n2[i].id = i;
        }
        sort(n1 + 1, n1 + 1 + m);
        sort(n2 + 1, n2 + 1 + q);
        ll s = 0;
        int i = 1;
        for(int in = 1; in <= q; in++)
        {
            while(i <= m && n1[i].c <= n2[in].x)
            {
                int f1 = find_(n1[i].u);
                int f2 = find_(n1[i].v);
                if(f1 != f2)
                {
                    s = s - rank_[f1] * (rank_[f1] - 1) - rank_[f2] * (rank_[f2] - 1) + (rank_[f1] + rank_[f2]) * (rank_[f1] + rank_[f2] - 1);
                    par[f1] = f2;
                    rank_[f2] += rank_[f1];
                }
                i++;
            }
            ans[n2[in].id] = s;
        }
        for(int i = 1; i <= q; i++)
            cout<<ans[i]<<endl;
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值