【图论】【二分图匹配】[POJ 1325]Machine Schedule

可以发现如果把一条边看作一个任务,那么连接的就是两台机器的模式的编号,那么问题就转化成了最小的点覆盖问题,那么注意特殊处理存在一个任务在任意一台机器中是0的情况,因为一开始大家都是0,那么如果存在一个0直接不考虑这个任务就行了。

#include <cstdio>
#include <cstring>
#include <algorithm>
//#include <conio.h>
using namespace std;
const int MAXN = 400;
bool vis[MAXN+10];
int con[MAXN+10];
int endcnt;
int n, m, k;
struct node{
    int v;
    node *next;
}Edges[2000*2+10], *adj[MAXN+10], *ecnt=Edges;
void addedge(int u, int v){
    ++ecnt;
    ecnt->v = v;
    ecnt->next = adj[u];
    adj[u] = ecnt;
}
bool dfs(int u){
    for(node *p=adj[u];p;p=p->next){
        if(!vis[p->v]){
            vis[p->v] = true;
            if(con[p->v]==-1 || dfs(con[p->v])){
                con[u] = p->v;
                con[p->v] = u;
                return true;
            }
        }
    }
    return false;
}
int work(){
    int ret = 0;
    memset(con, -1, sizeof con);
    for(int i=1;i<endcnt;i++) if(con[i] == -1){
        memset(vis, 0, sizeof vis);
        ret += dfs(i);
    }
    return ret;
}
bool read(){
    int t1, t2, t3;
    scanf("%d", &n);
    if(!n) return false;
    scanf("%d%d", &m, &k);
    for(int i=0;i<k;i++){
        scanf("%d%d%d", &t1, &t2, &t3);
        if(!t2) continue;
        if(!t3) continue;
        addedge(t2, t3+n);
        addedge(t3+n, t2);
    }
    endcnt=n;
    return true;
}
void prepare(){
    memset(adj, 0, sizeof adj);
    ecnt=Edges;
}
int main(){
    while(read()){
        printf("%d\n", work());
        prepare();
    }
    return 0;
}

posted on 2015-07-28 15:57  JeremyGuo  阅读(137)  评论(0编辑  收藏  举报

导航