POJ 3041 Asteroids HDU 1150 Machine Schedule (最小顶点覆盖)

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

链接: HDU 1150 http://acm.hdu.edu.cn/showproblem.php?pid=1150


POJ 3041 :

建立二分图,X集合为行,Y为列,小行星的位置(x,y)就在二分图里建一条边x->y,要求的是在X Y集合里找到最少的点,使得所有的边都至少连接一个顶点。

目的就是求最小顶点覆盖。二分图里   最小顶点覆盖 = 最大匹配


HDU 1150:

模型很明显就是最小顶点覆盖,由于0是不需要消耗的 所以不必把0建在图里。


poj3041:代码:(二分图匹配模板)


/*--------------------- #headfile--------------------*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
/*----------------------#define----------------------*/
#define DRII(X,Y) int (X),(Y);scanf("%d%d",&(X),&(Y))
#define EXP 2.7182818284590452353602874713527
#define CASET int _;cin>>_;while(_--)
#define RII(X, Y) scanf("%d%d",&(X),&(Y))
#define DRI(X) int (X);scanf("%d", &X)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,n) for(int i=0;i
#define rson o<<1|1,m+1,r
#define PII pair
#define MAX 0x3f3f3f3f
#define lson o<<1,l,m
#define MP make_pair
#define PB push_back
#define SE second
#define FI first
typedef long long ll;
templateT MUL(T x,T y,T P){T F1=0;while(y){if(y&1){F1+=x;if(F1<0||F1>=P)F1-=P;}x<<=1;if(x<0||x>=P)x-=P;y>>=1;}return F1;}
templateT POW(T x,T y,T P){T F1=1;x%=P;while(y){if(y&1)F1=MUL(F1,x,P);x=MUL(x,x,P);y>>=1;}return F1;}
templateT gcd(T x,T y){if(y==0)return x;T z;while(z=x%y)x=y,y=z;return y;}
#define DRIII(X,Y,Z) int (X),(Y),(Z);scanf("%d%d%d",&(X),&(Y),&(Z))
#define RIII(X,Y,Z) scanf("%d%d%d",&(X),&(Y),&(Z))
const double pi = acos(-1.0);
const double eps = 1e-8;
const ll mod = 1000000007ll;
const int M = 100005;
const int N = 1005;
using namespace std;

/*----------------------Main-------------------------*/

int X, Y;
int g[N][N];
int mc[N];
bool used[N];

bool dfs(int u) {
    for(int v = 0; v < Y; v++) { // 0 start
        if(g[u][v] && !used[v]) {
            used[v] = 1;
            if(mc[v] == -1 || dfs(mc[v])) {
                mc[v] = u;
                return 1;
            }
        }
    }
    return 0;
}
int Match() {
    int res = 0;
    mem(mc, -1);
    for(int u = 0; u < X; u++) { // 0 start
        mem(used, 0);
        if(dfs(u)) res++;
    }
    return res;
}
int k;
void solve() {
    while(RII(X, k) != EOF) {
        mem(g, 0);
        Y = X;
        while(k--) {
            DRII(x, y);
            x--, y--;
            g[x][y] = 1;
        }
        printf("%d\n", Match());
    }
}

int main() {
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt","w",stdout);
//    CASET
    solve();
    return 0;
}

Hdu 1150

/*--------------------- #headfile--------------------*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
/*----------------------#define----------------------*/
#define DRII(X,Y) int (X),(Y);scanf("%d%d",&(X),&(Y))
#define EXP 2.7182818284590452353602874713527
#define CASET int _;cin>>_;while(_--)
#define RII(X, Y) scanf("%d%d",&(X),&(Y))
#define DRI(X) int (X);scanf("%d", &X)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,n) for(int i=0;i
#define rson o<<1|1,m+1,r
#define PII pair
#define MAX 0x3f3f3f3f
#define lson o<<1,l,m
#define MP make_pair
#define PB push_back
#define SE second
#define FI first
typedef long long ll;
templateT MUL(T x,T y,T P){T F1=0;while(y){if(y&1){F1+=x;if(F1<0||F1>=P)F1-=P;}x<<=1;if(x<0||x>=P)x-=P;y>>=1;}return F1;}
templateT POW(T x,T y,T P){T F1=1;x%=P;while(y){if(y&1)F1=MUL(F1,x,P);x=MUL(x,x,P);y>>=1;}return F1;}
templateT gcd(T x,T y){if(y==0)return x;T z;while(z=x%y)x=y,y=z;return y;}
#define DRIII(X,Y,Z) int (X),(Y),(Z);scanf("%d%d%d",&(X),&(Y),&(Z))
#define RIII(X,Y,Z) scanf("%d%d%d",&(X),&(Y),&(Z))
const double pi = acos(-1.0);
const double eps = 1e-8;
const ll mod = 1000000007ll;
const int M = 100005;
const int N = 1005;
using namespace std;

/*----------------------Main-------------------------*/

int X, Y;
int g[N][N];
int mc[N];
bool used[N];

bool dfs(int u) {
    for(int v = 0; v < Y; v++) { // 0 start
        if(g[u][v] && !used[v]) {
            used[v] = 1;
            if(mc[v] == -1 || dfs(mc[v])) {
                mc[v] = u;
                return 1;
            }
        }
    }
    return 0;
}
int Match() {
    int res = 0;
    mem(mc, -1);
    for(int u = 0; u < X; u++) {
        mem(used, 0);
        if(dfs(u)) res++;
    }
    return res;
}
int k;
void solve() {
    while(RI(X) && X > 0) {
        RII(Y, k);
        mem(g, 0);
        while(k--) {
            DRIII(id, x, y);
            if(x > 0 && y > 0) g[x][y] = 1;
        }
        printf("%d\n", Match());
    }
}

int main() {
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt","w",stdout);
//    CASET
    solve();
    return 0;
}


你可能感兴趣的:(ACM__图论)