POJ2442 优先队列

优先队列不同于普通队列,它分为最大优先队列和最小优先队列,当你写成priority_queue<int >a时,它默认为最大优先队列

最大优先队列即,当它用  a.top()时   从队列中寻找最大的数值,当它用  a.pop()时   从队列中删除最大的数值,

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int main()
{
priority_queue<int> a;

int t;
scanf("%d",&t);
while(t--)
{


int i,j,k,x[3000],y[3000],m,n;
scanf("%d %d",&m,&n);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
sort(x,x+n);

for(i=1;i<m;i++)
{
for(int g=0;g<n;g++)
scanf("%d",&y[g]);

 


for(k=0;k<n;k++)
a.push(x[k]+y[0]);

for(j=1;j<n;j++)
for(k=0;k<n;k++)
{
if(y[j]+x[k]>a.top())
break;

a.pop();
a.push(y[j]+x[k]);
}
for(k=0;k<n;k++)
{
x[k]=a.top();
a.pop();
}
sort(x,x+n);
}

for(i=0;i<n-1;i++)
cout << x[i] << " " ;
cout << x[n-1] << endl;
}
return 0;
}

posted @ 2015-10-10 16:15  大领主  阅读(178)  评论(0编辑  收藏  举报