您的位置:首页 > 其它

hdu1151最小路径覆盖

2015-09-11 12:31 405 查看
思路:一个士兵只能访问邻近的点,但是不能回头,相当于是访问一条边,问最少需要的士兵数;

显然就是一个最小路径覆盖问题;

ps:最小路径覆盖数 = 顶点数 - 最大匹配数;

时间效率:O(V * E)

题目链接

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long ll;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 510;
int link
;
int vis
;
int gg

;
int n,m;

bool dfs(int u){
for (int i = 1;i <= n;++i){
if (!vis[i] && gg[u][i]){
vis[i] = 1;
if (link[i] == -1 || dfs(link[i])){
link[i] = u;
// link[u] = i;
return true;
}
}
}
return false;
}

inline int Hungary(){
int ret = 0;
MEM(link, -1);
for (int i = 1;i <= n;++i){
MEM(vis, 0);
if (dfs(i)) ret++;
}
return ret;
}

int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int T;
cin >> T;
while(T--){
cin >> n >> m;
MEM(gg, 0);
int a,b;
while(m--){
cin >> a >> b;
gg[a][b] = 1;
}
int ans = Hungary();
// cout << ans << endl;
cout << n - ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: