Escape(匈牙利算法变形)

来源:互联网 发布:好老师淘宝网 编辑:程序博客网 时间:2024/05/05 04:22

Link:http://acm.hdu.edu.cn/showproblem.php?pid=3605


Problem:

Escape

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5145    Accepted Submission(s): 1346


Problem Description
2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.
 

Input
More set of test data, the beginning of each data is n (1 <= n <= 100000), m (1 <= m <= 10) n indicate there n people on the earth, m representatives m planet, planet and people labels are from 0. Here are n lines, each line represents a suitable living conditions of people, each row has m digits, the ith digits is 1, said that a person is fit to live in the ith-planet, or is 0 for this person is not suitable for living in the ith planet.
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000
 

Output
Determine whether all people can live up to these stars
If you can output YES, otherwise output NO.
 

Sample Input
1 1112 21 01 01 1
 

Sample Output
YESNO
 


#include<iostream>  #include<cstdio>  #include<list>  #include<algorithm>  #include<cstring>  #include<string>  #include<queue>  #include<stack>  #include<map>  #include<vector>  #include<cmath>  #include<memory.h>  #include<set>     #define ll long long  #define LL __int64  #define eps 1e-8  const ll INF=9999999999999;     using namespace std;     #define M 400000100     #define inf 0xfffffff     //vector<pair<int,int> > G;  //typedef pair<int,int> P;  //vector<pair<int,int>> ::iterator iter;  //  //map<ll,int>mp;  //map<ll,int>::iterator p;     //vector<int>G[8000];     int mp[100012][22];  int num[22];  int marry[22];  bool vis[22];  int tempmp[22][100012];     int dis[2][4]={0,-1,0,1,1,0,-1,0};     int n,m,k;     void clear()  {      memset(tempmp,0,sizeof(tempmp));      memset(marry,0,sizeof(marry));      memset(num,0,sizeof(num));      memset(vis,false,sizeof(vis));      memset(mp,0,sizeof(mp));      /*for(int i=0;i<=n;i++)         G[i].clear();*/ }     bool dfs(int x)  {      for(int i=0;i<m;i++)      {          if(mp[x][i] && !vis[i])          {              vis[i]=true;              if(marry[i]<num[i])//这里的标记数组不一样了,不是之前的简单以为就能解决的,要是一个二维的,而且要遵循星球所住人数有限的限定              {                  tempmp[i][marry[i]++]=x;//这里的marry数组跟以前意义大不一样,marry[i]=j,就是i跟左集合中的点匹配了j次                  return 1;              }              for(int j=0;j<marry[i];j++)//比如marry[i]=j,要在i与各个点匹配中去寻找,              {                  if(dfs(tempmp[i][j]))  //给tempmp[i][j]找新的星球                 {                      tempmp[i][j]=x;                      return 1;                  }              }          }      }      return 0;  }     int main(void)  {      while(cin>>n>>m)      {          clear();          for(int i=0;i<n;i++)          {              for(int j=0;j<m;j++)              {                  scanf("%d",&mp[i][j]);              }          }          for(int i=0;i<m;i++)              scanf("%d",&num[i]);//因为星球住的人数有限所以要设一个数组来限制          int ans=0;          for(int i=0;i<n;i++)          {              memset(vis,false,sizeof(vis));              if(!dfs(i))              {                  ans=1;//本来这里用的是常用的ans++,如果ans==n就输出YES,不然就输出NO,可是那样超时了,现在直接标记,有出现不可匹配的直接输出NO,这样就过了,我觉得是题目数据有问题                  break;              }          }          if(ans)              puts("NO");          else             puts("YES");      }  } 


0 0
原创粉丝点击