HDU3938Portal(并查集离线应用)求路的条数

来源:互联网 发布:人像摄影师 知乎 编辑:程序博客网 时间:2024/06/05 20:26

Portal

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


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 107 2 16 8 34 5 85 8 22 8 96 4 52 1 58 10 57 3 77 8 810615918276
 

Sample Output
36131133613621613
 

Source
2011 Multi-University Training Contest 10 - Host by HRBEU 
题意:有n个点m条边,现在问给出一个能量值L,有多少条路,两个点之间虽有多条路可走但只算一条,这条路的边的最大权值要小于等于给出的L值。
#include<stdio.h>#include<algorithm>using namespace std;const int N = 10005;struct EDG{    int u,v,c;};struct ANS{    int i,c;};EDG edg[N*5];ANS ans[N];int fath[N],node[N],n;int cmp1(const EDG &a,const EDG &b){    return a.c<b.c;}int cmp2(const ANS &a,const ANS &b){    return a.c<b.c;}void init(){    for(int i=1; i<=n;i++)    {        node[i]=1; fath[i]=i;    }}int findfath(int x){    if(x!=fath[x])        fath[x]=findfath(fath[x]);    return fath[x];}int main(){    int m,q,aa[N];    while(scanf("%d%d%d",&n,&m,&q)>0)    {        init();        for(int i=1;i<=m;i++)            scanf("%d%d%d",&edg[i].u,&edg[i].v,&edg[i].c);        for(int i=1;i<=q;i++)        {            scanf("%d",&ans[i].c); ans[i].i=i;        }        sort(edg+1,edg+1+m,cmp1);        sort(ans+1,ans+1+q,cmp2);        int sum,u,v,tq,tm;        sum=0; tq=tm=1;        while(tm<=m)        {            if(edg[tm].c<=ans[tq].c)            {                u=findfath(edg[tm].u);                v=findfath(edg[tm].v);                if(u!=v)                {                    sum+=node[u]*node[v];                    fath[u]=v;                    node[v]+=node[u];                }                tm++;            }            else while(tq<=q&&edg[tm].c>ans[tq].c)            {                aa[ans[tq].i]=sum; tq++;            }            if(tq>q)                break;        }        while(tq<=q)         {             aa[ans[tq].i]=sum; tq++;         }        for(int i=1;i<=q;i++)            printf("%d\n",aa[i]);    }}


0 0
原创粉丝点击