HDU 5441 Travel

Source: Internet
Author: User

Test instructions: A person who does not want to be in a bus or other time wants to travel, give N cities, m roads and Q inquiries

Test instructions thought reference: Portal

PS: Brute force n times minimum spanning tree must timeout, because the number of edges reached 10 of 5

1#include <cmath>2#include <cstring>3#include <cstdio>4#include <algorithm>5 using namespacestd;6 intn,m,q;7 Const intqq=20005;8 intPRE[QQ];9 intSUM[QQ];Ten intTAR[QQ]; One structEdge A { -     intu,v,w; -     BOOL operator< (Edge a)Const the     { -         returnw<A.W; -     } -}edge[qq*5]; + structQuery - { +     intMoney ; A     intorder; at     BOOL operator< (Query b)Const -     { -         returnmoney<b.money;//always forget to bring return -     } - }QUERY[QQ]; - intFindintx) in { -     if(X==pre[x])returnx; to     returnpre[x]=find (Pre[x]); + } - voidSlove () the { *      for(intI=1; i<=n;++i) { $pre[i]=i; Panax Notoginsengsum[i]=1;//initialize each of the connected blocks -     } the     intans=0; +     intj=0; A      for(intI=0; i<q;++i) { the          for(; J<m && edge[j].w<=query[i].money;++j) { +             intu,v; -u=edge[j].u;v=edge[j].v; $             intX=find (U);inty=Find (v); $             if(x==y)Continue; -Ans+=sum[x]*sum[y];//How many 22 combinations of cities will be added when connecting block X and connected block y to form a new connected block?  -Pre[y]=x;//The city here is a combination, not an orderly pair the                     //In other words, each element of the connected block X can form a new city with any element in the connected block y . -                         //So here is the number of elements connecting block x to multiply the elements of the connected block Y,WuyiSum[x]=sum[x]+sum[y];//two connected blocks connect to a connected block, update the number of elements of the connected block the         }                              -tar[query[i].order]=ans*2;//because it is ordered, so multiply by 2, Wu}//The corresponding order to fill in the value, -      for(intI=0; i<q;++i) Aboutprintf"%d\n", Tar[i]); $ } - intMain () - { -     intT;SCANF ("%d",&t); A      while(t--){ +scanf"%d%d%d",&n,&m,&q); the          for(intI=0; i<m;++i) -scanf"%d%d%d",&edge[i].u,&edge[i].v,&EDGE[I].W); $          for(intI=0; i<q;++i) { thescanf"%d",&Query[i].money); theQuery[i].order=i;//record labels to fill values with arrays the         } theSort (edge,edge+m); -Sort (query,query+q); in slove (); the     } the     return 0; About}

HDU 5441 Travel

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.