题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2389
题意:一些人的坐标和速度,一些雨伞位置,规定人可以运动的时间,人不分高低贵贱。求最优的匹配方案使得尽可能多的伞有人。
思路:二分图版题,由于数据量达到1e3使用HK算法。初始化处理人为X集,雨伞为Y集,通过计算距离求得边。然后求最大匹配。
学习了HK算法,其实是增加了一个源点和汇点,然后把二分图问题转换为网络流问题。
<1>宽搜,从源点出发,所有X中未使用点加入队列为第一层。他们相连的Y上的点为第二层。这时候关键来了,Y上的点反馈到已经确定为相连匹配点的X上的点,置其为第三层。。。依次类推,退出条件是X上没有新点。
<2>深搜。遍历所有X中未使用的点。然后按照网络流来增广路。
学习了前向星,主要有struct EE{int b;int next}edge[MAXN*MAXN];int first[MAXN]两个数组,first用于指向X中某点最后一次使用是在哪一条边,next指向上一条使用的边,b记录当前相连的点。具体使用如下:
Memset(first,-1,sizeof(first));
If(满足条件)
Edge[tot].b = y;
Edge[tot].next = first[x];
First[x] = tot++;
源码:
邻接表版:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
#define inf 0x3f3f3f
int const MAXN = 3000+5;
int time,n,m;
int first[MAXN],dx[MAXN],dy[MAXN],distx[MAXN],disty[MAXN],dd;
struct PP
{
double x,y,speed;
void init(double a,double b,double c){x=a;y=b;speed=c;}
void init(double a,double b){x=a;y=b;}
}peo[MAXN],umbre[MAXN];
vector<int>edge[MAXN];
int edge_num;
double dis(PP a,PP b)
{
double s1 = pow(a.x-b.x,2);
double s2 = pow(a.y-b.y,2);
return sqrt(s1+s2);
}
void add_edge()
{
edge_num = 0;
memset(first,-1,sizeof(first));
for(int i=0; i<n; i++){
double limit = peo[i].speed * time;
for(int j=0; j<m; j++){
if(dis(peo[i],umbre[j])<=limit){
edge[i].push_back(j);
}
}
}
}
bool BFS()
{
memset(distx,0,sizeof(distx)); memset(disty,0,sizeof(disty));
int q[MAXN];
int head,tail;
head = tail = 0;
for(int i=0; i<n; i++){
if(dx[i]==-1){
q[tail++] = i;
}
}
bool flag(0);
dd = inf;
while(head<tail){
int x = q[head++];
if(distx[x]>dd)
break;
for(int k=0; k<edge[x].size(); k++){
int y = edge[x][k];
if(disty[y]==0){
disty[y] = distx[x] + 1;
if(dy[y]==-1){
flag = 1;
dd = disty[y];
}
else{
distx[dy[y]] = disty[y]+1;
q[tail++] = dy[y];
}
}
}
}
return flag;
}
bool DFS(int i)
{
int x = i;
for(int k=0; k<edge[i].size(); k++){
int y = edge[i][k];
if(disty[y] == distx[x]+1){
disty[y] = 0;
if(dy[y]!=-1 && disty[y]==dd)
continue;
if(dy[y]==-1 || DFS(dy[y])){
dx[x] = y;
dy[y] = x;
return true;
}
}
}
return 0;
}
int main()
{
int t;scanf("%d",&t);
for(int i=1; i<=t; i++){
scanf("%d",&time);
scanf("%d",&n);
double x,y,speed;
for(int i=0; i<n; i++){
scanf("%lf%lf%lf",&x,&y,&speed);
peo[i].init(x,y,speed);
edge[i].clear();
}
scanf("%d",&m);
for(int i=0; i<m; i++){
scanf("%lf%lf",&x,&y);
umbre[i].init(x,y);
}
add_edge();
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
int ans = 0;
while(BFS()){
for(int i=0; i<n; i++){
if(dx[i]==-1 && DFS(i)){
ans++;
}
}
}
printf("Scenario #%d:\n%d\n\n",i,ans);
}
return 0;
}
前向星版:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <iostream>
using namespace std;
#define inf 0x3f3f3f
int const MAXN = 3000+5;
int time,n,m;
int first[MAXN],dx[MAXN],dy[MAXN],distx[MAXN],disty[MAXN],dd;
struct PP
{
double x,y,speed;
void init(double a,double b,double c){x=a;y=b;speed=c;}
void init(double a,double b){x=a;y=b;}
}peo[MAXN],umbre[MAXN];
struct EE
{
int x,y,next;
}edge[MAXN*MAXN];
int edge_num;
double dis(PP a,PP b)
{
double s1 = pow(a.x-b.x,2);
double s2 = pow(a.y-b.y,2);
return sqrt(s1+s2);
}
void add_edge()
{
edge_num = 0;
memset(first,-1,sizeof(first));
for(int i=0; i<n; i++){
double limit = peo[i].speed * time;
for(int j=0; j<m; j++){
if(dis(peo[i],umbre[j])<=limit){
edge[edge_num].x = i;
edge[edge_num].y = j;
edge[edge_num].next = first[i];
first[i] = edge_num++;
}
}
}
}
bool BFS()
{
memset(distx,0,sizeof(distx)); memset(disty,0,sizeof(disty));
int q[MAXN];
int head,tail;
head = tail = 0;
for(int i=0; i<n; i++){
if(dx[i]==-1){
q[tail++] = i;
}
}
bool flag(0);
dd = inf;
while(head<tail){
int x = q[head++];
if(distx[x]>dd)
break;
for(int k=first[x]; k!=-1; k = edge[x].next){
int y = edge[k].y;
if(disty[y]==0){
disty[y] = distx[x] + 1;
if(dy[y]==-1){
flag = 1;
dd = disty[y];
}
else{
distx[dy[y]] = disty[y]+1;
q[tail++] = dy[y];
}
}
}
}
return flag;
}
bool DFS(int i)
{
for(int k=first[i];k!=-1;k=edge[k].next){
int x = edge[k].x;
int y = edge[k].y;
if(disty[y] == distx[x]+1){
disty[y] = 0;
if(dy[y]!=-1 && disty[y]==dd)
continue;
if(dy[y]==-1 || DFS(dy[y])){
dx[x] = y;
dy[y] = x;
return true;
}
}
}
return 0;
}
int main()
{
int t;scanf("%d",&t);
for(int i=1; i<=t; i++){
scanf("%d",&time);
scanf("%d",&n);
double x,y,speed;
for(int i=0; i<n; i++){
scanf("%lf%lf%lf",&x,&y,&speed);
peo[i].init(x,y,speed);
}
scanf("%d",&m);
for(int i=0; i<m; i++){
scanf("%lf%lf",&x,&y);
umbre[i].init(x,y);
}
add_edge();
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
int ans = 0;
while(BFS()){
for(int i=0; i<n; i++){
if(dx[i]==-1 && DFS(i)){
ans++;
}
}
}
printf("Scenario #%d:\n%d\n\n",i,ans);
}
return 0;
}