K Smallest Sums UVA - 11997

有序表合并

#include<bits/stdc++.h>
using namespace std;
//#define int long long
const int maxn=750+5;
struct Item
{
    int s,b;
    bool operator<(const Item& rhs) const
    {
        return s>rhs.s;
    }
};
void Merge(int *A,int *B,int *C,int n)
{
    priority_queue<Item> q;
    for(int i=0;i<n;i++)
    q.push({A[i]+B[0],0});
    for(int i=0;i<n;i++){
        Item it=q.top();q.pop();
        A[i]=it.s;
        if(it.b+1<n) q.push({it.s-B[it.b]+B[it.b+1],it.b+1});
    }
}
int A[maxn][maxn];
int32_t main()
{
    int n;
    while(cin>>n){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)
                cin>>A[i][j];
            sort(A[i],A[i]+n);
        }
        for(int i=1;i<n;i++)
            Merge(A[0],A[i],A[0],n);
        cout<<A[0][0];
        for(int i=1;i<n;i++)
            printf(" %d",A[0][i]);
        puts("");
    }
}

猜你喜欢

转载自www.cnblogs.com/033000-/p/10311831.html