第一次写HK, 这个算法的原理和dinic比较类似,都是先bfs求出层次图,然后在层次图上进行dfs搜索增广路。
这个算法要注意的几点
1. 每次构建层次图时,最大层(dis)为最近的Y中的未覆盖的点
2. 每次在层次图中找的时候最多找到dis(最大层).
听说可以证明到这个算法的复杂度 为 sqrt(n)*m 。 我没有去证
但是对比dinic ,这种算法应该都是会提速的,将无规律的搜索变成有规律的
Rain on your Parade
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 655350/165535 K (Java/Others) Total Submission(s): 2046 Accepted Submission(s): 624
Problem Description
You’re giving a party in the garden of your villa by the sea. The party is a huge success, and everyone is here. It’s a warm, sunny evening, and a soothing wind sends fresh, salty air from the sea. The evening is progressing just as you had imagined. It could be the perfect end of a beautiful day. But nothing ever is perfect. One of your guests works in weather forecasting. He suddenly yells, “I know that breeze! It means its going to rain heavily in just a few minutes!” Your guests all wear their best dresses and really would not like to get wet, hence they stand terrified when hearing the bad news. You have prepared a few umbrellas which can protect a few of your guests. The umbrellas are small, and since your guests are all slightly snobbish, no guest will share an umbrella with other guests. The umbrellas are spread across your (gigantic) garden, just like your guests. To complicate matters even more, some of your guests can’t run as fast as the others. Can you help your guests so that as many as possible find an umbrella before it starts to pour?
Given the positions and speeds of all your guests, the positions of the umbrellas, and the time until it starts to rain, find out how many of your guests can at most reach an umbrella. Two guests do not want to share an umbrella, however.
Given the positions and speeds of all your guests, the positions of the umbrellas, and the time until it starts to rain, find out how many of your guests can at most reach an umbrella. Two guests do not want to share an umbrella, however.
Input
The input starts with a line containing a single integer, the number of test cases. Each test case starts with a line containing the time t in minutes until it will start to rain (1 <=t <= 5). The next line contains the number of guests m (1 <= m <= 3000), followed by m lines containing x- and y-coordinates as well as the speed si in units per minute (1 <= si <= 3000) of the guest as integers, separated by spaces. After the guests, a single line contains n (1 <= n <= 3000), the number of umbrellas, followed by n lines containing the integer coordinates of each umbrella, separated by a space. The absolute value of all coordinates is less than 10000.
Output
For each test case, write a line containing “Scenario #i:”, where i is the number of the test case starting at 1. Then, write a single line that contains the number of guests that can at most reach an umbrella before it starts to rain. Terminate every test case with a blank line.
Sample Input
2
1
2
1 0 3
3 0 3
2
4 0
6 0
1
2
1 1 2
3 3 2
2
2 2
4 4
Sample Output
Scenario #1:
2
Scenario #2:
2
Source
Recommend
lcy
// 对于这种题,用邻接矩阵会更好些吧 #include <stdio.h> #include <string.h> #include <iostream> using namespace std; #define N 3033 #define INF 0x3ffffff bool g[N][N]; int gx[N],gy[N],spd[N]; int t; int n,m; int mark[N],frt[N],frt1[N]; int lvx[N],lvy[N]; int que[10*N]; int flag; /*double dis(double x,double y,double x1,double y1) 有sqrt就会导致不精确 { return sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)); }*/ /*void add_edge(int u,int v) { edge[cnt].to=v; edge[cnt].next=pre[u]; pre[u]=cnt++; }*/ int bfs() { memset(lvx,-1,sizeof(lvx)); memset(lvy,-1,sizeof(lvy)); int qf=0,qd=0; for(int i=1;i<=n;i++) { if( frt1[i] == -1 ) { lvx[i]=0; que[qf++]=i; } } int v; flag = INF; while( qf>qd ) { int cur=que[qd++]; if( lvx[cur] > flag ) break; for(int i=1;i<=m;i++) { v=i; if( lvy[v] != -1 || g[cur][v]==0) continue; lvy[v] = lvx[cur]+1; if( frt[v]==-1 ) // 如果==-1 的时候 { flag=lvy[v]; } else { //if(lvx[ frt[v] ] != -1) continue; // 据bfs的性质,每个点只能访问一次 lvx[ frt[v] ] = lvy[v]+1; que[ qf++ ] = frt[v]; } } } return flag!=INF; } int dfs(int s) { int v; for(int i=1;i<=m;i++) { v=i; if(mark[v]==1 || lvx[s]+1 != lvy[v] || g[s][v]==0 ) continue;// 一个程序wa了绝对是有错误。 mark[v] = 1; if(frt[v] != -1 && lvy[v] == flag) continue; // 加了这个就不超时了?!!!,这个是必须用的 if( frt[v] == -1 || dfs(frt[v]) ) { frt[v]=s; frt1[s]=v; return 1; } } return 0; } int main() { int tt=1; int T; scanf("%d",&T); while(T--) { memset(g,0,sizeof(g)); scanf("%d",&t); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d%d",&gx[i],&gy[i],&spd[i]); } scanf("%d",&m); for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); for(int j=1;j<=n;j++) { if( ((gx[j]-x)*(gx[j]-x)+(gy[j]-y)*(gy[j]-y)) <= t*t*spd[j]*spd[j]) { g[j][i]=1; } } } int sum=0; memset(frt,-1,sizeof(frt)); memset(frt1,-1,sizeof(frt1)); while( bfs() ) // 先对当前网络分层,然后在dfs搜索增广路径,类似于dinic,只在层次网络上搜索,路径很少可以减少时间复杂度 { for(int i=1;i<=n;i++) { memset(mark,0,sizeof(mark)); if(frt1[i] == -1) // 如果 sum += dfs(i); } } printf("Scenario #%d:\n",tt++); printf("%d\n",sum); printf("\n"); } return 0; }