Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么会超时呢?是不是模板有问题(程序如下)

Posted by xinghe at 2006-05-17 22:24:43 on Problem 2483
#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;
const int INF=100;  
#define min(a,b) a<b?a:b
#define max(a,b) a>b?a:b
vector<int> v1[51],v2[51];
bool DFS(int p,int n,int *match,bool map[][51],bool *xckd,bool *yckd);
void KM(int n,int edge[][51],int *match,bool map[][51],bool *xckd,bool *yckd) 
{ 
   int i,j; 
   int lx[51],ly[51];   
   for(i=1;i<=n;i++)
   { 
      lx[i]=-INF; 
      ly[i]=0; 
      for(j=1;j<=n;j++)
	  { 
         lx[i]=max(lx[i],edge[i][j]); 
      } 
   } 
    bool perfect=false; 
    while(!perfect) 
	{ 
     
        for(i=1;i<=n;i++)
		{ 
            for(j=1;j<=n;j++) 
			{ 
				if(lx[i]+ly[j]==edge[i][j]) map[i][j]=true; 
                else map[i][j]=false; 
         } 
      } 
	  int live=0; 
      for(i=0;i<=n;i++) match[i]=-1;
      for(i=1;i<=n;i++)
	  { 
		 memset(xckd,false,sizeof(xckd)); 
         memset(yckd,false,sizeof(yckd)); 
         if(DFS(i,n,match,map,xckd,yckd)) live++; 
         else 
		 { 
            xckd[i]=true; 
            break; 
         } 
      } 
      if(live==n) perfect=true; 
      else 
	  { 
         
         int ex=INF; 
         for(i=1;i<=n;i++)
		 { 
            for(j=1;xckd[i]&&j<=n;j++)
			{ 
               if(!yckd[j]) ex=min(ex,lx[i]+ly[j]-edge[i][j]); 
            } 
         } 
         for(i=1;i<=n;i++)
		 { 
            if(xckd[i]) lx[i]-=ex; 
            if(yckd[i]) ly[i]+=ex; 
         } 
      } 
   } 
} 
bool DFS(int p,int n,int *match,bool map[][51],bool *xckd,bool *yckd)
{ 
   int i; 
   for(i=1;i<=n;i++)
   { 
      if(!yckd[i]&&map[p][i])
	  { 
         yckd[i]=true; 
         int t=match[i]; 
         match[i]=p; 
         if(t==-1||DFS(t,n,match,map,xckd,yckd))
		 { 
            return true; 
         } 
         match[i]=t; 
         if(t!=-1) xckd[t]=true; 
      } 
   } 
   return false; 
} 
int search(int x,int y)
{
//	if(v1[x].size()==0||v2[y].size()==0) return 1;
	int edge[51][51];
	bool map[51][51];         
    bool xckd[51], yckd[51];  
    int match[51];             
	int n,m,i,k;
	int count,x1,y1;
	n=v1[x].size();
	m=v2[y].size();
	if(n==0||m==0) return 1;
	memset(edge,0,sizeof(edge));
	for(i=0;i<n;i++)
	{
		x1=v1[x][i];
		for(k=0;k<m;k++)
		{
			y1=v2[y][k];
			edge[i+1][k+1]=search(x1,y1);
		}
	}
	KM(n,edge,match,map,xckd,yckd); 
	count=0;
	for(i=1;i<=n;i++)
	{ 
		count+=edge[match[i]][i]; 
	}
	return count+1;
}
int main()
{
	int n,m,a,b,i;
	while((scanf("%d %d",&n,&m))!=-1)
	{
		for(i=1;i<=n-1;i++)
		{
			scanf("%d %d",&a,&b);
			v1[b].push_back(a);
		}
		for(i=1;i<=m-1;i++)
		{
			scanf("%d %d",&a,&b);
			v2[b].push_back(a);
		}
		printf("%d\n",search(1,1));
		for(i=1;i<=n;i++) v1[i].clear();
		for(i=1;i<=m;i++) v2[i].clear();
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator