HDU OJ 5441 Travel 2015online E

Source: Internet
Author: User

Title: Click here

Test instructions

  There is a very grumpy person who wants to travel by car n a city. Connecting the city is a total of M-road (bidirectional). He was uncomfortable sitting in the car, with a maximum of X minutes at a time. But at every stop he can rest (re-timer). There are a total of Q queries. Ask how many roads there are on the way he can not erupt. A to B and B to a are different roads. A and B must be different.

Analysis:

and check Set

1#include <iostream>2#include <cstdio>3#include <algorithm>4 5 using namespacestd;6 Const intM = 2e4+5;7 8 structEdge {//Save node Information9     int  from, to, cost;Ten     BOOL operator< (ConstEdge x)Const{returnCost <X.cost;} One} es[m*5]; A  - structQuery {//Save problem Information -     intVal, id; the     BOOL operator< (ConstQuery x)Const{returnVal <X.val;}; -} que[5005]; -  - intN, M, q; + intPRE[M];//and check Set - intANS[M];//Answer + intSUM[M];//record and check the height of the set tree A  at intFindintx) { -     returnPRE[X] = = x? X:PRE[X] =find (Pre[x]); - } - voidMergeintXinty) { -x = Find (x); y =find (y); -     if(Sum[x] > Sum[y]) {Sum[x] + = Sum[y]; Pre[y] =x;} in     Else{Sum[y] + = sum[x]; Pre[x] =y;} - } to voidsolve () { +scanf"%d%d%d", &n, &m, &q); -      for(intI=0; i<m; i++ ) thescanf"%d%d%d", &es[i]. from, &es[i].to, &es[i].cost); *Sort (es, es+m); $      for(intI=0; i<q; i++ )    {Panax Notoginsengscanf"%d", &que[i].val); -Que[i].id =i; the     } +Sort (que, que+q); A      for(intI=0; i<=n; i++) {Pre[i] = i; sum[i] =1; } the     intj =0; +     intres =0; -      for(intI=0; i<q; i++ )    { $          for(; j<m && que[i].val>=es[j].cost; J + + ) { $             intx = Find (Es[j]. from ); -             inty =find (es[j].to); -             if(x = = y)Continue; theRes + = sum[x] *Sum[y]; -Merge (Es[j]. from, es[j].to);Wuyi         } theAns[que[i].id] =Res; -     } Wu      for(intI=0; i<q; i++ ) -printf"%d\n", ans[i]<<1 ); About } $  - intMain () { -     intT scanf"%d", &t); -      while(t-- )    { A solve ();  +     } the  -     return 0; $}

HDU OJ 5441 Travel 2015online E

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.