poj 1422 Air Raid 最小路径覆盖

对于路径覆盖概念请点击以下链接:

  Path Cover

 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

const int N = 150;
int n, m;

int ma[N],mb[N];
bool g[N][N], vis[N];

int path( int u ){
    for(int v = 1; v <= n; v++){
        if( g[u][v] && !vis[v] ){
            vis[v] = 1;
            if( ma[v] == -1 || path( ma[v] )){
                ma[v] = u; mb[u] = v;
                return 1;    
            }    
        }    
    }    
    return 0;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d", &n,&m);
        memset( g, 0, sizeof(g));
        int a, b;
        for(int i = 0; i < m; i++){
            scanf("%d%d", &a,&b);
            g[a][b] = 1;
        }
        memset( ma, 0xff, sizeof(ma));
        memset( mb, 0xff, sizeof(mb));
        int ans = 0;
        for(int i = 1; i <= n; i++){
            if( mb[i] == -1 ){
                memset( vis, 0, sizeof(vis));
                ans += path( i );    
            }    
        } 
        printf("%d\n", n-ans);
    }
    return 0;    
}

 

posted @ 2013-04-03 19:41  yefeng1627  阅读(191)  评论(0编辑  收藏  举报

Launch CodeCogs Equation Editor