(并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )

http://acm.hdu.edu.cn/showproblem.php?pid=5441

Travel

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2061    Accepted Submission(s): 711

Problem Description

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

Input

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

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

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

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

Output

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

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

题意:是有n个城市,m条边包含u v w;代表u到v的时间是w;

给q的时间x,求在x时间内Jack可以到达多少对城市;其中ab和ba是不同的;

1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000   ///可以到达35和53;
10000 ///2 5 3是可以相互到达的所以有6种;
13000 ///2 3 5 4是相通的有12种;

Source

2015 ACM/ICPC Asia Regional Changchun Online

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 100100

struct node
{
    int u, v, w, index;
} a[N], b[N];

int r[N], f[N], Q[N];

int cmp(node n1, node n2)
{
    return n1.w < n2.w;
}

int Find(int x)
{
    if(x!=f[x])
        f[x]=Find(f[x]);
    return f[x];
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, m, q, i;
        scanf("%d%d%d", &n, &m, &q);

        for(i=0; i<=n; i++)
        {
            f[i] = i;
            r[i] = 1;
        }

        for(i=0; i<m; i++)
            scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
        for(i=0; i<q; i++)
        {
            scanf("%d", &b[i].w);
            b[i].index = i;
        }

        sort(a, a+m, cmp);
        sort(b, b+q, cmp);

        int sum = 0, j = 0;
        for(i=0; i<q; i++)
        {
            while(j<m && a[j].w<=b[i].w)
            {
                int fu = Find(a[j].u);
                int fv = Find(a[j].v);

                if(fu!=fv)
                {
                    f[fu] = fv;
                    sum += r[fu]*r[fv]; ///两个集合的任意组合
                    r[fv] += r[fu];     ///r[i]代表i的根节点所包含的元素的个数
                }
                j++;
            }
            Q[b[i].index] = sum*2;  ///ab和ba是不一样的
        }

        for(i=0; i<q; i++)
            printf("%d\n", Q[i]);
    }
    return 0;
}

时间: 2024-10-18 16:11:30

(并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )的相关文章

2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 591    Accepted Submission(s): 329 Problem Description Elves are very peculiar creatures. As we all know, they can live for a very

2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v)满足两点之间每一条边都不超过x,问有多少对 思路:首先我想到的是dfs求出每个查询小于等于他的一个连通块,然后num*(num-1)就是答案,但是时间只有一秒,这个复杂度是5*1e8,超时了(亲身体验了) 然后我们想这个是离线的,我们可不可以由小到大来推,这样前面的贡献到后面就依然能用了,但是我们

2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 (拓扑排序+并查集)

Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and th

2015 ACM/ICPC Asia Regional Changchun Online Pro 1005 Travel

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 46    Accepted Submission(s): 20 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is trave

(线段树 区间查询)The Water Problem -- hdu -- 5443 (2015 ACM/ICPC Asia Regional Changchun Online)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 738    Accepted Submission(s): 591 Problem Description In Land waterless, w

HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online

[题目链接]:click here~~ [题目大意]: 题意:在一个无向图中有 p 个点, m 条边,每个点有一个值 vi .不断的删去度数小于2的点直到不能删为止.求新图中所有点个数为奇数的连通分量的点值的和. 1<p<10^4,1<m<10^5 [思路]删边考虑类似拓扑排序的写法,不过topsort是循环一遍1到n结点入度为0的结点,然后加入到队列中,这里只要改一下度数小于等于1,最后DFS 判断一下 挫挫的代码: /* * Problem: HDU No.5438 * Run

HDU 5444 Elven Postman (2015 ACM/ICPC Asia Regional Changchun Online)

[题目链接]:click here~~ [题目大意]: HDU 5444 题意:在最初为空的二叉树中不断的插入n个数.对于每个数,从根节点开始判断,如果当前节点为空,就插入当前节点,如果当前节点不为空,则小于当前节点的值,插入右子树,否则插入左子树. 接着q次询问,每次询问一个值在二叉树中从根节点开始的查找路径. 3 直接用二叉树模拟整个插入和询问的过程 代码: /* * Problem: HDU No.5444 * Running time: 0MS * Complier: G++ * Aut

Hdu 5442 Favorite Donut (2015 ACM/ICPC Asia Regional Changchun Online 最大最小表示法 + KMP)

题目链接: Hdu 5442 Favorite Donut 题目描述: 给出一个文本串,找出顺时针或者逆时针循环旋转后,字典序最大的那个字符串,字典序最大的字符串如果有多个,就输出下标最小的那个,如果顺时针和逆时针的起始下标相同,则输出顺时针. 解题思路: 看到题目感觉后缀数组可以搞,正准备犯傻被队友拦下了,听队友解释一番,果断丢锅给队友.赛后试了一下后缀数组果然麻烦的不要不要的(QWQ),还是最大最小表示法 + KMP来的干净利索. 最大表示法:对于一个长度为len文本串,经过循环旋转得到长度

hdu 5444 Elven Postman(根据先序遍历和中序遍历求后序遍历)2015 ACM/ICPC Asia Regional Changchun Online

很坑的一道题,读了半天才读懂题,手忙脚乱的写完(套上模板+修改模板),然后RE到死…… 题意: 题面上告诉了我们这是一棵二叉树,然后告诉了我们它的先序遍历,然后,没了……没了! 反复读题,终于在偶然间注意到了这一句——"Not only that, when numbering the rooms, they always number the room number from the east-most position to the west." 它告诉我们,东边的点总是比西边的点