UVa 11997 K Smallest Sums / 优先队列

优先队列

n行每行n个数字 每行选一个求和 求和最小的n个

假设只有2行

每一行先排序

a1 a2 a3 ... an

b1 b2 b3 ... bn

把a1 + b1, a2 + b1, a3 + b1,..., an + b1入队列

然后取出一个最小的(pop ) 假如是ax + by 那么就放进去一个ax+b(y+1)(push)直到取出n个 把他们存到数组c

现在有n 那么可以两两合并 第一行和第二行合并后的结果在和第三行合并。。。。

#include <cstdio>
#include <queue>
#include <cstring> 
#include <algorithm>
const int maxn = 800;
using namespace std;

int a[maxn][maxn];
struct Item
{
	int s;
	int b; 
	Item(int s, int b):s(s), b(b) {}
	bool operator < (const Item& rhs) const
	{
		return s > rhs.s;
	}	
};
int n;

void merge(int* A, int* B, int* C)
{
	 priority_queue <Item> q;
	 for(int i = 0; i < n; i++)
	 	q.push(Item(A[i]+B[0], 0));
	 for(int i = 0; i < n; i++)
	 {
	 	Item item = q.top();
	 	q.pop();
	 	C[i] = item.s;
	 	int b = item.b;
	 	if(b+1 < n)
	 	q.push(Item(item.s-B[b]+B[b+1], b+1)); 
	 }
}		  
int main()
{
	int i, j; 
	while(scanf("%d", &n) == 1)
	{
		for(i = 0; i < n; i++)
		{
			for(j = 0; j < n; j++)
				scanf("%d", &a[i][j]);
			sort(a[i], a[i]+n);
		}
		for(i = 1; i < n; i++)
			merge(a[0], a[i], a[0]);
		printf("%d",a[0][0]);
		for(i = 1; i < n; i++)
			printf(" %d", a[0][i]); 
		printf("\n");
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Prim算法是一种最小生成树算法,可以通过使用优先队列来提高算法效率。以下是使用C语言实现Prim算法的示例代码: ```c #include <stdio.h> #include <limits.h> #define V 5 // 顶点数 #define INF INT_MAX // 定义无穷大 int graph[V][V] = {{0, 2, 0, 6, 0}, {2, 0, 3, 8, 5}, {0, 3, 0, 0, 7}, {6, 8, 0, 0, 9}, {0, 5, 7, 9, 0}}; // 图的邻接矩阵表示 int primMST() { int key[V]; // 存储每个顶点到MST的最小距离 int parent[V]; // 存储每个顶点的父节点 int visited[V]; // 存储每个顶点是否已经加入MST for (int i = 0; i < V; i++) { key[i] = INF; visited[i] = 0; } key[0] = 0; parent[0] = -1; // 创建优先队列 struct node { int vertex; int key; }; struct priority_queue { int size; struct node heap[V]; } pq; pq.size = 0; // 将第一个顶点加入优先队列 struct node n; n.vertex = 0; n.key = key[0]; pq.heap[pq.size++] = n; while (pq.size > 0) { // 从优先队列中取出距离MST最近的顶点 int u = pq.heap[0].vertex; pq.heap[0] = pq.heap[--pq.size]; int i = 0; while (2 * i + 1 < pq.size) { int left = 2 * i + 1; int right = 2 * i + 2; int smallest = i; if (pq.heap[left].key < pq.heap[smallest].key) { smallest = left; } if (right < pq.size && pq.heap[right].key < pq.heap[smallest].key) { smallest = right; } if (smallest == i) { break; } struct node temp = pq.heap[i]; pq.heap[i] = pq.heap[smallest]; pq.heap[smallest] = temp; i = smallest; } // 将该顶点加入MST visited[u] = 1; // 更新与该顶点相邻的顶点到MST的最小距离 for (int v = 0; v < V; v++) { if (graph[u][v] && !visited[v] && graph[u][v] < key[v]) { key[v] = graph[u][v]; parent[v] = u; // 将该顶点加入优先队列 struct node n; n.vertex = v; n.key = key[v]; pq.heap[pq.size++] = n; int j = pq.size - 1; while (j > 0 && pq.heap[(j - 1) / 2].key > pq.heap[j].key) { struct node temp = pq.heap[(j - 1) / 2]; pq.heap[(j - 1) / 2] = pq.heap[j]; pq.heap[j] = temp; j = (j - 1) / 2; } } } } // 输出MST int weight = 0; for (int i = 1; i < V; i++) { printf("%d - %d : %d\n", parent[i], i, graph[i][parent[i]]); weight += graph[i][parent[i]]; } printf("MST的总权重为:%d\n", weight); } int main() { primMST(); return 0; } ``` 在这个示例代码中,我们首先定义了一个邻接矩阵表示的图,然后实现了Prim算法。在实现Prim算法时,我们使用了一个优先队列来存储距离MST最近的顶点。在每一次迭代中,我们从优先队列中取出距离MST最近的顶点,并将该顶点加入MST。然后,我们更新与该顶点相邻的顶点到MST的最小距离,并将这些顶点加入优先队列。最后,我们输出MST的边和总权重。 需要注意的是,在这个示例代码中,我们使用了一个结构体来表示优先队列中的每一个元素,同时使用了一个结构体来表示优先队列本身。我们还实现了一个删除堆顶元素的函数和一个向堆中插入元素的函数,以保证优先队列的正确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值