1. 程式人生 > >uva 11997(優先隊列)

uva 11997(優先隊列)

int string name con cstring operator truct style tdi

不說了,這種優化想法想不到,劉汝嘉牛逼

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=800;
int a[maxn][maxn];
int n;
struct note
{
    int s;
    int b;
   bool operator <(const note &p) const
   {
       
return s>p.s; } }; void mage(int *A,int *B,int *C,int n) { priority_queue<note> pq; for(int i=0;i<n;i++) pq.push(note{A[i]+B[0],0}); for(int i=0;i<n;i++) { note nn=pq.top(); pq.pop(); C[i]=nn.s; int b=nn.b; if(b+1<n) pq.push(note{nn.s-B[b]+B[b+1
],b+1}); } } int main() { while(~scanf("%d",&n)) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) scanf("%d",&a[i][j]); sort(a[i],a[i]+n); } for(int i=1;i<n;i++) mage(a[0],a[i],a[0],n); for
(int i=0;i<n-1;i++) printf("%d ",a[0][i]); printf("%d\n",a[0][n-1]); } return 0; }

uva 11997(優先隊列)