poj 1422 Air Raid (DAG的最小路径覆盖,匈牙利算法)

poj 1422题目链接

题意+思路:DAG的最小路径覆盖。。。匈牙利算法。。。poj 2594的低配版。。

/* ***********************************************
Author :111qqz
Created Time :2016年05月26日 星期四 20时24分15秒
File Name :code/poj/r2594.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=505;
 7int n,m;
 8bool conc[N][N];
 9bool vis[N];
10int link[N];
11void floyd()
12{
13    for ( int k = 1 ; k <= n ; k++)
14	for ( int i = 1 ; i <= n ; i++)
15	    for ( int j = 1 ; j <= n ; j++)
16		if (conc[i][k]&&conc[k][j]) conc[i][j] = true; 
17}
 1bool dfs( int u)
 2{
 3    for ( int i = 1 ; i <= n ; i++)
 4    {
 5	if (conc[u][i])
 6	{
 7	    if (vis[i]) continue;
 8	    vis[i] = true;
 9	    if (link[i]==-1||dfs(link[i]))
10	    {
11		link[i] = u;
12		return true;
13	    }
14	}
15    }
16    return false;
17}
18int hungary()
19{
20    int res = 0 ;
21    ms(link,-1);
22    for ( int i = 1 ; i <= n ; i++)
23    {
24	ms(vis,false);
25	if (dfs(i)) res++;
26    }
27    return res;
28}
29int main()
30{
31	#ifndef  ONLINE_JUDGE 
32	freopen("code/in.txt","r",stdin);
33  #endif
 1	while (scanf("%d%d",&n,&m)!=EOF)
 2	{
 3	    if (n==0&&m==0) break;
 4	    ms(conc,false);
 5	    for ( int i = 1 ; i <= m ; i++)
 6	    {
 7		int u,v;
 8		scanf("%d%d",&u,&v);
 9		conc[u][v] = true;
10	    }
1	    floyd();
2	    int ans = hungary();
3	    printf("%d\n",n-ans);
	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}