您的位置:首页 > 其它

hdu 3938 Portal(离线并查集)

2014-02-25 01:21 417 查看

Portal

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 747 Accepted Submission(s): 373



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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).

[align=left]Output[/align]
Output the answer to each query on a separate line.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

36
13
1
13
36
1
36
2
16
13
题意:给出m条边,每条边都有一个花费,每两点a到b可形成多条路径,定义走过a b路径的花费为a到b的各条路径中花费最大的边的最小值,给出q个询问,每个询问给出拥有多少权值,对于每个询问,要输出能走过的路径条数。思路:类似kruskal算法的思想,用并查集离线处理。将互达的点看成一个集合,集合的点的个数为rank[i]。按边的花费从小到大给边排序,然后从小到大枚举每条边,若这条边的两个端点不在一个同集合中,设分别为集合a和集合b,那么这条边必为集合a的点到集合b的点的路径中花费最大的边的最小值,那么答案就可以加上rank[a]*rank[b]。
AC代码:[code]#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define L(r) (r<<1)
#define R(r) (r<<1|1)
using namespace std;

const int maxn = 10005;
const int maxm = 50005;
const int INF = 1000000000;

int n, m, q;
int fa[maxn], rank[maxn];
struct Edge
{
int u, v, w;
} et[maxm];
struct node
{
int L, id;
} query[maxn];
bool cmp1(Edge a, Edge b)
{
return a.w < b.w;
}
bool cmp2(node a, node b)
{
return a.L < b.L;
}
void init()
{
for(int i = 1; i <= n; i++)
{
rank[i] = 1;
fa[i] = i;
}
}
int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void Union(int a, int b)
{
if(rank[a] >= rank[b])
{
fa[b] = a;
rank[a] += rank[b];
}
else
{
fa[a] = b;
rank[b] += rank[a];
}
}

int main()
{
while(~scanf("%d%d%d", &n, &m, &q))
{
init();
for(int i = 0; i < m; i++)
scanf("%d%d%d", &et[i].u, &et[i].v, &et[i].w);
for(int i = 0; i < q; i++)
{
scanf("%d", &query[i].L);
query[i].id = i;
}
sort(et, et + m, cmp1);
sort(query, query + q, cmp2);
int cnt = 0;
int ans[maxm] = {0};
for(int i = 0; i < q; i++)
{
if(i > 0) ans[query[i].id] = ans[query[i - 1].id];
while(et[cnt].w <= query[i].L  && cnt < m)
{
int ra = find(et[cnt].u);
int rb = find(et[cnt].v);
if(ra != rb)
{
ans[query[i].id] += rank[ra] * rank[rb];
Union(ra, rb);
}
cnt++;
}
}
for(int i = 0; i < q; i++)
printf("%d\n", ans[i]);
}
return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: