2016-08-03 Problem Solving►UVa UVa 10954 - Add All Contents 1. Problem2. Solution3. Code Problem中文網址 Solution利用 Huffman Code 的方法,將較小的數字先行結合成另一個數字,一邊計算其代價。 CodeUVa 1095412345678910111213141516171819202122232425262728293031323334353637#include<cstdio>#include<queue>#include<vector>#include<functional>using namespace std;int main(){ int n; priority_queue<int, vector<int>, greater<int> > PQ; while (scanf("%d", &n) && n) { int num; long long sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &num); PQ.push(num); } for (int i = 1; i < n; i++) { int a, b; a = PQ.top(), PQ.pop(); b = PQ.top(), PQ.pop(); sum += (a + b); PQ.push(a + b); } PQ.pop(); printf("%lld\n", sum); } return 0;} Newer UVa 122 - Trees on the level Older UVa 884 - Factorial Factors