Poj Solution 2060

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

#include <memory.h>
#include <stdio.h>
#include <math.h>

#define null 0
#define y1 yy1
const int size = 510;
//size must be bigger than n and m.
//nС��mʱЧ�ʸ�
int maxmatch( int n, int m, bool w[][size], int *p)
{

    int p_n[size];
    int p_m[size];
    bool sign[size];
    int q[size],from[size],s,t;
    int i,j,link,now,h;
    memset( p_n, -1, sizeof(int)*n );
    memset( p_m, -1, sizeof(int)*m );
    for(i=0;i<n;i++)
    if(p_n[i]==-1)
    {
        memset( sign, 0, sizeof(bool)*m );
                s=1;link=-1;
        from[0]=-1;
        q[0]=size-1;
        p_m[size-1]=i;
        for(t=0;t<s;t++)
        {
           now=q[t];
           for(j=0;j<m;j++)
           {
               if( w[p_m[now]][j] != null && sign[j]==0 )
               {
                    sign[j]=1;
                    q[s]=j;
                    from[s++]=t;

                    if(p_m[j]==-1)
                    {
                        link=s-1;
                        break;
                    }
               }
           }
           if(j<m)break;
        }
        if(t<s)
        {
             while(from[link]!=-1)
             {
                    h=from[link];
                    p_m[q[link]]=p_m[q[h]];
                    p_n[p_m[q[h]]]=q[link];
                    link=h;
             }
        }

        //���걸ƥ��
        //else return 0;
    }
    int an;
    for(i=0,an=0;i<n;i++)
    {
        if(p)p[i]=p_n[i];
        if(p_n[i]>=0)an++;
    }
    return an;
}
bool e[size][size];
int begin[size], end[size];
int x1[size], y1[size], x2[size], y2[size], n;
inline int dis( int x1, int y1, int x2, int y2 )
{
    return abs( x1 - x2 ) + abs( y1 - y2 );
}
int main()
{
    int cas, n, i, j, a, b;    
    scanf( "%d", &cas );
    while( cas-- )
    {
        memset( e, 0, sizeof e );
        scanf( "%d", &n );
        for( i=0; i<n; i++ )
        {
            scanf( "%d:%d %d %d %d %d", &a, &b, x1+i, y1+i, x2+i, y2+i );
            begin[i] = a*60 + b;
            end[i] = begin[i] + dis( x1[i], y1[i], x2[i], y2[i] );
        }
        for( i=0; i<n; i++ )
        for( j=i+1; j<n; j++ )
            if( end[i] + dis( x2[i], y2[i], x1[j], y1[j] ) < begin[j] )
                e[i][j] = true;
        printf( "%dn", n - maxmatch( n, n, e, 0 ) );
    }
    return 0;
}
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *