HDU 3938

Portal

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1796    Accepted Submission(s): 884


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
 

Source
 

Recommend
We have carefully selected several similar problems for you:   3935  3937  3931  3932  3933 

题意:从u到v两点之间建立路径花费精力是其中路段cost最多的那个,Q次询问给你总精力为L,求出有多少种类的路径被建成

将询问按照从小到大排序,然后将每条边按照权值从小到大排序,对于当前加入树边(u,v,w),对应ans【w】=sum【u】*sum【v】,这里sum【u】表示的就是点u所在联通块点的个数。


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define N 110000
struct node {
    int u,v;
    int w;
}edge[N*5];
struct Node {
    int pos;
    int val;
}a[N];
int father[N];
int num[N], ans[N];
int n, m, q;
bool cmp1(node a, node b)
{
    return a.w < b.w;
}
bool cmp2(Node a, Node b)
{
    return a.val < b.val;
}
int findfather(int x)
{
    int a = x;
    while(x != father[x])
        x = father[x];
    while(a != father[a]) {
        int t = a;
        a = father[a];
        father[t] = x;
    }
    return x;
}
int Union(int a, int b)
{
    int faA = findfather(a);
    int faB = findfather(b);
    if(faA == faB)  //如果合并后属于同一子集说明已经被联通两点已经被计算在内
        return 0;
    int t = num[faA] * num[faB];
    father[faB] = faA;
    num[faA] += num[faB];
    num[faB] = 0;//已经合并在一个集合里防止重复
    
    return t;
}
int main()
{
    while(scanf("%d%d%d", &n, &m, &q) != EOF) {
        for(int i = 0; i <= n; i++) {
            father[i] = i;
            num[i] = 1;
        }
        for(int i = 0; i < m; i++)
            scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);
        sort(edge, edge + m, cmp1); //边长从小到大排序
        for(int i = 0; i < q; i++) {
            scanf("%d", &a[i].val);
            a[i].pos = i;
        }
        sort(a, a+q, cmp2);
        int t = 0;
        int j = 0;
        for(int i = 0; i < q; i++) {
            while(j < m && edge[j].w <= a[i].val) {
                t += Union(edge[j].u, edge[j].v);
                j++;
            }
            ans[a[i].pos] = t;
        }
        for(int i = 0; i < q; i++)
            printf("%d\n", ans[i]);

    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值