POJ 1325 Machine Schedule

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

链接:http://poj.org/problem?id=1325

题意:有A、B两种机器,分别有n、m种(0~n-1,0~m-1)模式,k项工作,每项工作可以由A的第i种模式或者B的第j种模式来完成,机器由某一状态转化为其他状态时需要花费一定的时间,问完成所有工作所需要的最少的机器启动次数。

思路:很显然,A机器的n种模式作为左节点,B的m种模式作为右节点,同时因为机器一开始均在0模式,所以可以首先完成那些可以由0模式完成的工作,其次k条边连接对应的A、B中的两种模式,然后就很显然是求将所有边覆盖的最小点集。

AC代码:(注意0~n-1,且0模式不花费额外时间)

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include<algorithm>
#include <set>
#include <queue>
#include <stack>
#include<vector>
#include<map>
#include<ctime>
#define ll long long
using namespace std;
const int N=210;
const int M=1010*2;
int Next[M];
int ver[M];
int head[N];
int tot;
void add(int x,int y)
{
    ver[++tot]=y;
    Next[tot]=head[x];
    head[x]=tot;
}
bool visit[N];
int match[N];
bool dfs(int x)
{
    for(int i=head[x];i;i=Next[i])
    {
        int y=ver[i];
        if(!visit[y])
        {
            visit[y]=1;
            if(!match[y]||dfs(match[y]))//对立点没有被使用,或者占用它的左节点可以
            {
                match[y]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int n,m,k;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%d%d",&m,&k);
        memset(head,0,sizeof(head));
        memset(Next,0,sizeof(Next));
        memset(ver,0,sizeof(ver));
        tot=0;
        memset(match,0,sizeof(match));
        for(int i=1;i<=k;++i)
        {
            int x,y,tt;
            scanf("%d%d%d",&tt,&x,&y);
            if(x==0||y==0)continue;
            add(x,y+n);
            add(y+n,x);
        }
        int ans=0;
        for(int i=0;i<n;++i)//遍历每一个左部节点,看能不能找到其在右边的对应匹配节点
        {
            memset(visit,0,sizeof(visit));//visit只是一个临时标记,只在单一节点为起点的搜索中起作用,搜一个节点更新一次;
            if(dfs(i))ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

The end;

猜你喜欢

转载自blog.csdn.net/qq_41661919/article/details/86444389