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)