标签:err 分享 com wear other 技术分享 chm strong tin
题目链接:https://vjudge.net/problem/HDU-2389
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 655350/165535 K (Java/Others)
Total Submission(s): 4889 Accepted Submission(s): 1612
题解:
就直接求二分图最大匹配,不过由于数据较大,匈牙利算法超时,所以需要用HK算法。
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <string> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <queue> 10 #include <sstream> 11 #include <algorithm> 12 using namespace std; 13 const int INF = 2e9; 14 const int MOD = 1e9+7; 15 const int MAXN = 3000+10; 16 17 struct Node 18 { 19 int x, y, speed; 20 }gue[MAXN], umb[MAXN]; 21 22 int uN, vN, t; 23 vector<int>g[MAXN]; 24 25 int Mx[MAXN], My[MAXN]; 26 int dx[MAXN], dy[MAXN]; 27 int dis; 28 bool used[MAXN]; 29 30 bool SearchP() 31 { 32 queue<int>Q; 33 dis = INF; 34 memset(dx, -1, sizeof(dx)); 35 memset(dy, -1, sizeof(dy)); 36 for(int i = 1; i<=uN; i++) 37 if(Mx[i]==-1) 38 { 39 Q.push(i); 40 dx[i] = 0; 41 } 42 43 while(!Q.empty()) 44 { 45 int u = Q.front(); 46 Q.pop(); 47 if(dx[u]>dis) break; 48 int sz = g[u].size(); 49 for(int i = 0; i<sz; i++) 50 { 51 int v = g[u][i]; 52 if(dy[v]==-1) 53 { 54 dy[v] = dx[u] + 1; 55 if(My[v]==-1) dis = dy[v]; 56 else 57 { 58 dx[My[v]] = dy[v] + 1; 59 Q.push(My[v]); 60 } 61 } 62 } 63 } 64 return dis!=INF; 65 } 66 67 bool DFS(int u) 68 { 69 int sz = g[u].size(); 70 for(int i = 0; i<sz; i++) 71 { 72 int v = g[u][i]; 73 if(!used[v] && dy[v]==dx[u]+1) 74 { 75 used[v] = true; 76 if(My[v]!=-1 && dy[v]==dis) continue; 77 if(My[v]==-1 || DFS(My[v])) 78 { 79 My[v] = u; 80 Mx[u] = v; 81 return true; 82 } 83 } 84 } 85 return false; 86 } 87 88 int MaxMatch() 89 { 90 int res = 0; 91 memset(Mx, -1, sizeof(Mx)); 92 memset(My, -1, sizeof(My)); 93 while(SearchP()) 94 { 95 memset(used, false, sizeof(used)); 96 for(int i = 1; i<=uN; i++) 97 if(Mx[i]==-1 && DFS(i)) 98 res++; 99 } 100 return res; 101 } 102 103 int main() 104 { 105 int T, kase = 0; 106 scanf("%d", &T); 107 while(T--) 108 { 109 scanf("%d%d", &t, &uN); 110 for(int i = 1; i<=uN; i++) 111 { 112 scanf("%d%d%d", &gue[i].x, &gue[i].y, &gue[i].speed); 113 g[i].clear(); 114 } 115 116 scanf("%d", &vN); 117 for(int i = 1; i<=vN; i++) 118 scanf("%d%d", &umb[i].x, &umb[i].y); 119 120 for(int i = 1; i<=uN; i++) 121 for(int j = 1; j<=vN; j++) 122 { 123 int dis = (gue[i].x-umb[j].x)*(gue[i].x-umb[j].x) 124 +(gue[i].y-umb[j].y)*(gue[i].y-umb[j].y); 125 int s = gue[i].speed*gue[i].speed*t*t; 126 if(s>=dis) g[i].push_back(j); 127 } 128 129 int ans = MaxMatch(); 130 printf("Scenario #%d:\n%d\n\n", ++kase, ans); 131 132 } 133 }
HDU2389 Rain on your Parade —— 二分图最大匹配 HK算法
标签:err 分享 com wear other 技术分享 chm strong tin
原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7818293.html