首页 > ACM题库 > HDU-杭电 > HDU 3938-Portal-图-[解题报告]HOJ
2015
04-14

HDU 3938-Portal-图-[解题报告]HOJ

Portal

问题描述 :

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.

输入:

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).

输出:

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).

样例输入:

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

样例输出:

36
13
1
13
36
1
36
2
16
13

题意描述:简单的讲就是,给你一张无向图,求有多少条路径使得路径上的花费小于L,这里路径上的花费是这样规定的,a、b两点之间的多条路径中的最长的边最小值!

分析:因为考虑到数据的大小所以需采用离线算法先计算出结果后在统一输出结果!这里首先要从a到b的所有路径中筛选出这样一条路径(路径中的最长边是所有路径中最小),那么这就需要采用最小生成树kruskal的思想,那么任意两个集合A,B之间可以形成的路径即为num[A]*num[B](这里忽视了原来两集合中形成的条数,因为实现的时候要不断的累加以前的结果,详情见代码)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<set>
using namespace std;
const int N= 10003;
const int M= 50002;
struct  Node
{
    int u,v;
    int len;
} edge[M];
bool cmp(Node a,Node b)
{
    return a.len<=b.len;
}
int fa[N];
int sum[N];
int n,m,q;
void init()
{
    for(int i=1; i<=n; i++)
        fa[i]=i,sum[i]=1;
}
int findx(int x)
{
    return fa[x]=fa[x]==x?x:findx(fa[x]);
}
int Union(int a,int b)
{
    int aa=findx(a);
    int bb=findx(b);
    if(aa==bb)
        return 0;
    else if(fa[aa]<fa[bb])
    {
        fa[bb]=aa;
        int tmp=sum[aa]*sum[bb];
        sum[aa]+=sum[bb];
        return tmp;
    }
    else
    {
        fa[aa]=bb;
        int tmp=sum[aa]*sum[bb];
        sum[bb]+=sum[aa];
        return tmp;
    }
}

int ans[N];
struct Q
{
    int L;
    int id;
    int ans;
} que[N];
bool cmp1(Q a,Q b)
{
    return a.L<=b.L;
}
bool cmp2(Q a,Q b)
{
    return a.id<b.id;
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        init();
        for(int i=0; i<m; i++)
            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].len);
        for(int i=0; i<q; i++)
            {scanf("%d",&que[i].L);que[i].id=i;que[i].ans=0;}
        sort(edge,edge+m,cmp);
        sort(que,que+q,cmp1);
        int cnt=0;
        for(int i=0; i<q; i++)
        {
            while(edge[cnt].len<=que[i].L&&cnt<m)
            {
                //cout<<"FFF"<<endl;
                int aa=findx(edge[cnt].u);
                int bb=findx(edge[cnt].v);
                if(aa==bb)
                {
                    cnt++;
                    continue;
                }
                else
                {
                    que[i].ans+=Union(edge[cnt].u,edge[cnt].v);
                    cnt++;
                }
            }
            if(i>=1)
                que[i].ans+=que[i-1].ans;
        }
        sort(que,que+q,cmp2);
        for(int i=0; i<q; i++)
        {
           printf("%d\n",que[i].ans);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/weiguang_123/article/details/8067239