andre_joy

导航

poj 2442

地址:http://poj.org/problem?id=2442

题意:m段序列,每个序列取一个数,求组成序列和最小的n个数。

mark:最近刚接手的优先队列。

代码:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <queue>

using namespace std;

int a[2010], b[2010];

int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

int main()
{
    int t,m,n;
    int i,j,k;
    scanf("%d", &t);
    while(t-- && scanf("%d%d", &m, &n))
    {
        priority_queue <int> q;
        for(i = 0; i < n; i++)
            scanf("%d", a+i);
        qsort(a, n, 4, cmp);
        for(i = 1; i < m; i++)
        {
            for(j = 0; j < n; j++)
                scanf("%d", b+j);
            qsort(b, n, 4, cmp);
            for(j = 0; j < n; j++)
                q.push(a[j]+b[0]);
            for(j = 1; j < n; j++)
                for(k = 0; k < n; k++)
                {
                    if(b[j]+a[k] > q.top()) break;
                    q.pop();
                    q.push(b[j]+a[k]);
                }
            for(j = 0; j < n; j++)
            {
                a[n-j-1] = q.top();
                q.pop();
            }
        }
        for(i = 0; i < n; i++)
        {
            if(i) printf(" ");
            printf("%d", a[i]);
        }
        printf("\n");
    }
    return 0;
}

 

posted on 2012-07-22 10:13  andre_joy  阅读(107)  评论(0编辑  收藏  举报