Hdu3938 portal offline query set

Source: Internet
Author: User

An offline algorithm is a method that reads all input and computes all the answers before outputting them. It is mainly used to avoid repeated computation. It is similar to using the tabulation method when calculating the Fibonacci series.

Question: How many points are there for an undirected graph so that the cost on the path between the two points is less than l, the minimum value of the maximum edge in all paths between two points A and B.
Of course this is not the case. The question is how many paths are there, which is fuzzy here. In the end, how can two paths be regarded as two? At this time, you can check the question again and find that, if it is understood that the points passing through the path are different, the so-called spending between two points in the question is meaningless, so we can guess, the question must be correct.
Then, the question is clarified and then analyzed. For all the paths of a point, as long as the path of the shortest biggest edge appears, the path of all the larger and largest edges after it is meaningless, then, sort all edges by weight from small to large, and add edges using the query set method. Connected points are no longer considered. When edge is added, two sets are combined. Assume that the set size is divided into A and B. Obviously, the path type is a * B, in this case, the number of types must be added to all L values greater than the maximum edge in the set.

The algorithm makes it clear that it is an offline algorithm. First, all edges and l are input, and all edges are sorted from small to large, then, for each L, combine the edges with smaller edge weights and calculate the number of types.

The above two sections are from: http://blog.csdn.net/sdj222555/article/details/7439187

I am a little touched. If I have not understood the question, I can't really do it.

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=10010,M=50010;struct node{    int u,v,w;}edge[M];bool cmp(const node &a, const node &b){    return a.w<b.w;}int f[N],r[N];int n,m,q,cnt;void init(){    for(int i=0;i<=n;i++)    {        f[i]=i;r[i]=1;    }}int Find(int x){    if(x==f[x])  return x;    return f[x]=Find(f[x]);}void Link(int x,int y){    int a=Find(x), b=Find(y);    cnt=0;    if(a!=b)    {        f[b]=a;        cnt=r[a]*r[b];        r[a]+=r[b];    }}int ans[N];struct NODE{    int id,re,num;}que[N];bool cmp1(const NODE &a,const NODE &b){    return a.num<b.num;}bool cmp2(const NODE &a,const NODE &b){    return a.id<b.id;}int main(){    //freopen("test.txt","r",stdin);    int i,j,k;    while(scanf("%d%d%d",&n,&m,&q)!=EOF)    {        init();        for(i=0;i<m;i++)            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);        for(i=0;i<q;i++)        {            scanf("%d",&que[i].num);            que[i].id=i;            que[i].re=0;        }        sort(edge,edge+m,cmp);        sort(que,que+q,cmp1);        k=0;        for(i=0;i<q;i++)        {            while(edge[k].w<=que[i].num&&k<m)            {                int a=Find(edge[k].u), b=Find(edge[k].v);                if(a==b) {k++; continue;}                else                {                    Link(edge[k].u,edge[k].v);                    que[i].re+=cnt;                    k++;                }            }            if(i>0) que[i].re+=que[i-1].re;        }        sort(que,que+q,cmp2);        for(i=0;i<q;i++)            printf("%d\n",que[i].re);    }    return 0;}

 

Hdu3938 portal offline query set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.