POJ 1087 C++ (图论)

POJ 1087 C++ (图论)

//题目太难懂了,建模比较困难
//很多人把它做成二分图
匹配
//但貌似用最大流也能求出来
#include<iostream>
#include<string>
#include<map>
using namespace std;
int nr,np,na,flag,res,node;
int arr[402][402];
int q[1000],pre[1000],used[402];
map <string,int> PR;

int path(int s)
{ int u,head,tail,temp,i,j;
  head=tail=0;
  q[tail++]=s;
  used[s]=1;
  while(head<tail)
       { temp=tail;
         for(i=head;i<temp;i++)
             {    u=q[i];
                  if(u==1)
                     return 1;
                  for(j=0;j<node;j++)
                     if(used[j]==0 && arr[u][j]>0)
                        {pre[j]=u;
                         used[j]=1;
                         q[tail++]=j;
                         }
              }
          head=temp;
       }
  return 0;      
}


void  ford_fulkerson()
{ int i,j,u,v,min,x,y;
  min=INT_MAX;
  u=pre[1];
  v=1;
  while(u>=0)
       {if(arr[u][v]<min)
             min=arr[u][v];
            v=u;
            u=pre[u];
       }
    
  u=pre[1];
  v=1;
  while(u>=0)
       {   arr[u][v]=arr[u][v]-min;
           arr[v][u]=arr[v][u]+min;
           v=u;
           u=pre[u];
        }
   res=res+min;       
}       


int main()
{int u,v,w,i,j;
 char str1[25],str2[25];
       freopen("in.txt","r",stdin);
       freopen("out.txt","w",stdout);
         scanf("%d",&nr);
          flag=0;
          res=0;
          node=2;
          for(i=0;i<nr;i++)
             { scanf("%s",str1);
               if(!PR[str1])
                   PR[str1]=node++;
               arr[0][PR[str1]]++;
             }
        
         scanf("%d",&np);
         for(i=0;i<np;i++)
             {  scanf("%s%s",str1,str2);
                 if(!PR[str2])
                    PR[str2]=node++;
                arr[PR[str2]][1]++;
             }   
        
         scanf("%d",&na);
         for(i=0;i<na;i++)
             {   scanf("%s%s",str1,str2);
                 if(!PR[str1])
                    PR[str1]=node++;
                  if(!PR[str2])
                    PR[str2]=node++; 
                  arr[PR[str2]][PR[str1]]=INT_MAX;
              }    
          
         while(!flag)
             { memset(used,0,sizeof(used));
               memset(pre,-1,sizeof(pre));
               if(path(0))
                  ford_fulkerson();
                else
                  flag=1;
              }
   printf("%d\n",np-res);  
   return 0;
}   

你可能感兴趣的:(POJ 1087 C++ (图论))