hdu 2389(最大匹配bfs版)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2389
思路:纯裸的一个最大匹配题,不过悲摧的是以前一直用的dfs版一直过不了,TLE无数次啊,然后改成bfs就过了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<cmath> 7 #include<queue> 8 using namespace std; 9 #define MAXN 3333 10 vector<int>map[MAXN]; 11 int ly[MAXN],lx[MAXN]; 12 int distx[MAXN],disty[MAXN]; 13 int n,m; 14 struct Node { 15 int x,y,s; 16 } p[MAXN]; 17 struct Point { 18 int x,y; 19 } q[MAXN]; 20 21 bool bfs() 22 { 23 bool flag=false; 24 memset(distx,0,(n+2)*sizeof(int)); 25 memset(disty,0,(n+2)*sizeof(int)); 26 queue<int>Q; 27 for(int i=1; i<=n; i++) { 28 if(lx[i]==-1)Q.push(i); 29 } 30 while(!Q.empty()) { 31 int u=Q.front(); 32 Q.pop(); 33 for(int i=0; i<map[u].size(); i++) { 34 int v=map[u][i]; 35 if(disty[v]==0) { 36 disty[v]=distx[u]+1; 37 if(ly[v]==-1)flag=true; 38 else { 39 distx[ly[v]]=disty[v]+1; 40 Q.push(ly[v]); 41 } 42 } 43 } 44 } 45 return flag; 46 } 47 48 int dfs(int u) 49 { 50 for(int i=0; i<map[u].size(); i++) { 51 int v=map[u][i]; 52 if(disty[v]==distx[u]+1) { 53 disty[v]=0; 54 if(ly[v]==-1||dfs(ly[v])) { 55 ly[v]=u; 56 lx[u]=v; 57 return 1; 58 } 59 } 60 } 61 return 0; 62 } 63 64 65 int MaxMatch() 66 { 67 int res=0; 68 memset(lx,-1,(n+2)*sizeof(lx[0])); 69 memset(ly,-1,(n+2)*sizeof(ly[0])); 70 while(bfs()) { 71 for(int i=1; i<=n; i++) { 72 if(lx[i]==-1)res+=dfs(i); 73 } 74 } 75 return res; 76 } 77 78 int main() 79 { 80 // freopen("1.txt","r",stdin); 81 int _case,time,t=1; 82 scanf("%d",&_case); 83 while(_case--) { 84 scanf("%d%d",&time,&n); 85 for(int i=1; i<=n; i++)map[i].clear(); 86 for(int i=1; i<=n; i++) { 87 scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].s); 88 } 89 scanf("%d",&m); 90 for(int i=1; i<=m; i++) { 91 scanf("%d%d",&q[i].x,&q[i].y); 92 } 93 for(int i=1; i<=n; i++) { 94 for(int j=1; j<=m; j++) { 95 double xx=(p[i].x-q[j].x)*(p[i].x-q[j].x)*1.0; 96 double yy=(p[i].y-q[j].y)*(p[i].y-q[j].y)*1.0; 97 double dd=sqrt(xx+yy); 98 if(dd<=1.0*time*p[i].s)map[i].push_back(j); 99 } 100 } 101 int ans=MaxMatch(); 102 printf("Scenario #%d:\n%d\n",t++,ans); 103 puts(""); 104 } 105 return 0; 106 }