1. 程式人生 > >K Smallest Sums UVA - 11997

K Smallest Sums UVA - 11997

ace puts spa int out bsp struct smallest clu

有序表合並

#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(""); } }

K Smallest Sums UVA - 11997