UVa 10954 Add All ——优先队列
2017-06-01 21:20
337 查看
用优先队列实现Huffman算法
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int main()
{
int n, x;
while (scanf("%d", &n) == 1 && n) {
priority_queue<int, vector<int>, greater<int> > q;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
q.push(x);
}
int ans = 0;
for (int i = 0; i < n - 1; i++) {
int a = q.top(); q.pop();
int b = q.top(); q.pop();
ans += a + b;
q.push((a + b));
}
printf("%d\n", ans);
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int main()
{
int n, x;
while (scanf("%d", &n) == 1 && n) {
priority_queue<int, vector<int>, greater<int> > q;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
q.push(x);
}
int ans = 0;
for (int i = 0; i < n - 1; i++) {
int a = q.top(); q.pop();
int b = q.top(); q.pop();
ans += a + b;
q.push((a + b));
}
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- UVA 10954 Add All(优先队列)
- UVA 10954 Add All (贪心 + 优先队列)
- UVA10954:Add All(优先队列)
- UVa 10954 (Huffman 优先队列) Add All
- UVA 10954- Add All(优先队列)
- UVA - 10954 Add All(贪心+优先队列)
- UVA - 10954 Add All 优先队列
- UVa 10954 Add All(优先队列)
- UVa10954 Add All (优先队列)
- UVA 10954 Add All 全加一起 模拟+贪心+优先队列
- Add All - UVa 10954 优先队列
- UVa 10954 Add All (石子合并_优先队列)
- UVa 10954 Add All(优先队列)
- Uva - 10954 - Add All
- 【NOIP合并果子】uva 10954 add all【贪心】——yhx
- UVA 10954 Add All
- UVa 10954 - Add All
- uva 10954 - Add All
- UVa 10954 - Add All
- Uva - 10954 - Add All