思路:先考虑两行的关系
1.排序
2.观察发现以下规律
故从A1+B1开始宽搜,儿子(x+1,y),(x,y+1)扩展
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<iostream>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<stdlib.h>
using namespace std;
int shu[105][2005],tshu[2005];
#define M 2005
int flag[M][M];
struct node
{
int x,y,val;
bool operator<(const node& s0)const
{
return val>s0.val;//将s0看为第一个元素,值小优先级高
}
}t,tp;
int main()
{
int cas,i,j,k,n,m,x,y;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%d",&shu[i][j]);
}
for(i=0;i<m;i++)
{
sort(shu[i],shu[i]+n);
}
memset(flag,0,sizeof(flag));
int an=0;
for(i=1;i<m;i++)
{
priority_queue<node>pq;
t.x=0;
t.y=0;
t.val=shu[i-1][0]+shu[i][0];
pq.push(t);
flag[0][0]=1+an;
for(j=0;j<n;j++)
{
tp=pq.top();
pq.pop();
tshu[j]=tp.val;
// printf("x=%d y=%d val=%d\n",tp.x,tp.y,tp.val);
t.x=tp.x+1;
t.y=tp.y;
if(flag[t.x][t.y]!=1+an)
{
t.val=shu[i-1][t.x]+shu[i][t.y];
flag[t.x][t.y]=1+an;
pq.push(t);
}
t.x=tp.x;
t.y=tp.y+1;
if(flag[t.x][t.y]!=1+an)
{
t.val=shu[i-1][t.x]+shu[i][t.y];
flag[t.x][t.y]=1+an;
pq.push(t);
}
}
an+=3;
for(j=0;j<n;j++)
shu[i][j]=tshu[j];
}
for(i=0;i<n;i++)
{
printf("%d%c",shu[m-1][i],i==n-1?'\n':' ');
}
}
return 0;
}
/*
1111
2 5
1 2 3 100 1000
1 2 31 1000 1000
*/