poj2594 机器人寻找宝藏(最小路径覆盖)
题目来源:http://poj.org/problem?id=2594
参考博客:http://www.cnblogs.com/ka200812/archive/2011/07/31/2122641.html
题解:
最小路径覆盖=|P|-最大匹配数
单向图且没有循环,不可能存在a到b,b又到a,并且可以经过已经经过的点,所以对整个图进行处理,间接连接的点可以看作是直接连接
将整个图分成两边,点与点二分匹配,与平时的两种不同物体匹配不同
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=500+5;
int ma[maxn][maxn],match[maxn];
bool used[maxn];
int n;
void floyed(){
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(ma[i][k]+ma[k][j]==2)ma[i][j]=1;
}
bool dfs(int x){
for(int i=1;i<=n;i++){
if(ma[x][i]&&used[i]==0){
used[i]=1;
if(match[i]==0||dfs(match[i])){
match[i]=x;
return 1;
} }
}
return 0;
}
int solve(){
memset(match,0,sizeof(match));
int ans=0;
for(int i=1;i<=n;i++){
memset(used,0,sizeof(used));
if(dfs(i))ans++;
}
return n-ans;
}
int main()
{
int m;
while(scanf("%d %d",&n,&m)==2){
if(n==0&&m==0)break;
memset(ma,0,sizeof(ma));
for(int i=1;i<=m;i++){
int a,b;
scanf("%d %d",&a,&b);
ma[a][b]=1;
}
floyed();
cout<<solve()<<endl;
}
return 0;
}
poj2594 机器人寻找宝藏(最小路径覆盖)的更多相关文章
- POJ2594 Treasure Exploration(最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8550 Accepted: 3 ...
- [POJ2594] Treasure Exploration(最小路径覆盖-传递闭包 + 匈牙利算法)
传送门 引子: 有一个问题,是对于一个图上的所有点,用不相交的路径把他们覆盖,使得每个点有且仅属于一条路径,且这个路径数量尽量小. 对于这个问题可以把直接有边相连的两点 x —> y,建一个二分 ...
- poj2594 (最小路径覆盖 + floyd)
题目链接 http://poj.org/problem?id=2594) 题目大意: 一个有向图中, 有若干条连接的路线, 问最少放多少个机器人,可以将整个图上的点都走过. 最小路径覆盖问题. 分析 ...
- POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 9794 Accepted: 3 ...
- POJ-2594 Treasure Exploration,floyd+最小路径覆盖!
Treasure Exploration 复见此题,时隔久远,已忘,悲矣! 题意:用最少的机器人沿单向边走完( ...
- POJ2594 Treasure Exploratio —— 最小路径覆盖 + 传递闭包
题目链接:https://vjudge.net/problem/POJ-2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 655 ...
- POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8130 Accepted: 3 ...
- POJ2594 Treasure Exploration【DAG有向图可相交的最小路径覆盖】
题目链接:http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K T ...
- poj2594——最小路径覆盖
Description Have you ever read any book about treasure exploration? Have you ever see any film about ...
随机推荐
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
1.第一步,在本地数据库中建一个与服务器同名的数据库 2.第二步,右键源数据库,任务>导出数据,弹出导入导出提示框,点下一步继续 3.远程数据库操作,确认服务器名称(服务器地址).身份验证(输入 ...
- Android Studio连接天天模拟器
方法:安装两者后,打开天天模拟器的adb.exe所在目录,我的是C:\Users\ Android\sdk\platform-tools,在打开的文件夹下使用“shift+鼠标右键”打开cmd终端. ...
- Android内嵌PDF预览
一.在对应模块的build.gradle文件中加入依赖 dependencies { implementation 'com.github.barteksc:android-pdf-viewer:3. ...
- 【底层原理】深入理解Cache (上)
存储器是分层次的,离CPU越近的存储器,速度越快,每字节的成本越高,同时容量也因此越小.寄存器速度最快,离CPU最近,成本最高,所以个数容量有限,其次是高速缓存(缓存也是分级,有L1,L2等缓存),再 ...
- VMware虚拟机安装CentOS7【转】-添加部分注释(自己看着方便)
本文稍作改动,添加部分说明,原文链接:https://www.cnblogs.com/wcwen1990/p/7630545.html 本文基于vmware workstations进行CentOS7 ...
- JS 代码中到底加不加分号
背景 在写自动执行函数时 vm.$watch('datas', function() { console.log(vm.datas); }) (function () { console.log('t ...
- Spring MVC 之请求参数和路径变量
请求参数和路径变量都可以用于发送值给服务器.二者都是URL的一部分.请求参数采用key=value形式,并用“&”分隔. 例如,下面的URL带有一个名为productId的请求参数,其值为3: ...
- 【ctags/cscope/project安装使用】给神编辑器vim添加新的翅膀
本文地址 分享提纲: 1.安装 2.使用cscope 3.使用project 1.安装 1.1)linux(yum下安装) yum -y install cscope 1.2)linux(unbunt ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块
C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Django-rest-framework 接口实现 rest_framework 中有已经定义好的 工具类 mixins generics viewsets
rest_framework.mixins 请求业务 的 5 种实现 mixin(混合类):不能单独使用,和其它类搭配起来使用(利用了Python支持多继承) rest_framework.mix ...