PriorityQueue 使用方法 POJ 2442 NOJ 1491

POJ 2442:

import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;

public classMain {
	
	public static final int MAXN = 105;
	public static final int MAXM = 100005;
	public static final int MOD = 10000;
	public static final int INF = 1000000000;
	public static final double EPS = 1E-6;
	
	int[] arr1, arr2;
	PriorityQueue<Integer> heap;
	
	void run() {
		int t = cin.nextInt();
		while (t-- > 0) {
			int m = cin.nextInt();
			int n = cin.nextInt();
			arr1 = new int[n];
			arr2 = new int[n];
			heap = new PriorityQueue<Integer>(n, new Comparator<Integer>(){
				@Override
				public int compare(Integer a, Integer b) {
					if (a < b) return 1;
					if (a > b) return -1;
					return 0;
				}
			});
			for (int i = 0; i < n; i++) 
				arr1[i] = cin.nextInt();
			Arrays.sort(arr1);
			for (int i = 1; i < m; i++) {
				for (int j = 0; j < n; j++) {
					arr2[j] = cin.nextInt();
					heap.offer(arr1[0] + arr2[j]);
				}
				Arrays.sort(arr2);
				for (int j = 1; j < n; j++) {
					for (int k = 0; k < n; k++) {
						if (arr1[j] + arr2[k] > heap.peek())
							break;
						heap.poll();
						heap.offer(arr1[j] + arr2[k]);
					}
				}
				for (int j = n - 1; j >= 0; j--) 
					arr1[j] = heap.poll();
			}
			for (int i = 0; i < n; i++) {
				if (i != 0) System.out.print(' ');
				System.out.print(arr1[i]);
			}
			System.out.println();
		}
	}
	
	public static void main(String[] args) throws Exception {
		Main solved = new Main();
		solved.run();
	}
	
	Scanner cin = new Scanner(new BufferedInputStream(System.in));
	
}

 NOJ 1491 Hhffman编码:

import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;

public class Main {

static final int MAXN = 1005;
static final int MAXM = 5005;
static final int MOD = 10000;
static final int INF = 1000000000;
static final double EPS = 1E-6;

int n, idx, ans;
Node[] node = new Node[MAXN * MAXN / 2];
PriorityQueue<Node> Q; // 优先队列默认是从小到大

void init() {
for (int i = 0; i < n; i++) {
node[i] = new Node();
node[i].val = cin.nextInt();
node[i].left = node[i].right = -1;
node[i].idx = i;
Q.offer(node[i]);
}
ans = 0;
idx = n;
}

void Huffman() {
Node tt, tp;
while (Q.size() > 1) {
tt = Q.poll();
tp = Q.poll();
node[idx] = new Node();
node[idx].left = tt.idx;
node[idx].right = tp.idx;
node[idx].val = tt.val + tp.val;
node[idx].idx = idx;
Q.offer(node[idx]);
idx++;
}
}

void dfs(int idx, int count) {
if (node[idx].left == -1 && node[idx].right == -1) {
ans += node[idx].val * count;
return;
}
dfs(node[idx].left, count + 1);
dfs(node[idx].right, count + 1);
}

void run() throws IOException {
while (cin.hasNext()) {
n = cin.nextInt();
Q = new PriorityQueue<Node>(n, new Comparator<Node>() {
public int compare(Node a, Node b) {
return a.val - b.val;
}
});
init();
Huffman();
dfs(idx - 1, 0);
System.out.println(ans);
}
}

public static void main(String[] args) throws IOException {
Main solved = new Main();
solved.run();
}

Scanner cin = new Scanner(new BufferedInputStream(System.in));
}

class Node {
int left, right, idx, val;

Node() {}

Node(int left, int right, int idx, int val) {
this.left = left;
this.right = right;
this.idx = idx;
this.val = val;
}

}

 

 

注意PriorityQueue比较函数写法

posted on 2013-01-18 22:28  Sure_Yi  阅读(334)  评论(0编辑  收藏  举报

导航