POJ 1325 Machine Schedule 最小点覆盖

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

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.

Sample Input

5 5 10
0 1 1
1 1 2
2 1 3
3 1 4
4 2 1
5 2 2
6 2 3
7 2 4
8 3 3
9 4 3
0

Sample Output

3

题目大意:机器A有0、1……n-1共n种工作模式,机器B有0、1……m-1共m种工作模式,一个三元组(i,x,y)表示任务i可以在机器A的x模式下完成或在机器B的y模式下完成,机器每次更换工作模式都需要时间来重启,你可以任意调整任务的顺序,问最少需要重启机器的次数。

思路:因为两台机器初始的工作模式都是0,因此任何满足x=0或y=0的三元组我们都用不管。把其他的看成一个二分图,对于x!=0且y!=0的三元组我们给x、y连上一条边,问题就转换成了求该二分图的最小点覆盖,即求最大匹配。

匈牙利:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=205;

int n,m,k;
int s[maxn][maxn];
int pre[maxn];
int vis[maxn];

bool dfs(int x)
{
    for(int i=1;i<m;i++)
    {
        if(vis[i]==0&&s[x][i])
        {
            vis[i]=1;
            if(pre[i]==-1||dfs(pre[i]))
            {
                pre[i]=x;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    while(~scanf("%d",&n)&&n)
    {
        memset(s,0,sizeof(s));
        memset(pre,-1,sizeof(pre));
        scanf("%d%d",&m,&k);
        int t,u,v;
        for(int i=0;i<k;i++)
        {
            scanf("%d %d %d",&t,&u,&v);
            if(u==0||v==0)
                continue;
            s[u][v]=1;
        }
        int ans=0;
        for(int i=1;i<n;i++)
        {
            memset(vis,0,sizeof(vis));
            ans+=dfs(i);
        }
        printf("%d\n",ans);
    }
    return 0;
}

Dinic:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=1005;
const int maxm=10005;
const int inf=0x3f3f3f3f;

struct Edge
{
    int to,nxt,cap,flow;
}edge[maxm];
int tol;
int head[maxn];
int Q[maxn];
int dep[maxn],cur[maxn],sta[maxn];

void init()
{
    tol=2;
    memset(head,-1,sizeof(head));
}

void AddEdge(int u,int v,int w,int rw=0)//链式前向星存图
{
    edge[tol].to=v;edge[tol].cap=w;edge[tol].flow=0;
    edge[tol].nxt=head[u];head[u]=tol++;
    edge[tol].to=u;edge[tol].cap=rw;edge[tol].flow=0;//剩余网络
    edge[tol].nxt=head[v];head[v]=tol++;
}

bool bfs(int s,int t,int n)//建立层次网络
{
    int front=0,tail=0;
    memset(dep,-1,sizeof(dep));
    dep[s]=0;   //源点
    Q[tail++]=s;
    while(front<tail) //队列
    {
        int u=Q[front++]; //起点
        for(int i=head[u];i!=-1;i=edge[i].nxt)
        {
            int v=edge[i].to; //终点
            if(edge[i].cap>edge[i].flow&&dep[v]==-1)//除去饱和的弧
            {
                dep[v]=dep[u]+1;
                if(v==t)   //汇点
                    return true;
                Q[tail++]=v;
            }
        }
    }
    return false;
}

int dinic(int s,int t,int n)
{
    int maxflow=0;
    while(bfs(s,t,n))
    {
        for(int i=0;i<n;i++)
            cur[i]=head[i]; //边
        int u=s,tail=0; //u 点
        while(cur[s]!=-1) //源点还有邻点
        {
            if(u==t)//汇点
            {
                int tp=inf;
                for(int i=tail-1;i>=0;i--) //计算 找到的增广路的 流量
                    tp=min(tp,edge[sta[i]].cap-edge[sta[i]].flow);
                maxflow+=tp;
                for(int i=tail-1;i>=0;i--)
                {
                    edge[sta[i]].flow+=tp;
                    edge[sta[i]^1].flow-=tp; //剩余网络
                    if(edge[sta[i]].cap-edge[sta[i]].flow==0) tail=i; //饱和
                }
                u=edge[sta[tail]^1].to; //回退
            }
            else if(cur[u]!=-1&&edge[cur[u]].cap>edge[cur[u]].flow&&dep[u]+1==dep[edge[cur[u]].to]) //找增广路
            {
                sta[tail++]=cur[u]; //记录边的编号
                u=edge[cur[u]].to;
            }
            else
            {
                while(u!=s&&cur[u]==-1)//不是源点且无路可走
                    u=edge[sta[--tail]^1].to; //回退
                cur[u]=edge[cur[u]].nxt;
            }
        }
    }
    return maxflow;
}
int main()
{
    int n,m,k;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%d %d",&m,&k);
        init();
        int te,u,v;
        int s=0,t=n+m-1;
        for(int i=1;i<n;i++)
            AddEdge(s,i,1);
        for(int i=n;i<t;i++)
            AddEdge(i,t,1);
        for(int i=0;i<k;i++)
        {
            scanf("%d %d %d",&te,&u,&v);
            if(u==0||v==0)
                continue;
            AddEdge(u,v+n-1,1);
        }
        printf("%d\n",dinic(s,t,t+1));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值