POJ 1325 Machine Schedule 二分图最大匹配

把每一个任务看做一个边,机器的模式看做是一个点,这个其实就是求一个最少点覆盖所有边即最小点覆盖集的问题,因为最小点覆盖集=二分图的最大匹配,所以问题转化成了求二分图最大匹配问题。

第一次写二分图匹配,感觉建模还是相当困难的。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef long long LL;
const int maxn = 200;
bool g[maxn][maxn];
int nx,ny,m;
int bx[maxn],by[maxn];
bool vis[maxn];

int path(int now) {
    for(int i = 1;i <= ny;i++) if(g[now][i] && !vis[i]) {
        vis[i] = true;
        if(!by[i] || path(by[i])) {
            bx[now] = i; by[i] = now;
            return 1;
        }
    }
    return 0;
}

void solve() {
    memset(bx,0,sizeof(bx));
    memset(by,0,sizeof(by));
    int ans = 0;
    for(int i = 1;i <= nx;i++) {
        if(!bx[i]) {
            memset(vis,0,sizeof(vis));
            ans += path(i);
        }
    }
    printf("%d\n",ans);
}

int main() {
    while(scanf("%d",&nx),nx) {
        scanf("%d%d",&ny,&m);
        memset(g,0,sizeof(g));
        for(int i = 0;i < m;i++) {
            int a,b,c; scanf("%d%d%d",&a,&b,&c);
            g[b][c] = 1;
        }
        solve();
    }
    return 0;
}

  

POJ 1325 Machine Schedule 二分图最大匹配

时间: 2024-08-07 20:26:04

POJ 1325 Machine Schedule 二分图最大匹配的相关文章

POJ 1325 Machine Schedule (二分图最小点集覆盖 匈牙利算法)

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12621   Accepted: 5399 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

POJ - 1325 Machine Schedule 二分图 最小点覆盖

题目大意:有两个机器,A机器有n种工作模式,B机器有m种工作模式,刚开始两个机器都是0模式,如果要切换模式的话,机器就必须的重启 有k个任务,每个任务都可以交给A机器的i模式或者B机器的j模式完成,问要重启多少次机器才能完成任务 解题思路:两个机器的点分为两个点集,点集之间的关系就是任务了,要将所有任务都完成,就要将所有边都覆盖掉,所以就是求最小点覆盖了. 这里有一个点要注意,如果所有任务中都有一个0,那么机器就不用重启了,重启次数就为0了(因为刚开始都是0) #include<cstdio>

poj 1325 Machine Schedule 二分图匹配+DFS实现

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <ma

poj 1325 Machine Schedule 解题报告

题目链接:http://poj.org/problem?id=1325 题目意思:有 k 个作业,机器A有 n 个模式:0 ~ n-1,机器B 有 m 个模式:0~ m-1.每一个作业能运行在 A 的 某一个模式(假设为 i (0 <= i <= n-1 ) )或 B 的某一个模式下(j (0 <= j <= m-1)).多个作业可以同时运行在 A 的某一个 模式下,当然 B 也如此.每对A 或 B 转换一次模式,就要重启一次 A 或者 B,你需要选择A 或 B 的一些模式,使得所

POJ 1325 Machine Schedule【最小点覆盖】

E - Machine Schedule Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1325 Appoint description:  System Crawler  (2014-08-10) Description As we all know, machine scheduling is a very classical pr

POJ 1325 Machine Schedule——S.B.S.

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13731   Accepted: 5873 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

poj 1325 Machine Schedule

Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u   Java class name: Main [Submit] [Status] [Discuss] Description As we all know, machine scheduling is a very classical problem in computer science and has been studied

poj 1325 Machine Schedule 题解

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14479   Accepted: 6172 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

ZOJ 1364 Machine Schedule(二分图最大匹配)

题意 机器调度问题 有两个机器A,B A有n种工作模式0...n-1 B有m种工作模式0...m-1 然后又k个任务要做 每个任务可以用A机器的模式i或b机器的模式j来完成 机器开始都处于模式0 每次换模式时都要重启 问完成所有任务机器至少重启多少次 最基础的二分图最大匹配问题 对于每个任务把i和j之间连一条边就可以构成一个二分图 那么每个任务都可以对应一条边 那么现在就是要找最少的点 使这些点能覆盖所有的边 即点覆盖数 又因为二分图的点覆盖数 = 匹配数 那么就是裸的求二分图最大匹配问题了 两