hdu1151最小路径覆盖
2015-09-11 12:31
405 查看
思路:一个士兵只能访问邻近的点,但是不能回头,相当于是访问一条边,问最少需要的士兵数;
显然就是一个最小路径覆盖问题;
ps:最小路径覆盖数 = 顶点数 - 最大匹配数;
时间效率:O(V * E)
题目链接
显然就是一个最小路径覆盖问题;
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; }
相关文章推荐
- Linux下软件常见安装方式
- swap文件
- yum报错
- ACM中素数(prime)操作入门解析
- TCP协议疑难杂症全景解析
- 【leetcode每日一题】226.Invert Binary Tree
- LeetCode ||Number of Digit One
- 《JavaScript从入门到精通》学习笔记(1)
- [css]input text ie6/7 border兼容问题
- 从“造”论文到自然而然写论文
- android:allowBackup=true 数据备份(adb backup)+查看(abe unpack backup.ab backup.tar)+恢复(adb re)
- 隐私:网民最常用密码MD5解密
- LeetCode 279 PerfectSquares
- Codeforces Round #257 (Div. 1) B. Jzzhu and Cities (记录最短路数量)
- 最老程序员开发实训9--Android---应用介绍页面实现1
- Uva 11752 The Super Powers
- 杭电 HDU OJ Max Sum ID1003 AC
- KMP算法:用于序列识别
- MVC4 创建控制器时,无法检索元数据
- Uvalive 6855 Banks