hdu 3938 Portal

 http://acm.hdu.edu.cn/showproblem.php?pid=3938

                            Portal

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


 

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 Outpu
36
13
1
13
36
1
36
2
16
13
 

半天沒看懂題目,水題一個。

問你有多少對(u,v)使得u,v之間的所有路徑的最大邊權的最小值小於給定的查詢值L。這種題一般都是把查詢和邊統一排序,然後並查集處理。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define M 70050
#define N 10050
struct node
{
	int u,v,c;
}a[M];
int father[N],rank[N];
__int64 V[N],ans[N],coun;
int find(int t)
{
	if(father[t]!=t)
		father[t]=find(father[t]);
	return father[t];
}
void Union(int x,int y)
{
	int a=find(x),b=find(y);
	if(a==b)
		return ;
	coun+=V[a]*V[b];
	if(rank[a]<rank[b])
	{
		father[a]=b;
		V[b]+=V[a];
	}
	else
	{
		father[b]=a;
		V[a]+=V[b];
		if(rank[a]==rank[b])
			rank[a]++;
	}
}
bool cmp(node a,node b)
{
	if(a.c==b.c)
		return a.u>b.u;
	return  a.c<b.c;
}
int main()
{
	int n,m,q,i,u,v,c,L;
	while(scanf("%d%d%d",&n,&m,&q)!=EOF)
	{
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&u,&v,&c);
			a[i].u=u;a[i].v=v;a[i].c=c;
		}
		for(i=0;i<q;i++)
		{
			scanf("%d",&L);
			a[m+i].u=-(i+1),a[m+i].v=-1;a[m+i].c=L;
		}
		sort(a,a+m+q,cmp);
		coun=0;
		for(i=1;i<=n;i++)
		{
			father[i]=i;
			rank[i]=1;
			V[i]=1;
		}
		for(i=0;i<m+q;i++)
		{
			u=a[i].u;v=a[i].v;
			if(u>0)
				Union(u,v);
			else
				ans[-u]=coun;
		}
		for(i=1;i<=q;i++)
			printf("%I64d\n",ans[i]);
	}
	return 0;
}


 

發佈了60 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章