zoukankan      html  css  js  c++  java
  • HDU 3938 Portal (离线并查集,此题思路很强!!!,得到所谓的距离很巧妙)

    Portal

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


    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 
     
    分析:
    题目意思:
    给你一个图,带权,问你两点间的距离小于等于L的点对的数量
     
    两点间距离的定义:
    两点间所有路径中,最长的边中的最小的边(很多路中的最长的哪些边中的最小值) 注意理解
    先将L升序排序,将边按照输入的权值升序排序
    原因:比如L1<L2,现在得到L1的答案(两点间的距离小于等于L的点对的数量),现在要知道L2的答案,L2的答案肯定是包含L1的答案的
    是L1的答案加上某个值(该值必须是不与前面值有重叠的部分),所以我们先得到小一点的L的答案,然后通过小一点的L的答案求大一点的
    L的答案,然后通过L输入的顺序按照顺序输出,这个就是所谓的离线化

    然后开始从第一个最小的L开始跑,此时边也是从最小权值的边开始跑的,此代码最神奇的地方在于得到两点间所谓的距离:
    原因:因为一开始是按照权值升序排序的,那么此时得到的边是集合中所有边中最大的那个,也就是所有路中最长边的最小值,因为你一开始两个
    点是没有连通的,一旦通过此边去连通,那么此时能走的就只有这一条路,你肯定是要经过的,且此边是目前所有边中最长的那条边,所以此边就是
    两点间所谓的距离!!!
     
    本人很菜,可能没有说清楚,大家可以看看代码,主要是for循环和内部的while循环部分
    很难理解,也很奇妙
     
    code:
    #include<queue>
    #include<set>
    #include<cstdio>
    #include <iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define N 10005
    #define M 50005
    int pa[N];
    int sum[N];
    int n,m;
    struct node1
    {
        int id,ans,l;
    }query[N];
    struct node2
    {
        int u,v,w;
    }edge[M];
    bool cmp1(node2 a,node2 b)
    {
        return a.w<b.w;
    }
    bool cmp2(node1 a,node1 b)
    {
        return a.id<b.id;
    }
    bool cmp3(node1 a,node1 b)
    {
        return a.l<b.l;
    }
    void init()
    {
        for(int i=1;i<=n;i++)
        {
            pa[i]=i;
            sum[i]=1;//集合内点的数量
        }
    }
    int find_set(int x)
    {
        if(x!=pa[x])
            pa[x]=find_set(pa[x]);
        return pa[x];
    }
    int union_set(int x,int y)
    {
        int fx=find_set(x);
        int fy=find_set(y);
        int temp=0;
        if(fx!=fy)
        {
            pa[fx]=fy;
            temp=sum[fx]*sum[fy];//没有重叠部分的答案就是俩个集合点数量的乘积
            sum[fy]+=sum[fx];//合并之后大集合点的数量等于两个小集合点数量之和
        }
        return temp;//当两点是连通的时候,返回的是0,说明当前L得到的答案就是前面小一点的L的答案
    }
    int main()
    {
        int q;
        while(~scanf("%d %d %d",&n,&m,&q))
        {
            init();
            for(int i=0;i<m;i++)
            {
                scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w);
            }
            for(int i=0;i<q;i++)
            {
                scanf("%d",&query[i].l);
                query[i].id=i;//按照L输入顺序输出结果的保证
                query[i].ans=0;
            }
            sort(edge,edge+m,cmp1);//边按照权值排序 升序
            sort(query,query+q,cmp3);//问询按照L排序 升序
            int cnt=0;
            for(int i=0;i<q;i++)
            {
                while(edge[cnt].w<=query[i].l&&cnt<m)//当前的W就是两点间定义的距离 很神奇!!!
                {
                    int x=edge[cnt].u;
                    int y=edge[cnt].v;
    
                    query[i].ans+=union_set(x,y);
                    cnt++;//cnt一直在++,保证了当前大L算出来的值和前面小一点的L算出来的值是没有重叠部分的
                }
                if(i>0)
                    query[i].ans+=query[i-1].ans;//此时大L的值是前面小L的值加上没有重叠部分的值,前面while循环得到的值是没有重叠部分的值
            }
            sort(query,query+q,cmp2);//按照L的输入顺序输出结果
            for(int i=0;i<q;i++)
            {
                printf("%d
    ",query[i].ans);
            }
        }
        return 0;
    }
    /*
    题目意思:
    给你一个图,带权,问你两点间的距离小于等于L的点对的数量
    
    两点间距离的定义:
    两点间所有路径中,最长的边中的最小的边(很多路中的最长的哪些边中的最小值) 注意理解
    
    先将L升序排序,将边按照输入的权值升序排序
    原因:比如L1<L2,现在得到L1的答案(两点间的距离小于等于L的点对的数量),现在要知道L2的答案,L2的答案肯定是包含L1的答案的
    是L1的答案加上某个值(该值必须是不与前面值有重叠的部分),所以我们先得到小一点的L的答案,然后通过小一点的L的答案求大一点的
    L的答案,然后通过L输入的顺序按照顺序输出,这个就是所谓的离线化
    
    
    然后开始从第一个最小的L开始跑,此时边也是从最小权值的边开始跑的,此代码最神奇的地方在于得到两点间所谓的距离:
    原因:因为一开始是按照权值升序排序的,那么此时得到的边是集合中所有边中最大的那个,也就是所有路中最长边的最小值,因为你一开始两个
    点是没有连通的,一旦通过此边去连通,那么此时能走的就只有这一条路,你肯定是要经过的,且此边是目前所有边中最长的那条边,所以此边就是
    两点间所谓的距离!!!
    
    */
     
  • 相关阅读:
    JS 禁止刷新和右键
    报错 避免重复
    CSS 总结
    CSS BUG 总结
    安装 mrtg
    人人网 网站接入总结
    PHPcms 把盛大登陆换成人人网登陆
    HTML 相同name 传递一个数组
    file_get_contents无法请求https连接的解决方法
    现货黄金白银上阻力位和压力位的确定和应用
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9464049.html
Copyright © 2011-2022 走看看