POJ 1659 Frogs' Neighborhood

http://poj.org/problem?id=1659

Havel 定理 贪心。。

考虑要全面

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>

using std::sort;
struct Node
{
    int pos, w;
    bool operator < (Node a)
    {    return w >= a.w ; }
}lake[25];
int map[25][25];
int main()
{
    int cas, i, j, n, a;
    bool flag;
    scanf("%d", &cas);
    while(cas--)
    {
        flag = 0;
        memset(map, 0, sizeof(map));
        scanf("%d", &n);
        for(i=1; i<=n; i++)
        {
            scanf("%d", &a);
            lake[i].pos = i, lake[i].w = a;
        }
        for(j=1; j<=n&& !flag; j++)
        {
            sort(lake+1, lake+n+1);
            int val = lake[1].w;
            lake[1].w = 0;
            if(val >= n) { flag = 1;}
            for(i=2; i<2+val ; i++)
            {
                if( --lake[i].w < 0) { flag = 1; break;}
                map[lake[1].pos][lake[i].pos] = 
                map[lake[i].pos][lake[1].pos] = 1;
            }
        }
        if(!flag)
        {
            printf("YES\n");
            for(i=1; i<=n; i++)
            {
                for(j=1; j<n; j++)
                    printf("%d ", map[i][j]);
                printf("%d\n", map[i][n]);
            }
        }
        else
            printf("NO\n");
        printf("\n");
    }
    return 0;
}

 

 

posted @ 2013-02-28 09:59  April_Tsui  阅读(125)  评论(0编辑  收藏  举报