hdu 5441 Travel 离线带权并查集

转自:http://www.cnblogs.com/qscqesze/p/4805266.html

题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5441
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,T≤5, which represents the number of test case.

For each test case, the first line consists of three integers n,m and q where n≤20000,m≤100000,q≤5000. 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 d≤100000. 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

HINT

题意:

给你一个图,无向边。

有5000个询问

每次询问,问你有多少对城市之间的最长度不超过D

题解:

离线处理,离线之后用并查集维护就好了

按照边权排序,每次就把小于d还没有添加过的边连进去就好了

每次都由并查集维护 ,带权并查集

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>

using namespace std;
const int maxn = 2e4 + 500;
int set[maxn] , n , m , q , ans[maxn] , number[maxn] , cot  = 0;

struct Edge
{
    int u , v , w;
    Edge(int u ,int v ,int w) : u(u) , v(v) , w(w) {}
    friend bool operator < (const Edge & x,const Edge & y)
    {
        return x.w < y.w;
    }
};

struct Query
{
    int val , idx;
    friend bool operator < (const Query & x,const Query & y)
    {
        return x.val < y.val;
    }
};

vector<Edge>e;
Query qq[6000];

int find_set(int u)
{
    return set[u] ^ u ? set[u] = find_set(set[u]) : u;
}

int union_set(int u,int v)
{
    int p1 = find_set(u);
    int p2 = find_set(v);
    if(p1 != p2)
    {
        int x = number[p1];
        int y = number[p2];
        cot -= x*(x-1);
        cot -= y*(y-1);
        number[p2] += number[p1];
        cot += (number[p2])*(number[p2]-1);
        number[p1] = 0;
        set[p1] = p2;
    }
}

void initiation()
{
    scanf("%d%d%d",&n,&m,&q);
    for(int i = 1 ; i <= n ; ++ i)
    {
        set[i] = i;
        number[i] = 1;
    }
    for(int i = 1 ; i <= m ; ++ i)
    {
        int u , v , w;
        scanf("%d%d%d",&u,&v,&w);
        e.push_back(Edge(u,v,w));
    }
    sort(e.begin(),e.end());
    for(int i = 1 ; i <= q ; ++ i)
    {
        int d;
        scanf("%d",&d);
        qq[i].val = d , qq[i].idx = i;
    }
    sort( qq + 1 , qq + 1 + q);
}

void solve()
{
    int ptr = 0;
    for(int i = 1 ; i <= q; ++ i)
    {
        while(ptr < m && e[ptr].w <= qq[i].val)
        {
            union_set(e[ptr].u,e[ptr].v);
            ptr++;
        }
        ans[qq[i].idx] = cot;
    }
    for(int i = 1 ; i <= q; ++ i) printf("%d\n",ans[i]);
}

void clear_all()
{
    e.clear();
    cot = 0;
}

int main(int argc,char *argv[])
{
    int Case;
    scanf("%d",&Case);
    while(Case--)
    {
        initiation();
        solve();
        clear_all();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值