[POJ2594]Treasure Exploration(最小路径覆盖变种,floyd算法,匈牙利算法)

题目链接:http://poj.org/problem?id=2594

题意:给一张图,有单向边。现在要往图上某几个点放几个机器人,机器人要把所有的点都走过,问最少需要放多少个机器人,路径可以重复走。

路径可以重复走,则可以用floyd求出所有可达的点,再做最小路径覆盖,根据公式 最小路径覆盖=节点数-最大匹配直接做二分图最大匹配就可以。

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35 using namespace std;
 36 #define fr first
 37 #define sc second
 38 #define cl clear
 39 #define BUG puts("here!!!")
 40 #define W(a) while(a--)
 41 #define pb(a) push_back(a)
 42 #define Rint(a) scanf("%d", &a)
 43 #define Rs(a) scanf("%s", a)
 44 #define Cin(a) cin >> a
 45 #define FRead() freopen("in", "r", stdin)
 46 #define FWrite() freopen("out", "w", stdout)
 47 #define Rep(i, len) for(int i = 0; i < (len); i++)
 48 #define For(i, a, len) for(int i = (a); i < (len); i++)
 49 #define Cls(a) memset((a), 0, sizeof(a))
 50 #define Clr(a, x) memset((a), (x), sizeof(a))
 51 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
 52 #define lrt rt << 1
 53 #define rrt rt << 1 | 1
 54 #define pi 3.14159265359
 55 #define RT return
 56 #define lowbit(x) x & (-x)
 57 #define onecnt(x) __builtin_popcount(x)
 58 typedef long long LL;
 59 typedef long double LD;
 60 typedef unsigned long long ULL;
 61 typedef pair<int, int> pii;
 62 typedef pair<string, int> psi;
 63 typedef pair<LL, LL> pll;
 64 typedef map<string, int> msi;
 65 typedef vector<int> vi;
 66 typedef vector<LL> vl;
 67 typedef vector<vl> vvl;
 68 typedef vector<bool> vb;
 69 
 70 const int maxn = 555;
 71 int nu, nv;
 72 int G[maxn][maxn];
 73 int linker[maxn];
 74 bool vis[maxn];
 75 
 76 bool dfs(int u) {
 77     For(v, 1, nv+1) {
 78         if(G[u][v] && !vis[v]) {
 79             vis[v] = 1;
 80             if(linker[v] == -1 || dfs(linker[v])) {
 81                 linker[v] = u;
 82                 return 1;
 83             }
 84         }
 85     }
 86     return 0;
 87 }
 88 
 89 int hungary() {
 90     int ret = 0;
 91     Clr(linker, -1);
 92     For(u, 1, nu+1) {
 93         Cls(vis);
 94         if(dfs(u)) ret++;
 95     }
 96     return ret;
 97 }
 98 
 99 int n, m;
100 
101 int main() {
102     // FRead();
103     int u, v;
104     while(~Rint(n) && ~Rint(m) && n + m) {
105         Cls(G);
106         nu = nv = n;
107         Rep(i, m) {
108             Rint(u); Rint(v);
109             G[u][v] = 1;
110         }
111         For(k, 1, n+1) {
112             For(i, 1, n+1) {
113                 For(j, 1, n+1) {
114                     if(G[i][k] + G[k][j] == 2) {
115                         G[i][j] = 1;
116                     }
117                 }
118             }
119         }
120         printf("%d
", nu - hungary());
121     }
122     RT 0;
123 }
原文地址:https://www.cnblogs.com/kirai/p/5808143.html