zoukankan      html  css  js  c++  java
  • Travel

    题目链接: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): 4647    Accepted Submission(s): 1526


    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,T5 , which represents the number of test case.

    For each test case, the first line consists of three integers n,m and q where n20000,m100000,q5000 . The Undirected Kingdom has n cities and m bidirectional 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 d100000 . It takes Jack d minutes to travel from city a to city b and 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
     

    Source
     

    Recommend
    hujie   |   We have carefully selected several similar problems for you:  6343 6342 6341 6340 6339 
    Recommend
    hujie   |   We have carefully selected several similar problems for you:  6343 6342 6341 6340 6339 
    题目大意:输入t,代表t组样例,输入n,m,q.  分别代表有n个城市,m条路,q次询问。  问你每次询问满足该条件的边有多少条,(a,b)  (b,a)为不同的边
    思路:并查集的一道题,个人感觉挺难的,因为加了权值,大概思路就是把边的权值从小到大排序,再把询问的权值从小到大排序,这样遍历一遍边就可以了
    具体看代码
    #include<iostream>
    #include<string.h>
    #include<map>
    #include<cstdio>
    #include<cstring>
    #include<stdio.h>
    #include<cmath>
    #include<math.h>
    #include<algorithm>
    #include<set>
    #include<queue>
    typedef long long ll;
    using namespace std;
    const ll mod=1e9+7;
    const int maxn=1e5+10;
    const int maxk=5e3+10;
    const int maxx=1e4+10;
    const ll maxe=1000+10;
    #define INF 0x3f3f3f3f3f3f
    #define Lson l,mid,rt<<1
    #define Rson mid+1,r,rt<<1|1
    int deep[maxn],node[maxn];
    ll ans[maxk];
    int n,m,q;
    struct city
    {
        int be,en,va;
    }c[maxn];
    struct Qu
    {
        int id,va;
    }qu[maxk];
    void init()
    {
        for(int i=1;i<=n;i++)
        {
            node[i]=i;
            deep[i]=1;
        }
    }
    bool cmp(const city a,const city b)
    {
        return a.va<b.va;
    }
    bool cmp1(const Qu a,const Qu b)
    {
        return a.va<b.va;
    }
    int Find(int a)
    {
        return a==node[a]?a:node[a]=Find(node[a]);
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            memset(ans,0,sizeof(ans));
            scanf("%d%d%d",&n,&m,&q);
            init();
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d",&c[i].be,&c[i].en,&c[i].va);
            }
            sort(c,c+m,cmp);//按照边的权值进行排序,从小到大
            for(int i=0;i<q;i++)
            {
                scanf("%d",&qu[i].va);
                qu[i].id=i;
            }
            sort(qu,qu+q,cmp1);//按照询问的大小从小到大排序
            int cnt=0;
            ll tmp=0;
            for(int i=0;i<m;i++)//因为已经排好序,所以只需遍历一遍就行
            {
                int x=Find(c[i].be);
                int y=Find(c[i].en);
                while(c[i].va>qu[cnt].va&&cnt<q)//询问的权值小于当前边的权值
                {
                    ans[qu[cnt].id]=tmp;
                    cnt++;
                }
                if(x!=y)//代表两个点不在同一个集合中
                {
                    ll n1=deep[x],n2=deep[y];
                    tmp+=(n1+n2)*(n1+n2-1);//合并后的总对数
                    tmp-=n1*(n1-1)+n2*(n2-1);//减去原来的对数
                    node[x]=y;//根的变化
                    deep[y]+=deep[x];//节点数变化
                }
            }
            while(cnt<q)
            {
                ans[qu[cnt++].id]=tmp;//如果已经把所有的边都遍历完了,但是还有询问没有执行完,则所有的边都是该询问的=可以满足的边
            }
            for(int i=0;i<q;i++)
                printf("%lld
    ",ans[i]);
        }
        return 0;
    }
    当初的梦想实现了吗,事到如今只好放弃吗~
  • 相关阅读:
    对PostgreSQL的 seq scan , bitmap index scan 和 index scan 的进一步理解
    C# IEnumerable和IEnumerator的区别,如何实现
    老板运煤问题及解决方案
    快速排序和简单排序
    给定数组,查找最小的k个元素或最大的k个元素
    一列数字的规则如下;1,1,2,3,5,8,13,21,34........ 求第30位数字是多少,用递规和非递归两种方法算法实现
    C# 如何利用反射来加载程序集,并调用程序集中有关类的方法
    http 请求 header 应用分析
    templatemonster 10231 面朝大海 你听到什么?
    足球俱乐部 网站模板 附带源文件 字体
  • 原文地址:https://www.cnblogs.com/caijiaming/p/9419168.html
Copyright © 2011-2022 走看看