[HDOJ2389]Rain on your Parade(二分图最大匹配,HK算法)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2389
题意:一个人请了m个人在院子里聚会,每个人有位置坐标和速度。院子里有n把伞,给出伞的位置。现在要下雨了,所有人应该在k秒内拿到伞,问如何拿伞能让尽可能多的人拿到伞,每把伞只能有一个人打。
以每个人到某把伞的时间是否在限定时间内来建立二分图,之后跑最大匹配就行。这里有一个问题出在了点的数量是3000,在点数量较大的情况下使用一般的匈牙利和KM算法会TLE,所以学习到了使用HK算法,尽管我暂时还不知道HK算法的具体解决问题的思路和方法,但是我会用板子…
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 Rll(a) scanf("%I64d", &a) 44 #define Rs(a) scanf("%s", a) 45 #define Cin(a) cin >> a 46 #define FRead() freopen("in", "r", stdin) 47 #define FWrite() freopen("out", "w", stdout) 48 #define Rep(i, len) for(int i = 0; i < (len); i++) 49 #define For(i, a, len) for(int i = (a); i < (len); i++) 50 #define Cls(a) memset((a), 0, sizeof(a)) 51 #define Clr(a, x) memset((a), (x), sizeof(a)) 52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 53 #define lrt rt << 1 54 #define rrt rt << 1 | 1 55 #define pi 3.14159265359 56 #define RT return 57 #define lowbit(x) x & (-x) 58 #define onecnt(x) __builtin_popcount(x) 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long ULL; 62 typedef pair<int, int> pii; 63 typedef pair<string, int> psi; 64 typedef pair<LL, LL> pll; 65 typedef map<string, int> msi; 66 typedef vector<int> vi; 67 typedef vector<LL> vl; 68 typedef vector<vl> vvl; 69 typedef vector<bool> vb; 70 const int maxn = 3030; 71 const int inf = 0x3f3f3f3f; 72 int nx, ny, dist; 73 int Mx[maxn], My[maxn], dx[maxn], dy[maxn], vis[maxn], G[maxn][maxn]; 74 75 bool Dfs(int u){ 76 for(int v = 0; v < ny; v++) 77 if(!vis[v] && G[u][v] && dy[v] == dx[u] + 1){ 78 vis[v] = 1; 79 if(My[v] != -1 && dy[v] == dist) 80 continue; 81 if(My[v] == -1|| Dfs(My[v])){ 82 My[v] = u; 83 Mx[u] = v; 84 return 1; 85 } 86 } 87 return 0; 88 } 89 bool Search(){ 90 queue<int> Q; 91 dist = inf; 92 memset(dx, -1, sizeof(dx)); 93 memset(dy, -1, sizeof(dy)); 94 for(int i = 0; i < nx; i++) 95 if(Mx[i] == -1){ 96 Q.push(i); 97 dx[i] = 0; 98 } 99 while(!Q.empty()){ 100 int u = Q.front(); 101 Q.pop(); 102 if(dx[u] > dist) 103 break; 104 for(int v = 0; v < ny; v++) 105 if(G[u][v] && dy[v] == -1){ 106 dy[v] = dx[u] + 1; 107 if(My[v] == -1) 108 dist = dy[v]; 109 else{ 110 dx[My[v]] = dy[v] + 1; 111 Q.push(My[v]); 112 } 113 } 114 } 115 return dist != inf; 116 } 117 int MaxMatch() { 118 int res = 0; 119 memset(Mx, -1, sizeof(Mx)); 120 memset(My, -1, sizeof(My)); 121 while(Search()){ 122 memset(vis, 0, sizeof(vis)); 123 for(int i = 0; i < nx; i++) 124 if(Mx[i] == -1&& Dfs(i)) 125 res++; 126 } 127 return res; 128 } 129 int n, m, k; 130 typedef struct Node { 131 double x, y, v; 132 }Node; 133 Node num[maxn], pos[maxn]; 134 135 double dis(Node a, Node b) { 136 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 137 } 138 139 inline bool scan_lf(double &num) { 140 char in;double Dec=0.1; 141 bool IsN=false,IsD=false; 142 in=getchar(); 143 if(in==EOF) return false; 144 while(in!='-'&&in!='.'&&(in<'0'||in>'9')) 145 in=getchar(); 146 if(in=='-'){IsN=true;num=0;} 147 else if(in=='.'){IsD=true;num=0;} 148 else num=in-'0'; 149 if(!IsD){ 150 while(in=getchar(),in>='0'&&in<='9'){ 151 num*=10;num+=in-'0';} 152 } 153 if(in!='.'){ 154 if(IsN) num=-num; 155 return true; 156 }else{ 157 while(in=getchar(),in>='0'&&in<='9'){ 158 num+=Dec*(in-'0');Dec*=0.1; 159 } 160 } 161 if(IsN) num=-num; 162 return true; 163 } 164 inline bool scan_d(int &num) { 165 char in;bool IsN=false; 166 in=getchar(); 167 if(in==EOF) return false; 168 while(in!='-'&&(in<'0'||in>'9')) in=getchar(); 169 if(in=='-'){ IsN=true;num=0;} 170 else num=in-'0'; 171 while(in=getchar(),in>='0'&&in<='9'){ 172 num*=10,num+=in-'0'; 173 } 174 if(IsN) num=-num; 175 return true; 176 } 177 178 int main() { 179 // FRead(); 180 int T, _ = 1; 181 scan_d(T); 182 W(T) { 183 Cls(G); 184 scan_d(k); scan_d(m); 185 Rep(i, m) { 186 scan_lf(num[i].x); 187 scan_lf(num[i].y); 188 scan_lf(num[i].v); 189 } 190 Rint(n); 191 Rep(i, n) { 192 scan_lf(pos[i].x); 193 scan_lf(pos[i].y); 194 } 195 nx = m; ny = n; 196 Rep(i, m) { 197 Rep(j, n) { 198 if(dis(num[i], pos[j]) / num[i].v <= k) { 199 G[i][j] = 1; 200 } 201 } 202 } 203 printf("Scenario #%d:\n%d\n\n", _++, MaxMatch()); 204 } 205 RT 0; 206 }