Problem G

Jewelry Exhibition

To guard the art jewelry exhibition at night, the security agency has decided to use a new laser beam system, consisting of sender-receiver pairs. Each pair generates a strip of light of one unit width and guards all objects located inside the strip. Your task is to help the agency and to compute for each exhibition room the minimum number of sender-receiver pairs which are sufficient to protect all exhibits inside the room.

Any room has a rectangle shape, so we describe it as an [0,N] × [0,M] rectangle in the plane. The objects we need to guard are represented as points inside that rectangle. Each sender is mounted on a wall and the corresponding receiver on the opposite wall in such a way that the generated strip is a rectangle of unit width and length either N or M. Since the new laser beam system is still not perfect, each sender-receiver pair can only be mounted to generate strips the corners of which have integer coordinates. An additional drawback is that the sender-receiver pairs can protect only items inside the strips, but not those lying on their borders. Thus, the security agency arranged the exhibits in such a way that both coordinates of any point representing an exhibit are non-integers. The figure below (left) illustrates eight items arranged in [0,4]×[0,4] (the second sample input). In the room, up to eight sender-receiver pairs can be mounted. The figure to the right shows an area protected by three sender-receiver pairs.

Input

The input starts with the number of exhibition rooms R ≤ 10. Then the descriptions of the R rooms follow. A single description starts with a single line, containing three integers: 0 < N ≤ 100, 0 < M ≤ 100, specifying the size of the current room and 0 < K ≤ 104, for the number of exhibits. Next K lines follow, each of which consists of two real numbers x,y describing the exhibit coordinates. You can assume that 0 < x < N, 0 < y < M and that x and y are non-integer.

Output

For every room output one line containing one integer, that is the minimum number of sender-receiver pairs sufficient to protect all exhibits inside the room.

Sample Input                                                                  Sample Output

2                                                                                    1

1 5 3                                                                              3

0.2 1.5

0.3 4.8

0.4 3.5

4 4 8

0.7 0.5

1.7 0.5

2.8 1.5

3.7 0.5

2.2 3.6

2.7 2.7

1.2 2.2

1.2 2.7

解题:最大匹配

匈牙利算法

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = ;
bool e[maxn][maxn],used[maxn];
int linker[maxn],U,V;
bool dfs(int u) {
for(int i = ; i < V; ++i) {
if(e[u][i] && !used[i]) {
used[i] = true;
if(linker[i] == - || dfs(linker[i])) {
linker[i] = u;
return true;
}
}
}
return false;
}
int main() {
int ks,n;
double x,y;
scanf("%d",&ks);
while(ks--) {
scanf("%d%d%d",&U,&V,&n);
memset(linker,-,sizeof(linker));
memset(e,false,sizeof(e));
for(int i = ; i < n; ++i) {
scanf("%lf %lf",&x,&y);
e[(int)x][(int)y] = true;
}
int ans = ;
for(int i = ; i < U; ++i) {
memset(used,false,sizeof(used));
ans += dfs(i);
}
printf("%d\n",ans);
}
return ;
}

最大流

 #include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
}e[maxn*maxn*];
int head[maxn],d[maxn],cur[maxn],tot,S,T;
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
queue<int>q;
memset(d,-,sizeof(d));
d[S] = ;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == d[u] + &&(a=dfs(e[i].to,min(low,e[i].flow)))){
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int solve(){
int ans = ;
while(bfs()){
memcpy(cur,head,sizeof(head));
ans += dfs(S,INF);
}
return ans;
}
int main(){
int ks,n,m,k;
double x,y;
scanf("%d",&ks);
while(ks--){
scanf("%d %d %d",&n,&m,&k);
memset(head,-,sizeof(head));
for(int i = tot = ; i < k; ++i){
scanf("%lf %lf",&x,&y);
add((int)x,n+(int)y,);
}
S = n + m;
T = S + ;
for(int i = ; i < n; ++i)
add(S,i,);
for(int i = ; i < m; ++i)
add(i+n,T,);
printf("%d\n",solve());
}
return ;
}

最新文章

  1. .Net缓存管理框架CacheManager
  2. 浅谈servlet版本
  3. POJ 3114 Countries in War(强联通分量+Tarjan)
  4. HDU 4609 3-idiots FFT+容斥
  5. 进程间通信之XSI IPC
  6. IIS配置
  7. MFC模态和非模态对话框编程
  8. IHttpHandler处理请求api
  9. hdu2586How far away ?(LCA LCATarjan离线)
  10. RestTemplate 服务名请求
  11. sublime text修改package安装路径
  12. 5.5 C++重载赋值操作符
  13. 廖雪峰Java1-3流程控制-5循环
  14. 【转载】 java利用snmp4j包来读取snmp协议数据(Manager端)
  15. java梳理-序列化与反序列化
  16. console的所有用法
  17. Winform的学习
  18. php:Mcrypt响应慢的原因解决备注
  19. Integrating Google Sign-In into Your Android App
  20. Conferences

热门文章

  1. javascript jquery 推断对象为空的方式
  2. mysql-管理事务
  3. 3.IntelliJ IDEA 使用详解
  4. Car Talk1
  5. python3.x 学习笔记1(基础知识)
  6. CF 986C AND Graph(建模+DFS)
  7. CSS动画框架Loaders.css +animate.css
  8. Chrome发布73 beta版:增强Linux用户体验
  9. WPF模仿QQ登录按钮
  10. 现在有一个函数A和函数B,请你实现B继承A