zoj 1525 || poj 1422 Air Raid

    技术2022-05-20  37

    最小路径覆盖。

     

    有向图最小路径覆盖=|V| - 最大匹配数; 无向图最小路径覆盖=|V| - 最大匹配数/2。

     

    给你顶点和顶点,求最少的

     

    今晚在晚自习看了下黑书上的最小路径覆盖,最小路径 = n - 最大匹配边数。

     

    开始想麻烦了, = =。。。用 FOLYD 把边都给连上了,WA掉了,去了就A了,YM啊YM。。。

     

    #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h> #define MAX 150 using namespace std; int map[MAX][MAX]; int used[MAX],mat[MAX]; int Augment(int n,int x) { int i; for(i=1; i<=n; i++) if( !used[i] && map[x][i] ) { used[i] = 1; if( mat[i] == 0 || Augment(n,mat[i]) ) { mat[i] = x; return 1; } } return 0; } int Hungary(int n) { int i,sum = 0; memset(mat,0,sizeof(mat)); for(i=1; i<=n; i++) { memset(used,0,sizeof(used)); if( Augment(n,i) ) sum++; } return sum; } int main() { int ncases; int n,m,from,to,i,j,k; scanf("%d",&ncases); while( ncases-- ) { memset(map,0,sizeof(map)); scanf("%d%d",&n,&m); while( m-- ) { scanf("%d%d",&from,&to); map[from][to] = 1; } int ans = Hungary(n); printf("%d/n",n-ans); } return 0; }  


    最新回复(0)