【HDU 5441】Travel

来源:互联网 发布:阿里巴巴网络推广 编辑:程序博客网 时间:2024/05/22 10:51


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,T5T,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 n20000,m100000,q5000n≤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 d100000d≤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
15 5 32 3 63341 5 157243 5 57054 3 123821 3 2172660001000013000
Sample Output
2612



离线并查集,每走过一个城市,若其并未在并查集中,便将其代表的点加入并查集,对于(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;    }}


原创粉丝点击