博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hdu 2389 Rain on your Parade hk算法
阅读量:3904 次
发布时间:2019-05-23

本文共 5573 字,大约阅读时间需要 18 分钟。

题目:

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.

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 <= s i <= 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

2121 0 33 0 324 06 0121 1 23 3 222 24 4

Sample Output

Scenario #1:2Scenario #2:2

思路:

此题用匈牙利算法会超时,得用hk算法。

这里先附上匈牙利算法:

#include 
#include
#include
#include
#include
using namespace std;const int maxn=3*1e3+5;int q;int n,m;int t;int px[maxn],py[maxn],s[maxn];int ux[maxn],uy[maxn];int match[maxn];int vis[maxn];struct edge{ int to; int next;}edge[maxn*maxn];int head[maxn];double dis (int x1,int y1,int x2,int y2){ return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}void add (int id,int u,int v){ edge[id].to=v; edge[id].next=head[u]; head[u]=id;}bool Find(int x){ for (int i=head[x];i!=-1;i=edge[i].next) { int u=edge[i].to; if(!vis[u]) { vis[u]=1; if(match[u]==-1||Find(match[u])) { match[u]=x; return true; } } } return false;}int alor(){ int sum=0; for (int i=1;i<=n;i++) { memset (vis,0,sizeof(vis)); if(Find(i)) sum++; } return sum;}int main(){ scanf("%d",&q); for (int k=1;k<=q;k++) { memset (head,-1,sizeof(head)); memset (match,-1,sizeof(match)); scanf("%d",&t); scanf("%d",&m); for (int i=1;i<=m;i++) { scanf("%d%d%d",&px[i],&py[i],&s[i]); } scanf("%d",&n); for (int i=1,id=0;i<=n;i++) { scanf("%d%d",&ux[i],&uy[i]); for (int j=1;j<=m;j++) { if(s[j]*t*1.0

然后再是hk算法:

#include 
#include
#include
#include
#include
#include
using namespace std;const int maxn=3*1e3+5;const int INF=0x3f3f3f3f;int q;int n,m;int dis;int t;int px[maxn],py[maxn],s[maxn];int ux[maxn],uy[maxn];int match[maxn];int vis[maxn];int Mx[maxn],My[maxn];int dx[maxn],dy[maxn];struct edge{ int to; int next;}edge[maxn*maxn];int head[maxn];double distance (int x1,int y1,int x2,int y2){ return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}void add (int id,int u,int v){ edge[id].to=v; edge[id].next=head[u]; head[u]=id;}bool Search(){ queue
q; dis=INF; memset (dx,-1,sizeof(dx)); memset (dy,-1,sizeof(dy)); for (int i=1;i<=n;i++) { if(Mx[i]==-1) { q.push(i); dx[i]=0; } } while(!q.empty()) { int now=q.front(); q.pop(); if(dx[now]>dis) break; for (int i=head[now];i!=-1;i=edge[i].next) { int u=edge[i].to; if(dy[u]==-1) { dy[u]=dx[now]+1; if(My[u]==-1) { dis=dy[u]; } else { dx[My[u]]=dy[u]+1; q.push(My[u]); } } } } return dis!=INF;}bool dfs (int u){ for (int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(!vis[v]&&dy[v]==dx[u]+1) { vis[v]=1; if(My[v]!=-1&&dy[v]==dis) continue; if(My[v]==-1||dfs(My[v])) { Mx[u]=v; My[v]=u; return true; } } } return false;}int maxMatch (){ int sum=0; memset (Mx,-1,sizeof(Mx)); memset (My,-1,sizeof(My)); while(Search()) { memset (vis,0,sizeof(vis)); for (int i=1;i<=n;i++) { if(Mx[i]==-1&&dfs(i)) { sum++; } } } return sum;}int main(){ scanf("%d",&q); for (int k=1;k<=q;k++) { memset (head,-1,sizeof(head)); memset (match,-1,sizeof(match)); scanf("%d",&t); scanf("%d",&m); for (int i=1;i<=m;i++) { scanf("%d%d%d",&px[i],&py[i],&s[i]); } scanf("%d",&n); for (int i=1,id=0;i<=n;i++) { scanf("%d%d",&ux[i],&uy[i]); for (int j=1;j<=m;j++) { if(s[j]*t*1.0

 

转载地址:http://qwoen.baihongyu.com/

你可能感兴趣的文章
一直误解sql事务的用法.
查看>>
转:利用C#实现分布式数据库查询
查看>>
转:Remoting系列(三)----对象的生命周期管理
查看>>
转:Remoting系列(二)----建立第一个入门程序
查看>>
转:Remoting系列(一)----Remoting的基本概念
查看>>
转:NET Remoting程序开发入门篇
查看>>
Net Remoting Singleton and Singlecall 区别
查看>>
2016年安大校赛(补题)
查看>>
BESTCODER ROUND92 1001.Skip the Class
查看>>
POJ 1661 Help Jimmy
查看>>
百练OJ 2755 神奇的口袋(递归+递推)
查看>>
HDU 1003 Max Sum
查看>>
Code Vs 1014 装箱
查看>>
循环队列,队链的实现
查看>>
HDU 2602 Bone Collector (01背包)
查看>>
POJ 1837 Blance (01背包)
查看>>
HDU 2456 饭卡 (01背包)
查看>>
HDU 1559 最大子矩阵
查看>>
Open Judge 4010 :2011
查看>>
百练OJ-2815 城堡问题【DFS】
查看>>