K Smallest Sums UVA - 11997(优先队列、打表)【详解】

Describe

You’re given k arrays, each array has k integers. There are k^k ways to pick exactly one element in each 

array and calculate the sum of the integers. Your task is to find the k smallest sums among them. 


Input 
There will be several test cases. The first line of each case contains an integer k (2 ≤ k ≤ 750). Each of 
the following k lines contains k positive integers in each array. Each of these integers does not exceed 

1,000,000. The input is terminated by end-of-file (EOF). 


Output 
For each test case, print the k smallest sums, in ascending order. 
Sample Input 

1 8 5 
9 2 5 
10 7 6 

1 1 
1 2 
Sample Output 
9 10 12 

2 2


题意:输入一个k,然后输入k行k列个数,从每一行中选取一个数,使得他们之和最小,并从所有(k^k个数)中排列出k个最小的数——从小到大。

对于这样的题,我联想到了刚学习过的矩阵(同出一辙吧):

输入的数为a11, a12, a13,......, a1k

               a21, a22, a23,......, a2k

               .......

               ak1, ak2, ak3,......, akk

不妨先看一二两排,我们可以寻找一二两排的和最小k个数。

证明如下:

        我们先假设只有两排,分别为{排a ,排b }。

则有:

           a1+b1 <= a2+b1 <= a3+b1 <=......<= ak+b1

           a1+b1 <= a1+b2 <= a1+b3 <=......<= a1+bk

类似的我们可以得到这样一张表(默认在表中元素左边和上方的数小于等于该元素):

a1+b1    a1+b2    ......    a1+bk

a2+b1    a2+b2    ......    a2+bk

......    ......

ak+b1    ak+b2    ......    ak+bk

现在我们只需要从这表中找到n个最小的数再次赋给a,然后在和下一组的b重复上面的运算就可以慢慢的得到最后一组我们要的k个数的答案。

思路

    根据上面的证明,现在需要的就是对于处理每一个和对象,不难想到,这里需要用到优先队列,然后从a1+b1(这个注定是最小值)开始,我们查看a2+b1与a1+b2往下谁的值会是下一个小的数。

主要函数:

void work()
{
    priority_queue<node> Q;
    node temp;
    node now;
    for(int i=1; i<=k; i++)
    {
        temp.s=A[i]+B[1];
        temp.b=1;
        Q.push(temp);
    }
    for(int i=1; i<=k; i++)
    {
        temp=Q.top();
        Q.pop();
        A[i]=temp.s;
        if(temp.b<k)
        {
            now.s=temp.s-B[temp.b]+B[temp.b+1];
            now.b=temp.b+1;
            Q.push(now);
        }
    }
}

    这里,我们处理的B数组是有序的。

然后,很多人可能不懂我的if判断下的功能是什么,讲解一下:我们知道第一个判断的数是a1+b1,但显然如果没有这一串if语句,我们的下一个数只能是a2+b1,调用if语句的目的就是使得B数组也可以往后找。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
int k;
int A[800],B[800];
struct node
{
    int s,b;
    node()
    {}
    node(int _s, int _b)
    {
        s=_s;
        b=_b;
    }
    friend bool operator<(node a, node c)
    {
        return a.s>c.s;
    }
};
void work()
{
    priority_queue<node> Q;
    node temp;
    node now;
    for(int i=1; i<=k; i++)
    {
        temp.s=A[i]+B[1];
        temp.b=1;
        Q.push(temp);
    }
    for(int i=1; i<=k; i++)
    {
        temp=Q.top();
        Q.pop();
        A[i]=temp.s;
        if(temp.b<k)
        {
            now.s=temp.s-B[temp.b]+B[temp.b+1];
            now.b=temp.b+1;
            Q.push(now);
        }
    }
}
int main()
{
    while(scanf("%d",&k)!=EOF)
    {
        for(int i=1; i<=k; i++)
        {
            scanf("%d",&A[i]);
        }
        for(int i=2; i<=k; i++)
        {
            for(int j=1; j<=k; j++)
            {
                scanf("%d",&B[j]);
            }
            sort(B+1, B+k+1);
            work();
        }
        for(int i=1; i<=k; i++)
        {
            printf("%d",A[i]);
            if(i<k) printf(" ");
            else printf("\n");
        }
    }
    return 0;
}

  • 1
    点赞
  • 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
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值