Hdu 3938 Portal【离线+并查集+思维】

Portal

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1792    Accepted Submission(s): 883


Problem Description
ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.
 

Input
There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).
 

Output
Output the answer to each query on a separate line.
 

Sample Input
 
   
10 10 10 7 2 1 6 8 3 4 5 8 5 8 2 2 8 9 6 4 5 2 1 5 8 10 5 7 3 7 7 8 8 10 6 1 5 9 1 8 2 7 6
 

Sample Output
 
   
36 13 1 13 36 1 36 2 16 13

题目大意:


给出N个点,M条无向边,以及Q个查询,每个查询询问两点间所有路径上的最大值最小值为w,并且w<=Qi的点对数。


思路:

①我们首先思考,询问两点间所有路径的最大值最小,其实就是在询问,从点u到点v最短路径上的最大值。那么希望两点间最短路径最短,其实问题就相当于求一颗MST。

对应两点间最短路的最大值,就是联通点u和点v的最后一条边的权值。


②那么我们离线处理问题,将询问按照从小到大排序,然后将每条边按照权值从小到大排序,对于当前加入树边(u,v,w),对应ans【w】=sum【u】*sum【v】,这里sum【u】表示的就是点u所在联通块点的个数。


③注意数据范围,过程维护一下即可。


Ac代码:

#include
#include
#include
#include
using namespace std;
int f[1050000];
int sum[1050000];
long long int output[1050000];
struct node
{
    int x,y,w;
}a[1050000];
struct node2
{
    int val,pos;
}q[1050000];
int cmp(node a,node b)
{
    return a.w










你可能感兴趣的:(最小生成树及其拓展,思维)