zoukankan      html  css  js  c++  java
  • Regional Changchun Online--Travel(最小生成树&& 并查集)

    Travel

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


    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 aren 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


    通过并查集计算新加入的,每次的计算公式为原来的s+合并后的两个集合数量乘积。。。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <iostream>
    #include <stdio.h>
    #include <math.h>
    #include <string>
    #include <queue>
    #include <string.h>
    #include <map>
    #include <set>
    #include <vector>
    #include <algorithm>
    #include <stdlib.h>
    using namespace std;
    #define eps 1e-8
    #define INF 20000000005
    #define rd(x) scanf("%d",&x)
    #define rd2(x,y) scanf("%d%d",&x,&y)
    #define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z);
    #define rdLL(x) scanf("%I64d",&x)
    #define MAXn 20005
    #define MAXe 100005
    #define QUEnum  5005
    struct Node{
       int sta,end,val;
    }node[MAXe];
    
    struct Que{
       int queval , num;
    }que[QUEnum];
    
    int sett[MAXn];
    
    int sett_find(int x)
    {
    	if(sett[x]<0)  return x;
    	return sett[x]=sett_find(sett[x]);
    }
    
    
    bool cmp1(Node a,Node b){
       return a.val<b.val;
    }
    
    bool cmp2(Que a,Que b){
      return a.queval<b.queval;
    }
    int main ()
    {
    	int Case;
    	rd(Case);
    	while(Case--)
    	{
    		memset(sett,-1,sizeof(sett));
    		int sum[MAXn];
    		int res[QUEnum];
    		for(int i=0;i<MAXn;i++)	sum[i]=1;
    		int n,e,quenum;
    		rd3(n,e,quenum);
    		
    		for(int i=0;i<e;i++)  
                rd3(node[i].sta,node[i].end,node[i].val);
    
    		sort(node,node+e,cmp1);  ///这里的排序不是+n 而是e  找了两个小时
    		for(int i=0;i<quenum;i++) {
                rd(que[i].queval);
    		    que[i].num=i;
    		}
    
    		sort(que,que+quenum,cmp2);
    		int j=0,s=0;   ///计数有多少能到达
    		for(int i=0 ; i<quenum ;i++)  ///不能在这里添加j<e,因为大于的还需要计算
    		{
    		    while( j<e && node[j].val <= que[i].queval  ){
                    int a=node[j].sta,b=node[j].end;
    
    			    int p = sett_find(a);
    			    int q = sett_find(b);
    			    if(p!=q){
                        s += (sum[q]*sum[p]); ////((sum[q]*(sum[q]+1))>>1) - ((sum[p]*(sum[p]+1))>>1) +
    				    sett[q] = p;
    				    sum[p]=sum[q]+sum[p]; ///p作为根节点
    			    }
    			    j++;
    		    }
    		    res[ que[i].num ] = (s<<1);
    		}
    		for(int i=0;i<quenum;i++)
                printf("%d
    ",res[i]);
    	}
    	return 0;
    }
    





  • 相关阅读:
    【2019-08-03】自卑和悲观是有区别的
    你现在不用写代码了吧?
    【2019-08-02】信任是一种能力
    【2019-08-01】给孩子一个渴望长大的榜样
    【一句日历】2019年8月
    【2019-07-31】一切皆有寓意
    【2019-07-30】原来努力会上瘾
    【2019-07-29】睡多了,会被宰的
    【2019-07-28】活到老,学到老
    【2019-07-27】习惯的力量很强大
  • 原文地址:https://www.cnblogs.com/zswbky/p/5431918.html
Copyright © 2011-2022 走看看