5441 Travel

#题目

Travel

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


 

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

 

Source

2015 ACM/ICPC Asia Regional Changchun Online

 #分析

这不是道最短路问题。题意为给定n个城市和m条相连路径,以及每条路径的权,那个人在城市之间穿梭时不能忍受权大于某个值Q的路径。Q是一个序列,有q个数。要求依次输出在Q序列下的忍受权值时,那个人能够到达的城市对数,(a,b)与(b,a)为两个答案。

显然,如果(a,b)并且(b,c),则(a,c);如果(a,b),则(b,a)

所以我们用并查集来处理,节点表示城市,集合表示能够相互到达的城市

然后对序列q和边按权值大小进行sort,遍历一遍边,每到一个q的阈值就记录下来此时的ans,保存在答案数组中,由于打乱了q的顺序,需要在记录q时就记录编号和值,最后对ans进行输出即可。

#AC码

#include <iostream>
#include <stdio.h>
#include <algorithm>
#define Max_n 20010
using namespace std;
int par[Max_n];
int num[Max_n];     //记录一个集合元素总数
int an[Max_n];      //记录最终答案
struct edge   //边
{
    int u,v,cost;
    bool operator<(edge&a)
    {
        return cost<a.cost;
    }
}G[5*Max_n];
struct query   //查询序列,需要记录数值和编号
{
    int cost,cn;
    bool operator<(query&a)
    {
        return cost<a.cost;
    }
}Q[Max_n];
//初始化
void init()
{
    for(int i=1;i<=Max_n;i++)
    {
        par[i]=i;
        num[i]=1;
    }
}
//并查集查找根
int h_find(int x)
{
    return par[x]==x?x:par[x]=h_find(par[x]);
}
//合并集合
//此处由于传递过来的就是根,所以直接合并
void Union(int x,int y)
{
    par[y]=x;
    num[x]+=num[y];
}
int main()
{
    //freopen("5441.txt","r",stdin);
    int T;
    cin>>T;
    while(T--)
    {
        int ans=0;   //记录答案
        int m,n,q;
        cin>>n>>m>>q;
        init();
        for(int i=0;i<m;i++)
            scanf("%d%d%d",&G[i].u,&G[i].v,&G[i].cost);
        sort(G,G+m);
        for(int i=0;i<q;i++)
        {
            scanf("%d",&Q[i].cost);
            Q[i].cn=i;
        }
        sort(Q,Q+q);
        int j=0;  //j记录边检查
        for(int i=0;i<q;i++)
        {
            for(;j<m&&G[j].cost<=Q[i].cost;j++)
            {
                int h_u=h_find(G[j].u);
                int h_v=h_find(G[j].v);
                if(h_u==h_v)    //两点之间已经计算
                    continue;
                //先求ans再合并,因为合并时会增大num[x]
                //更新ans,增加的可到达数
                int x=h_u;
                int y=h_v;
                ans+=(num[x]+num[y])*(num[x]+num[y]-1)-num[x]*(num[x]-1)-num[y]*(num[y]-1);
                //否则合并这两个点所在集合,表示这两个点可以互通
                Union(h_u,h_v);
            }
            //编号为cn的查询值的答案
            an[Q[i].cn]=ans;
        }
        for(int i=0;i<q;i++)
            cout<<an[i]<<endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值