当前位置: 代码迷 >> 综合 >> HDU 2389 Rain on your Parade(Hopcroft_Carp Algorithm)
  详细解决方案

HDU 2389 Rain on your Parade(Hopcroft_Carp Algorithm)

热度:11   发布时间:2023-12-08 10:52:31.0

题目链接:
HDU 2389 Rain on your Parade

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <climits>
#include <queue>
using namespace std;
const int MAX_N = 3010;int T, n, m, total, dis, cases = 0;
double t, x[MAX_N], y[MAX_N], v[MAX_N];
int matchx[MAX_N], matchy[MAX_N], head[MAX_N];
int disx[MAX_N],disy[MAX_N], vis[MAX_N];struct Edge{int to, next;Edge () {}Edge(int _to, int _next) : to(_to), next(_next) {}
}edge[MAX_N*MAX_N];inline void init()
{total=0;memset(matchx,-1,sizeof(matchx));memset(matchy,-1,sizeof(matchy));memset(head,-1,sizeof(head));
}inline void AddEdge(int from, int to)
{edge[total].to = to;edge[total].next = head[from];head[from] = total++;
}inline bool bfs()
{dis = INT_MAX;memset(disx, -1, sizeof(disx));memset(disy, -1, sizeof(disy));queue<int> que;for(int i = 0; i < n; i++){if(matchx[i] == -1){que.push(i);disx[i] = 0;}}while(!que.empty()){int u = que.front();que.pop();if( disx[u] > dis) break;for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if( disy[v] == -1){disy[v] = disx[u] + 1;if(matchy[v] == -1) dis = disy[v];else {disx[matchy[v]] = disy[v] + 1;que.push(matchy[v]);}}}}return dis != INT_MAX;
}inline bool dfs(int u)
{for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if(!vis[v]){vis[v] = 1;if(matchy[v] != -1 && disy[v] == dis) continue;if(matchy[v] == -1 || dfs(matchy[v]) ){matchy[v] = u;matchx[u] = v;return true;}}   }return false;
}inline int Hopcroft_Carp()
{int ans = 0;while( bfs() ){memset(vis, 0, sizeof(vis));for(int i = 0; i < n; i++){if(matchx[i] == -1 && dfs(i)) ans++;}}return ans;
}int main()
{freopen("hdu2389.in","r",stdin);scanf("%d", &T);while(T-- > 0){init();scanf("%lf%d", &t, &n);for(int i = 0; i < n; i++){scanf("%lf%lf%lf",&x[i], &y[i], &v[i]);}scanf("%d",&m);for(int i = 0; i < m; i++){double tmpx, tmpy;scanf("%lf%lf",&tmpx, &tmpy);for(int j = 0; j < n; j++){double tmp = sqrt((tmpx - x[j]) * (tmpx - x[j]) + (tmpy - y[j]) * (tmpy - y[j]));if(v[j] * t >= tmp){//printf("j = %d i = %d\n",j, i);AddEdge(j,i);}}}int ans = Hopcroft_Carp();printf("Scenario #%d:\n%d\n\n",++cases,ans);}return 0;
}
  相关解决方案