HDU 3938 Portal(离线+Kruskal+并查集)

链接:

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

题目:

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

分析与总结:

做这题学到了什么是“离线算法”的概念。所谓“离线”,就是把所有的数据都输入之后再计算,“在线”就是边输入边计算。

用在这题中,是因为输入中的“询问部分”,有Q 个问,每个L可以有多少种不同路径。由于大的L必定会包含到小的L, 所以把所有问题都输入,再从大到小排序,再计算,可以减少很多计算量。

本栏目更多精彩内容:http://www.bianceng.cn/Programming/sjjg/

这题还需要用到的是并查集中的“权值”, 用rank数组表示,也就是某个棵树k有rank【k】个结点。同一个树之间的点都是连通的,任何点都可以通往其它的任意点, 那么当两颗树合并成一棵树时, 将会增加rank[a]*rank[b]条路径。

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
#define N 10005
int f[N], rank[N], ans[N], n, m, Q;  

struct Edge{
    int u, v, val;
    friend bool operator < (const Edge&a,const Edge&b){
        return a.val < b.val;
    }
}arr[N*5];  

struct Query{
    int id, L;
    friend bool operator<(const Query&a,const Query&b){
        return a.L<b.L;
    }
}q[N];  

void init(){
    for(int i=0; i<=n; ++i)
        f[i]=i, rank[i]=1;
}
int find(int x){
    int i, j=x;
    while(j!=f[j]) j=f[j];
    while(x!=j){ i=f[x]; f[x]=j; x=i; }
    return j;
}
int Union(int x,int y){
    int a=find(x), b=find(y);
    if(a==b) return 0;
    int t=rank[a]*rank[b];
    rank[a] += rank[b];
    f[b] = a;
    return t;
}  

int main(){
    while(~scanf("%d%d%d",&n,&m,&Q)){  

        for(int i=0; i<m; ++i)
            scanf("%d%d%d",&arr[i].u,&arr[i].v,&arr[i].val);
        for(int i=0; i<Q; ++i)
            scanf("%d",&q[i].L), q[i].id=i;  

        sort(arr,arr+m);
        sort(q,q+Q);  

        int cnt=0, j=0;
        init();
        for(int i=0; i<Q; ++i){
            while(j<m && arr[j].val<=q[i].L){
                cnt += Union(arr[j].u, arr[j].v);
                ++j;
            }
            ans[q[i].id] = cnt;
        }  

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索rank
, 这题怎么做啊
, hdu1716 排序 oj
, 输入
, 10
, and
, of
The
kruskal 并查集、kruskal算法 并查集、hdu并查集、并查集、带权并查集,以便于您获取更多的相关知识。

时间: 2024-12-01 08:51:11

HDU 3938 Portal(离线+Kruskal+并查集)的相关文章

HDU 2473 Junk-Mail Filter 【并查集+设立虚父节点(马甲)】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2473 原题: Problem Description Recognizing junk mails is a tough task. The method used here consists of two steps: 1) Extract the common characteristics from the incoming email. 2) Use a filter matching t

HDU 3461:Code Lock(并查集+二分求幂)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=3461 原题: Problem Description A lock you use has a code system to be opened instead of a key. The lock contains a sequence of wheels. Each wheel has the 26 letters of the English alphabet 'a' through 'z',

并查集专题【完结】

个人整理 并查集 [poj] 第一题 poj 1182 食物链 点击打开链接poj 1182 思路: 带权并查集 分析: 1 典型的带权并查集的题目 2 如果x和y是同类的关系认为是0,如果是x吃y那么关系认为是1,那么x和y的关系为2就说明y吃x 3 那么如果x和y同类则rank[x] == rank[y] , 如果x吃y那么rank[x] == (rank[y]+1)%3 , 注意mod3的使用 点击查看代码 第二题 poj 1308 Is It A Tree? 点击打开链接 poj 130

图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 B(G).其中 T(G)是遍历图时所经过的边的集合,B(G) 是遍历图时未经过的边的集合.显然,G1(V, T) 是图 G 的极小连通子图,即子图G1 是连通图 G 的生成树. 深度优先生成森林   右边的是深度优先生成森林: 连通图的生成树不一定是唯一的,不同的遍历图的方法得到不同的生成树;从不

HDU 3038 How Many Answers Are Wrong? :带权并查集

链接: http://acm.hdu.edu.cn/showproblem.php?pid=3038 题目: Problem Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. T

HDU 2818 Building Block, poj 1988 Cube Stacking:带权并查集

链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=2818 POJ:  http://poj.org/problem?id=1988 题目: Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbered 1...N.Initially, there are N piles, and each pile contai

HDU 1811 Rank of Tetris(拓扑排序+并查集)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1811 题目: Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜,定时更新,名堂要比福布斯富豪榜还响.关于如何排名,这个不用说都知道是根据Rating从高到低来排,如果两个人具有相同的Rating,那就按这几个人的R

HDU 1829 A Bug&#039;s Life(基础种类并查集)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1829 原题: Problem Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with b

HDU 3047 Zjnu Stadium:带权并查集

链接: http://acm.hdu.edu.cn/showproblem.php?pid=3047 题目: Problem Description In 12th Zhejiang College Students Games 2007, there was a new stadium built in Zhejiang Normal University. It was a modern stadium which could hold thousands of people. The au