二分图 x 覆盖集 POJ - 1325Machine Schedule POJ - 2226Muddy Fields

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niiick/article/details/82985027

二分图最小点覆盖集

定义:

在二分图中求出一个最小点集
使得图中任意一条边至少有一个端点在点集内
换句话说
若一个点能覆盖所有与他的连边
要求一个最小点集覆盖所有边

解法:

对二分图进行最大匹配
最大匹配数就是二分图的最小点覆盖集包含的点数


POJ - 1325 Machine Schedule

Time limit 1000 ms
Memory limit 10000 kB

As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling problems differ widely in the nature of the constraints that must be satisfied and the type of schedule desired. Here we consider a 2-machine scheduling problem.

There are two machines A and B. Machine A has n kinds of working modes, which is called mode_0, mode_1, …, mode_n-1, likewise machine B has m kinds of working modes, mode_0, mode_1, … , mode_m-1. At the beginning they are both work at mode_0.

For k jobs given, each of them can be processed in either one of the two machines in particular mode. For example, job 0 can either be processed in machine A at mode_3 or in machine B at mode_4, job 1 can either be processed in machine A at mode_2 or in machine B at mode_4, and so on. Thus, for job i, the constraint can be represent as a triple (i, x, y), which means it can be processed either in machine A at mode_x, or in machine B at mode_y.

Obviously, to accomplish all the jobs, we need to change the machine’s working mode from time to time, but unfortunately, the machine’s working mode can only be changed by restarting it manually. By changing the sequence of the jobs and assigning each job to a suitable machine, please write a program to minimize the times of restarting machines.

Input

The input file for this program consists of several configurations. The first line of one configuration contains three positive integers: n, m (n, m < 100) and k (k < 1000). The following k lines give the constrains of the k jobs, each line is a triple: i, x, y.

The input will be terminated by a line containing a single zero.

Output

The output should be one integer per line, which means the minimal times of restarting machine.


分析

把A机器的n个点作为二分图的左部,B机器的m个点作为右部
对于每个任务的两种模式 A x , B y A_x,B_y
左部的第x个点 与 右部的第y个点连边

这样每条边代表一个任务,每个点代表一种模式
题目要求用最少的模式完成所有任务
也就相当于用最少的点覆盖所有边
即这个二分图的最小点覆盖集


#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int inf=1e9;
const int maxn=510;
int n,m,k,s,t;
struct node{int v,f,nxt;}E[maxn*100];
int head[maxn],tot=1;
int lev[maxn],maxf;

void add(int u,int v,int f)
{
	E[++tot].nxt=head[u];
	E[tot].v=v; E[tot].f=f;
	head[u]=tot;
}

int bfs()
{
	queue<int> q; q.push(s);
	memset(lev,-1,sizeof(lev)); lev[s]=0;
	while(!q.empty())
	{
		int u=q.front(); q.pop();
		for(int i=head[u];i;i=E[i].nxt)
		{
			int v=E[i].v;
			if(E[i].f&&lev[v]==-1)
			{
				lev[v]=lev[u]+1;
				if(v==t) return 1;
				q.push(v);
			}
		}
	}
	return 0;
}

int dfs(int u,int cap)
{
	if(u==t) return cap;
	int flow=cap;
	for(int i=head[u];i;i=E[i].nxt)
	{
		int v=E[i].v;
		if(E[i].f&&lev[v]==lev[u]+1&&flow)
		{
			int f=dfs(v,min(E[i].f,flow));
			flow-=f;
			E[i].f-=f; E[i^1].f+=f;
		}
	}
	return cap-flow;
}

void init()
{
	maxf=0; tot=1;
	memset(head,0,sizeof(head)); 
}

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0) break; init();
		m=read();k=read();t=n+m+1;
		
		for(int i=1;i<=n;++i)
		add(s,i,1),add(i,s,0);
		for(int i=1;i<=m;++i)
		add(i+n,t,1),add(t,i+n,0);
		
		for(int i=1;i<=k;++i)
		{
			int num=read(),x=read(),y=read();
			if(x*y==0) continue;
			add(x,y+n,inf); add(y+n,x,0);
		}
		
		while(bfs()) 
		maxf+=dfs(s,inf);
		
		printf("%d\n",maxf);
	}
	return 0;
}

POJ - 2226 Muddy Fields

Time limit 1000 ms
Memory limit 65536 kB

Rain has pummeled the cows’ field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don’t want to get their hooves dirty while they eat.

To prevent those muddy hooves, Farmer John will place a number of wooden boards over the muddy parts of the cows’ field. Each of the boards is 1 unit wide, and can be any length long. Each board must be aligned parallel to one of the sides of the field.

Farmer John wishes to minimize the number of boards needed to cover the muddy spots, some of which might require more than one board to cover. The boards may not cover any grass and deprive the cows of grazing area but they can overlap each other.

Compute the minimum number of boards FJ requires to cover all the mud in the field.

Input

  • Line 1: Two space-separated integers: R and C

  • Lines 2…R+1: Each line contains a string of C characters, with ‘*’ representing a muddy patch, and ‘.’ representing a grassy patch. No spaces are present.

Output

  • Line 1: A single integer representing the number of boards FJ needs.

分析

首先假设只能横着放木板
只用横着的模板覆盖所有泥地后,给这些木板标号
再假设只能放纵木板,以同样方法铺完后标号

以样例为例
*.*.
.***
***.
..*.

只放横木板
1 . 2 .
. 3 3 3
4 4 4 .
. . 5 .
只放纵木板
1 . 4 .
. 3 4 5
2 3 4 .
. . 4 .

横着铺的木板作为二分图左部,纵着的作为右部
对于一块泥地,假设能覆盖他的横木板编号为 i i ,能覆盖他的纵木板编号为 j j
那么在二分图中 i , j i,j 两个节点间连边

这样每条边代表一个泥地,每个点代表一块木板
要用最少的木板覆盖所有泥地,即为这个二分图的最小点覆盖集


#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int inf=1e9;
const int maxn=5010;
int n,m,s,t;
int a[maxn][maxn],cnt_row,cnt_col;
int row[maxn][maxn],col[maxn][maxn];
struct node{int v,f,nxt;}E[maxn*100];
int head[maxn],tot=1;
int lev[maxn],maxf;
char ss[110];

void add(int u,int v,int f)
{
	E[++tot].nxt=head[u];
	E[tot].v=v; E[tot].f=f;
	head[u]=tot;
}

int bfs()
{
	queue<int> q; q.push(s);
	memset(lev,-1,sizeof(lev)); lev[s]=0;
	while(!q.empty())
	{
		int u=q.front(); q.pop();
		for(int i=head[u];i;i=E[i].nxt)
		{
			int v=E[i].v;
			if(E[i].f&&lev[v]==-1)
			{
				lev[v]=lev[u]+1;
				if(v==t) return 1;
				q.push(v);
			}
		}
	}
	return 0;
}

int dfs(int u,int cap)
{
	if(u==t) return cap;
	int flow=cap;
	for(int i=head[u];i;i=E[i].nxt)
	{
		int v=E[i].v;
		if(E[i].f&&lev[v]==lev[u]+1&&flow)
		{
			int f=dfs(v,min(E[i].f,flow));
			flow-=f;
			E[i].f-=f; E[i^1].f+=f;
		}
	}
	return cap-flow;
}

int main()
{
	n=read();m=read();
	for(int i=1;i<=n;++i)
	{
		scanf("%s",&ss);
		for(int j=0;j<m;++j)
		a[i][j+1]=(ss[j]=='*')?0:1;
	}
	
	for(int i=1;i<=n;++i)
	for(int j=1;j<=m;++j)
	if(a[i][j]==0){
		cnt_row++;
		while(j<=m&&a[i][j]==0){ row[i][j]=cnt_row; ++j;}
	}
	
	for(int j=1;j<=m;++j)
	for(int i=1;i<=n;++i)
	if(a[i][j]==0){
		cnt_col++;
		while(i<=n&&a[i][j]==0){ col[i][j]=cnt_col; ++i;}
	}
	
	t=cnt_row+cnt_col+1;
	for(int i=1;i<=cnt_row;++i)
	add(s,i,1),add(i,s,0);
	for(int i=1;i<=cnt_col;++i)
	add(i+cnt_row,t,1),add(t,i+cnt_row,0);
	
	for(int i=1;i<=n;++i)
	for(int j=1;j<=m;++j)
	if(a[i][j]==0)
	add(row[i][j],col[i][j]+cnt_row,inf),
	add(col[i][j]+cnt_row,row[i][j],0);
	
	while(bfs())
	maxf+=dfs(s,inf);
	
	printf("%d",maxf);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/82985027