HDOJ 题目3938 Portal(带权值并查集+离线)



Portal

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


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
 

Source
 

Recommend
We have carefully selected several similar problems for you:   3935  3937  3931  3932  3933 
 ac代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int rank[10010],pre[10010];
int n,m,q;
struct s
{
	int x,y,w;
}b[50020];
struct p
{
	int l,id;
}qu[50010];
int cmp1(const void *a,const void *b)
{
	return (*(struct s *)a).w-(*(struct s *)b).w;
}
int cmp2(const void *a,const void *b)
{
	return (*(struct p *)a).l-(*(struct p *)b).l;
}
 void inti(int n)
 {
	int i;
	for(i=0;i<=n;i++)
	{
		rank[i]=1;
		pre[i]=i;
	}
 }
 int find(int x)
 {
	int i,r=x,j;
	while(r!=pre[r])
		r=pre[r];
	i=x;
	while(i!=r)
	{
		j=pre[i];
		pre[i]=r;
		i=j;
	}
	return r;
 }
 int merge(int a,int b)
 {
	int fa,fb,t=0;
	fa=find(a);
	fb=find(b);
	if(fa==fb)
		return 0;
	
		t=rank[fa]*rank[fb];
		rank[fa]+=rank[fb];
		pre[fb]=fa;
	return t;
 }
int main() 
{
	while(scanf("%d%d%d",&n,&m,&q)!=EOF)
	{
		int i,j,c,ans[10010];
		inti(n);
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&b[i].x,&b[i].y,&b[i].w);
		}
		for(i=0;i<q;i++)
		{
			scanf("%d",&qu[i].l);
			qu[i].id=i;
		}
		qsort(b,m,sizeof(b[0]),cmp1);
		qsort(qu,q,sizeof(qu[0]),cmp2);
		j=0,c=0;
		for(i=0;i<q;i++)
		{
			while(j<m&&b[j].w<=qu[i].l)
			{
				c+=merge(b[j].x,b[j].y);
				j++;
			}	
			ans[qu[i].id]=c;
		}
		for(i=0;i<q;i++)
		{
			printf("%d\n",ans[i]);
		}
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值