POJ 3253 Fence Repair (Mansha)

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=3253

Main topic:

There is a stick, need to cut into n knots, each section has a fixed length, a stick of X-length into two segments, need to spend X, asked to cut into the required state to require the minimum cost?

Problem Solving Ideas:

Huffman number, the need for each section of the stick length of the tree as a node, the process of cutting the stick upside down, into the N-cut sticks connected, the two process costs are the same. According to the nature of Huffman, we know that the shortest two sticks together, put to the remaining n-2 root stick, and then select two the shortest connection up, and then put back, until all the wood roots are connected together OK.

Code:

1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <algorithm>5#include <iostream>6#include <cmath>7#include <queue>8 using namespacestd;9 Ten intMain () One { APriority_queue <int> Q;//priority queue, default big First out team, so we take the number of incoming team negative -     intN; -     Long Longsum, num, m; thescanf ("%d", &n); -      while(N--) -     { -scanf ("%d", &m); +Q.push (-M);//into the team -     } +sum =0; A      while(Q.size () >1)//the number of elements in the queue is greater than 1 at     { -num =0; -  -num + = Q.top ();//out of the team two shortest sticks - Q.pop (); -  innum + =q.top (); - Q.pop (); to  +sum + = num;//Link Up -Q.push (num);//into the team the     } *printf ("%lld\n", -sum); $     return 0;Panax Notoginseng}

POJ 3253 Fence Repair (Mansha)

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.