传送门:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3148
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int MAXN = 755;
struct Node{
int s;//A[i] + B[j];
int b;//j
Node(int _s,int _b):s(_s),b(_b){}
bool operator < (const Node a) const {
return s > a.s ;
}
};
void merge(int * A,int * B,int * C,int n) {
priority_queue <Node> p;
//sort(A,A+n);
//sort(B,B+n);
for(int i = 0 ; i < n ; i++) {
p.push(Node(A[i]+B[0],0));
}
for(int i = 0 ; i < n ; i++) {
Node a = p.top(); p.pop();
C[i] = a.s;//Update the min;
int b = a.b;
if(b + 1 < n)p.push(Node(C[i]+B[b+1]-B[b],b+1));
}
}
int M[MAXN][MAXN];
void Deal_with() {
int n ;
while(~scanf("%d",&n)) {
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < n ; j++) {
scanf("%d",M[i]+j);
}
sort(M[i],M[i]+n);
}
for(int i = 1 ; i < n ; i++) {
merge(M[0],M[i],M[0],n);
}
for(int i = 0 ; i < n ; i++) {
printf("%d",M[0][i]);
printf(i == n -1 ? "\n" : " ");
}
}
}
int main(void) {
//freopen("a.in","r",stdin);
Deal_with();
return 0;
}