Portal
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2604 Accepted Submission(s): 1246
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
2011 Multi-University Training Contest 10 - Host by HRBEU
问题链接:HDU3938 Portal
问题简述:(略)
问题分析:
带权并查集问题,变形kruskal算法。
需要一次性算出查询结果存储起来,然后一次性输出结果,这样速度比较快。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* HDU3938 Portal */
#include
using namespace std;
typedef long long LL;
const int N = 10000;
int f[N + 1];
LL cnt[N + 1], ans[N + 1];
void UFInit(int n)
{
for(int i = 0; i <= n; i++)
f[i] = i, cnt[i] = 1;
}
int Find(int a) {
return a == f[a] ? a : f[a] = Find(f[a]);}
int Union(int a, int b)
{
if ((a = Find(a)) != (b = Find(b))) {
int tmp = cnt[a] * cnt[b];
f[a] = b;
cnt[b] += cnt[a];
cnt[a] = 0;
return tmp;
} else
return 0;
}
int n, m, q;
int main()
{
while(~scanf("%d%d%d", &n, &m, &q)) {
memset(ans, 0, sizeof(ans));
vector<pair<int, pair<int, int> > > g(m);
vector<pair<int, int > > qq(q);
for(int i = 0; i < m; i++)
scanf("%d%d%d", &g[i].second.first, &g[i].second.second, &g[i].first);
for(int i = 0; i < q; i++)
scanf("%d", &qq[i].first), qq[i].second = i;
sort(g.begin(), g.end());
sort(qq.begin(), qq.end());
UFInit(n);
LL sum = 0;
int j = 0;
for(int i = 0; i < q; i++) {
while(j < m && g[j].first <= qq[i].first)
sum += Union(g[j].second.first, g[j].second.second), j++;
ans[qq[i].second] = sum;
}
for(int i = 0; i < q; i++)
printf("%lld\n", ans[i]);
}
return 0;
}
AC的C++语言程序如下:
/* HDU3938 Portal */
#include
using namespace std;
typedef long long LL;
const int N = 10000;
int f[N + 1];
LL cnt[N + 1], ans[N + 1];
void UFInit(int n)
{
for(int i = 0; i <= n; i++)
f[i] = i, cnt[i] = 1;
}
int Find(int a) {
return a == f[a] ? a : f[a] = Find(f[a]);}
int Union(int a, int b)
{
if ((a = Find(a)) != (b = Find(b))) {
int tmp = cnt[a] * cnt[b];
f[a] = b;
cnt[b] += cnt[a];
cnt[a] = 0;
return tmp;
} else
return 0;
}
int n, m, q;
struct Node {
int u, v, w;
} e[N*5];
struct Q {
int l, id;
} qq[N];
int cmp1(Node a, Node b)
{
return a.w < b.w;
}
int cmp2(Q a, Q b)
{
return a.l < b.l;
}
int main()
{
while(~scanf("%d%d%d", &n, &m, &q)) {
memset(ans, 0, sizeof(ans));
for(int i = 0; i < m; i++)
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
for(int i = 0; i < q; i++)
scanf("%d", &qq[i].l), qq[i].m = i;
sort(e, e + m, cmp1);
sort(qq, qq + q, cmp2);
UFInit(n);
LL sum = 0;
int j = 0;
for(int i = 0; i < q; i++) {
while(j < m && e[j].w <= qq[i].l)
sum += Union(e[j].u, e[j].v), j++;
ans[qq[i].id] = sum;
}
for(int i = 0; i < q; i++)
printf("%lld\n", ans[i]);
}
return 0;
}