HDU 5441Travel

Travel

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


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

一道并查集的题目。

题意是  A去游玩B城市,B城市有n条双向公交车线路,每条线路花费c分钟、每次给出A所能忍受的在一条公交车线路上的花费时间、求在A的忍受程度之内,A能从一个地点到达另一个地点的点对。


首先,将询问进行排序,就只用一次遍历询问增加并入树中的点就行,每次并入时,将原来两颗树产生的点对减掉,再加上新的树产生的点对即可。


#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

struct edge
{
    int u,v,w;
} e[100020];

struct node
{
    int w,i;
}q[5050];

int d[20020],f[20020];

int find(int n)
{
    if(f[n]==n) return n;
    f[n]=find(f[n]);
    return f[n];
}

bool cmp(edge a,edge b)
{
    return a.w<b.w;
}

bool cmpp(edge a,edge b)
{
    if(a.u==b.u&&a.v==b.v) return a.w<b.w;
    if(a.u==b.u) return a.v<b.v;
    return a.u<b.u;
}

bool compare(node a,node b)
{
    return a.w<b.w;
}

int main()
{
    int n,m;
    int t,T;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&T);
        for(int i=0; i<m; i++)
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
        sort(e,e+m,cmpp);
        int len=1;
        for(int i=1; i<m; i++)
        {
            if(e[i].u==e[len-1].u&&e[i].v==e[len-1].v)
                continue ;
            e[len++]=e[i];
        }
        sort(e,e+len,cmp);
        for(int i=0;i<T;i++)
        scanf("%d",&q[i].w),q[i].i=i;
        sort(q,q+T,compare);
        int i=0;
        int ans=0;
        int an[5050];
        for(int i=1;i<=n;i++)f[i]=i,d[i]=1;
        for(int j=0;j<T;j++)
        {
            for(i;i<len&&e[i].w<=q[j].w;i++)
            {
                int f1,f2;
                f1=find(e[i].u);
                f2=find(e[i].v);
                if(f1!=f2)  //两颗树的根节点不同,则两棵树合并,原来树产生的点对要减掉
                {
                    ans-=d[f1]*(d[f1]-1);
                    ans-=d[f2]*(d[f2]-1);
                    f[f1]=f2;
                    d[f2]+=d[f1];
                    ans+=d[f2]*(d[f2]-1);
                }
            }
            an[q[j].i]=ans;  //记录每一次询问的答案 
        }
        for(i=0;i<T;i++)
        printf("%d\n",an[i]);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值