hdu Travel

来源:互联网 发布:如何使用淘宝积分 编辑:程序博客网 时间:2024/06/08 17:00

Travel

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


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 mbidirectional 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 band 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
15 5 32 3 63341 5 157243 5 57054 3 123821 3 2172660001000013000
 

Sample Output
2612
 
这道题的大概意思:输入T表示T组测试数据,输入n m k 表示有n个点,m条边,k次询问。接下来输入m条边,a b c ,表示a到b权值为c。两点之间最多一条边。然后输入k个询问每次询问为一个数s,问你有多少个点集(a,b)使得经过的边的权值都不大于s,注意(a,b)和(b,a)是不同点集。
思路:
一看到这道题的数据,我第一感觉是用线段树,因为涉及到信息的查询量非常大,但是想了想又觉得用线段树不合适,最后觉得用并查集来解。首先这里要了解在一个有n个点的连通图中,假如每条边都是小于s的,那么这个连通图中有n*(n-1)条符合要求的点集。这样我们就可以先按边权值的大小排序一遍,然后不断地将边加入集合中,当我们加入第x条边时,前面的边的权值都是小于第x条边的,只要将它们构成的每个联通图的点的个数统计好,就能得出经过边的权值都不大于第x条边的权值的点集了。
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
struct edg{
    int x,y;
    int value;
};
edg edgs[100005];//保存每一条边。
bool cmp(edg a, edg b)
{
    return a.value < b.value;
}
int n,m,k;
long long ans[100005];//保存答案
long long sum;
int num[20005][2];
int fnd(int s)//判断s点所属的集合
{
    if ( num[s][0] == s ) return s;
    else return fnd(num[s][0]);
}
void gb(int a,int b)//这一步是必须的不然会超时,降低查找所属集合时的时间复杂度
{
    if ( b != num[b][0] ) gb(a,num[b][0]);
    num[b][0] = a;
}
void join()
{
    int a,b;
    for(int i = 0; i < m; i ++ )
    {
        a = fnd(edgs[i].x);
        b = fnd(edgs[i].y);
        if ( a != b )//如果这条边的两个点不属于同一个连通图
        {
            sum -= num[b][1]*(num[b][1]-1);
            sum -= num[a][1]*(num[a][1]-1);
            sum += (num[a][1]+num[b][1])*(num[a][1]+num[b][1]-1);
            num[a][1] += num[b][1];
            num[b][0] = a;
            gb(a,edgs[i].y);
        }
        ans[edgs[i].value] = sum;//将答案保存起来
    }
    int s = 0;
    for(int i = 0; i < 100005; i ++ )
    {
        if ( ans[i] > s ) s = ans[i];
        ans[i] = s;
    }
}
int main()
{
    int T,a,b,value,w;
    scanf("%d",&T);
    while ( T-- )
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i = 0; i < m; i ++ )
        {
            scanf("%d%d%d",&edgs[i].x,&edgs[i].y,&edgs[i].value);
        }
        sort(edgs,edgs+m,cmp);
        memset(ans,0,sizeof(ans));
        memset(num,0,sizeof(num));
        for(int i = 0; i < n; i ++ )
        {
            num[i][0] = i;
            num[i][1] = 1;
        }


        sum = 0;
        join();
        for(int i = 0; i < k; i ++ )
        {
            scanf("%d",&w);
            if ( w >= 100005 )
                printf("%lld\n",ans[100003] );
            else printf("%lld\n",ans[w]);
        }
    }
    return 0;
}


0 0