poj2442-优先队列

思路:先考虑两行的关系

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

*/


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值