Escape
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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 1
1
1
2 2
1 0
1 0
1 1
Sample Output
YES
NO
题目大意:有n个人准备去m个星球去逃难,但有些人对其中一些星球过敏,不能去这些星球,并且每个星球都限定难民数,让你求出这些人能否逃难成功?
题意很明确,求最大流。但是数据庞大,不经过缩点很容易超内存,超时。
AC代码:
# include <iostream>
# include <cstdio>
# include <queue>
# include <cstring>
using namespace std;
# define MAXN 100005
# define MAXM 2000005
# define INF 1 << 29
struct EDGE
{
int to;
int w;
int next;
}edge[MAXM];
int head[MAXN];
int tot;
int dis[MAXN];
int min(int a, int b)
{
return a > b ? b : a;
}
void Init()
{
tot = 0;
memset(head, -1, sizeof(head));
}
void Addedge(int u, int v, int w)
{
edge[tot].to = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
edge[tot].to = u;
edge[tot].w = 0;
edge[tot].next = head[v];
head[v] = tot++;
}
bool Bfs(int s, int t)
{
memset(dis, 0, sizeof(dis));
queue<int> que;
dis[s] = 1;
que.push(s);
while (!que.empty())
{
int u = que.front(); que.pop();
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (!dis[v] && edge[i].w > 0)
{
dis[v] = dis[u] + 1;
if (v == t)
{
return true;
}
que.push(v);
}
}
}
return false;
}
int Dfs(int u, int t, int f)
{
if (u == t)
{
return f;
}
int cost = 0;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
int w = edge[i].w;
if (dis[v] == dis[u] + 1 && w > 0)
{
int d = Dfs(v, t, min(w, f - cost));
if (d > 0)
{
edge[i].w -= d;
edge[i ^ 1].w += d;
cost += d;
if (cost == f)
{
break;
}
else
{
dis[v] = -1;
}
}
}
}
return cost;
}
int Dinic(int s, int t)
{
int maxflow = 0;
while (Bfs(s, t))
{
maxflow += Dfs(s, t, INF);
}
return maxflow;
}
void BuildGraph(int s, int t, int n, int m)
{
int i, j;
int num[MAXN];
memset(num, 0, sizeof(num));
//状态压缩
//进行缩点操作,把一个人可以去不同星球进行统计
//最后得出去可以一个星球多少人,可以去两个星球多少。。。。。
for(i=1;i<=n;i++)
{
int tmp=0;
for(j=1;j<=m;j++)
{
int a=0;
char c=getchar();
while(c>'9'||c<'0')
{
c=getchar();
}
a=c-'0';
tmp=tmp*2+a;
}
num[tmp]++;
}
for(i=1;i<=(1<<m)-1;i++)
{
if(num[i])
{
Addedge(0,i,num[i]);
int tmp=i;
for(j=m;j>=1;j--)
{
if(tmp&1)
{
Addedge(i,(1<<m)+j,INF);
}
tmp>>=1;
}
}
}
for(j=1;j<=m;j++)
{
int a=0;
char c=getchar();
while(c>'9'||c<'0')
{
c=getchar();
}
while(c>='0'&&c<='9')
{
a=a*10+c-'0';
c=getchar();
}
Addedge((1<<m)+j,m+(1<<m)+1,a);
}
}
void Solve(int s, int t, int pnum)
{
int max = Dinic(s, t);
//printf("max = %d\n", max);
int res = pnum - max;
//printf("res = %d\n", res);
if (!res)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
int main(void)
{
int n, m;
while (~scanf("%d %d", &n, &m))
{
int s = 0;
int t = (1 << m) + m + 1;
Init();
BuildGraph(s, t, n, m);
Solve(s, t, n);
}
return 0;
}
在网上看到有人用匈牙利算法也能通过:
先将代码收藏,以后研究
来自:http://www.cnblogs.com/nanke/archive/2012/04/15/2450185.html
# include<iostream>
# include <cstring>
# include<string>
using namespace std;
const int N = 100002;
int cap[12],map[N][12],vlink[12],link[12][N];
bool vis[12];
int n,m;
int in()
{
char ch;
int a = 0;
while((ch = getchar()) == ' ' || ch == '\n');
a += ch - '0';
while((ch = getchar()) != ' ' && ch != '\n')
{
a *= 10;
a += ch - '0';
}
return a;
}
int path(int s)
{
for(int i=0;i<m;i++)
{
if(map[s][i] && !vis[i])
{
vis[i]=true;
if(vlink[i]<cap[i])
{
link[i][vlink[i]++]=s;
return 1;
}
for(int j=0;j<vlink[i];j++)
{
if(path(link[i][j]))
{
link[i][j]=s;
return 1;
}
}
}
}
return 0;
}
int main()
{
while(scanf("%d %d",&n,&m)==2)
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
map[i][j]=in();
for(int i=0;i<m;i++)
scanf("%d",&cap[i]);
memset(vlink,0,sizeof(vlink));
bool flag=true;
for(int i=0;i<n;i++)
{
memset(vis,false,sizeof(vis));
if(!path(i))
{
flag=false;
break;
}
}
if(flag) puts("YES");
else puts("NO");
}
return 0;
}