题目链接:https://vjudge.net/problem/CSU-1531

Input

Output

Sample Input

2
1 5 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 Sample Output
1
3

题解:

一开始想用DP做,后来发现不行,因为新加入的点会破坏前面的结果,且不知道前面的状态如何,所以不能用动态规划的思想去解题。

1.用最少的边去覆盖掉所有的点,顾名思义,可以用二分图的最小覆盖点去做,只是这题的“点”为二分图的边,这题的"边"为二分图的点。

2.把题目的点的x、y坐标看做二分图的点,把题目的点当做二分图的边,其两端是x、y坐标,这样就转化成了用最少的点去覆盖掉所有的边。

代码一(矩阵):

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; int G[maxn][maxn];
int vis[maxn], match[maxn];
int n,m,k; int find(int u)
{
for(int i = ; i<m; i++)
{
if(G[u][i] && !vis[i] )
{
vis[i] = ;
if(match[i]==- || find(match[i]))
{
match[i] = u;
return ;
}
}
}
return ;
} void solve()
{
double x,y;
scanf("%d%d%d",&n,&m,&k); ms(G,);
for(int i = ; i<k; i++)
{
scanf("%lf%lf",&x,&y);
G[(int)x][(int)y] = ;
} int ans = ;
ms(match,-);
for(int i = ; i<n; i++)
{
ms(vis,);
if(find(i))
ans++;
}
printf("%d\n",ans);
} int main()
{
int T;
scanf("%d",&T);
while(T--){
solve();
} return ;
}

代码二(vector):

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; vector<int> G[maxn];
int vis[maxn], match[maxn];
int n,m,k; int find(int u)
{
int Size = G[u].size();
for(int i = ; i<Size; i++)
{
int v = G[u][i];
if(!vis[v] )
{
vis[v] = ;
if(match[v]==- || find(match[v]))
{
match[v] = u;
return ;
}
}
}
return ;
} void solve()
{
double x,y;
scanf("%d%d%d",&n,&m,&k); for(int i = ; i<n; i++)
G[i].clear();
for(int i = ; i<k; i++)
{
scanf("%lf%lf",&x,&y);
G[(int)x].pb((int)y);
} int ans = ;
ms(match,-);
for(int i = ; i<n; i++)
{
ms(vis,);
if(find(i))
ans++;
}
printf("%d\n",ans);
} int main()
{
int T;
scanf("%d",&T);
while(T--){
solve();
} return ;
}

CSU-1531 Jewelry Exhibition —— 二分图匹配(最小覆盖点)的更多相关文章

  1. CSUOJ 1531 Jewelry Exhibition

    Problem G Jewelry Exhibition To guard the art jewelry exhibition at night, the security agency has d ...

  2. csu 1552(米勒拉宾素数测试+二分图匹配)

    1552: Friends Time Limit: 3 Sec  Memory Limit: 256 MBSubmit: 723  Solved: 198[Submit][Status][Web Bo ...

  3. 【Learning】最小点覆盖(二分图匹配) 与Konig定理证明

    (附一道例题) Time Limit: 1000 ms   Memory Limit: 128 MB Description 最小点覆盖是指在二分图中,用最小的点集覆盖所有的边.当然,一个二分图的最小 ...

  4. 最短路&生成树&二分图匹配&费用流问题

    最短路 题意理解,建图 https://vjudge.net/problem/UVALive-4128 飞机票+行程建图 https://vjudge.net/problem/UVALive-3561 ...

  5. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  6. POJ 1274 裸二分图匹配

    题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...

  7. BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配

    1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2375  Solved: 1005[Submit][Sta ...

  8. HDU1281-棋盘游戏-二分图匹配

    先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...

  9. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

随机推荐

  1. tr命令用法

    原文链接 # echo "abcd"|tr 'a' 'b' bbcd tr 命令 用途 转换字符. 语法 tr [ -c | -cds | -cs | -C | -Cds | -C ...

  2. [Bzoj5358][Lydsy1805月赛]口算训练(预处理+动态开点线段树)

    5358: [Lydsy1805月赛]口算训练 Time Limit: 5 Sec  Memory Limit: 512 MBSubmit: 318  Solved: 105[Submit][Stat ...

  3. linux tomcat shutdown.sh 有时不能结束进程,使用如下指令进度重启

    ps -ef | grep tomcat | grep -v grep | cut -c 9-15 | xargs kill -9 & ./startup.sh

  4. AnsiString类型定义的时候可以直接指定代码页,比如950繁体字,936日文

    procedure TForm3.FormCreate(Sender: TObject); type AnsiStringForPage = type AnsiString(950);//代码页 va ...

  5. win7 x64 dtrace

    1.下载WINDOW DTRACE 工具 https://github.com/prash-wghats/DTrace-win32 2.系统参数修改 bcdedit/set testsigning o ...

  6. Python爬虫之路——简单网页抓图升级版(添加多线程支持)

    转载自我的博客:http://www.mylonly.com/archives/1418.html 经过两个晚上的奋斗.将上一篇文章介绍的爬虫略微改进了下(Python爬虫之路--简单网页抓图),主要 ...

  7. 转帖:HttpStatusCode状态说明C#版

    Continue 等效于 HTTP 状态 100.Continue 指示客户端可能继续其请求. SwitchingProtocols 等效于 HTTP 状态 101.SwitchingProtocol ...

  8. MIDI制作的相关软件

    Native Instruments Kontakt(音乐采样器) fl studio 12 v12.5.0.59 汉化版水果   http://xiazai.flstudiochina.com/wm ...

  9. Nginx简单了解

    1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置: server { listen80; # 端口号 lo ...

  10. Apcahe Shiro学习笔记(二):通过JDBC进行权限控制

    一.概述: 官方对Realm(领域)的描述:https://www.infoq.com/articles/apache-shiro 其功能本质上是一个安全特定的DAO,用于链接数据持久层(任何形式的都 ...