标签:
Description
Input
Output
Sample Input
1 0 2 1 1 2 2 0 0 0
Sample Output
1 1 2
#include<stdio.h> #include<string.h> #include<math.h> #include<queue> #include<vector> #include<algorithm> using namespace std; const int maxn = 600; const int INF = 0x3f3f3f3f; vector<int>G[maxn]; int Mx[maxn], My[maxn], dx[maxn], dy[maxn], used[maxn], dis; int Map[maxn][maxn]; bool SearchP(int _n){ queue<int>Q; memset(dx,-1,sizeof(dx)); memset(dy,-1,sizeof(dy)); int dis = INF; for(int i = 1; i <= _n; i++){ if(Mx[i] == -1){ dx[i] = 0; Q.push(i); } } int v; while(!Q.empty()){ int u = Q.front(); Q.pop(); if(dx[u] > dis) break; for(int i = 0; i < G[u].size(); i++){ v = G[u][i]; if(dy[v] == -1){ dy[v] = dx[u] + 1; if(My[v] == -1){ dis = dy[v]; }else{ dx[My[v]] = dy[v] + 1; Q.push(My[v]); } } } } return dis != INF; } int dfs(int u){ int v; for(int i = 0; i < G[u].size(); i++){ v = G[u][i]; if(!used[v] && dy[v] == dx[u] + 1){ used[v] = 1; if(My[v] != -1 && dy[v] == dis){ continue; } if(My[v] == -1 || dfs(My[v])){ Mx[u] = v; My[v] = u; return true; } } } return false; } int MaxMatch(int ln,int rn){ int ret = 0; memset(Mx,-1,sizeof(Mx)); memset(My,-1,sizeof(My)); while(SearchP(ln)){ memset(used,0,sizeof(used)); for(int i = 1; i <= ln; i++){ if(Mx[i] == -1 && dfs(i)){ ret++; } } } return ret; } int main(){ int T, cas = 0, n, m, N, M, k; while(scanf("%d%d",&N,&M)!=EOF&&(N+M)){ for(int i = 0; i <= N; i++){ G[i].clear(); } int a,b; memset(Map,0,sizeof(Map)); for(int i = 1; i <= M; i++){ scanf("%d%d",&a,&b); Map[a][b] = 1; } for(int k = 1; k <= N; k++){ for(int i = 1; i <= N; i++){ for(int j = 1; j <= N; j++){ Map[i][j] = Map[i][j]|| Map[i][k]&&Map[k][j]; } } } for(int i = 1; i <= N; i++){ for(int j = 1; j <= N; j++){ if(Map[i][j]){ G[i].push_back(j); } } } n = m = N; int res = MaxMatch(n,m); printf("%d\n",N-res); } return 0; }
POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4955577.html