POJ 1325 && 1274:Machine Schedule 匈牙利算法模板题
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 12976 | Accepted: 5529 |
Description
we consider a 2-machine scheduling problem.
There are two machines A and B. Machine A has n kinds of working modes, which is called mode_0, mode_1, ..., mode_n-1, likewise machine B has m kinds of working modes, mode_0, mode_1, ... , mode_m-1. At the beginning they are both work at mode_0.
For k jobs given, each of them can be processed in either one of the two machines in particular mode. For example, job 0 can either be processed in machine A at mode_3 or in machine B at mode_4, job 1 can either be processed in machine A at mode_2 or in machine
B at mode_4, and so on. Thus, for job i, the constraint can be represent as a triple (i, x, y), which means it can be processed either in machine A at mode_x, or in machine B at mode_y.
Obviously, to accomplish all the jobs, we need to change the machine's working mode from time to time, but unfortunately, the machine's working mode can only be changed by restarting it manually. By changing the sequence of the jobs and assigning each job to
a suitable machine, please write a program to minimize the times of restarting machines.
Input
x, y.
The input will be terminated by a line containing a single zero.
Output
Sample Input
5 5 10
0 1 1
1 1 2
2 1 3
3 1 4
4 2 1
5 2 2
6 2 3
7 2 4
8 3 3
9 4 3
0
Sample Output
3
题意是有两台机器,每个机器有若干个工作状态。有n个工作,每一个工作要求要么是在第一台机器的x状态下才能进行,要么是在第二台机器的y状态下才能进行。机器每切换一种状态要重启,问在给定工作的前提下,最少重启多少次。
二分图的最小点覆盖。发现做这种题最难的不是算法本身,什么匈牙利算法了。而是建图的那个过程,如何建图。这点还需要积累经验。
还有要注意的一点是两台机器的起始状态是0,所以有工作是要在状态0的时候的不用管它,一开始做它们就好了。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int grid[805][805];
int link[805];
int visit[805];
int n,k,V1,V2;
int result; bool dfs(int x)
{
int i;
for(i=0;i<V2;i++)
{
if(grid[x][i]==1&&visit[i]==0)
{
visit[i]=1;
if(link[i]==-1||dfs(link[i]))
{
link[i]=x;
return true;
}
}
}
return false;
} void Magyarors()
{
int i; result=0;
memset(link,-1,sizeof(link));//!!这里不能是0 for(i=0;i<V1;i++)
{
memset(visit,0,sizeof(visit));
if(dfs(i))
result++;
}
cout<<n-result/2<<endl;
} int main()
{
int i,j,temp1,temp2,temp3;
while(scanf("%d",&n)!=EOF)
{
memset(grid,0,sizeof(grid));
V1=V2=n;
for(i=0;i<n;i++)
{
scanf("%d: (%d)",&temp1,&temp2);
for(j=1;j<=temp2;j++)
{
scanf("%d",&temp3);
grid[temp1][temp3]=1;
}
}
Magyarors();
}
return 0;
}
与此题相类似的特别多,POJ1274 算一个。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int grid[805][805];
int link[805];
int visit[805];
int n,k,V1,V2;
int result; bool dfs(int x)
{
int i;
for(i=1;i<=V2;i++)
{
if(grid[x][i]==1&&visit[i]==0)
{
visit[i]=1;
if(link[i]==-1||dfs(link[i]))
{
link[i]=x;
return true;
}
}
}
return false;
} void Magyarors()
{
int i; result=0;
memset(link,-1,sizeof(link));//!!这里不能是0 for(i=1;i<=V1;i++)
{
memset(visit,0,sizeof(visit));
if(dfs(i))
result++;
}
cout<<result<<endl;
} int main()
{
int i,j,temp,temp2,temp3;
while(scanf("%d %d",&V1,&V2)!=EOF)
{
memset(grid,0,sizeof(grid)); for(i=1;i<=V1;i++)
{
scanf("%d",&temp);
for(j=1;j<=temp;j++)
{
scanf("%d",&temp2);
grid[i][temp2]=1;
}
}
Magyarors();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 1325 && 1274:Machine Schedule 匈牙利算法模板题的更多相关文章
- poj 1274 The Perfect Stall【匈牙利算法模板题】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20874 Accepted: 942 ...
- HDU - 1150 POJ - 1325 Machine Schedule 匈牙利算法(最小点覆盖)
Machine Schedule As we all know, machine scheduling is a very classical problem in computer science ...
- POJ 3041 匈牙利算法模板题
一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分 ...
- 51Nod 飞行员配对(二分图最大匹配)(匈牙利算法模板题)
第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...
- POJ 1276 Cash Machine(完全背包模板题)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44409 Accepted: 16184 Description A B ...
- HDU 1083 - Courses - [匈牙利算法模板题]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1083 Time Limit: 20000/10000 MS (Java/Others) M ...
- 匈牙利算法模板 hdu 1150 Machine Schedule(二分匹配)
二分图:https://blog.csdn.net/c20180630/article/details/70175814 https://blog.csdn.net/flynn_curry/artic ...
- hdu 2063 过山车 (最大匹配 匈牙利算法模板)
匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最 ...
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
随机推荐
- flutter如何使用配置文件pubspec.yaml(位于项目根目录)来管理第三方依赖包
官方文档 在软件开发中,很多时候有一些公共的库或SDK可能会被很多项目用到,因此,将这些代码单独抽到一个独立模块,然后哪个项目需要使用时再直接集成这个模块,便可大大提高开发效率.很多编程语言或开发工具 ...
- emmmmmmmmmmmmmmmmmm01
当体会活着有多么难之后,就不要在那么随意的活着,今天有多么不在意自己的人生,明天就要加倍的被别的人左右自己的人生. 多思考,多学习,多总结,多创造.让自己成为有用的人,让自己未来有一天成为自己的主人.
- 配置 git公钥报错:unknown key type -rsa
配置 git公钥的时候出现:ssh-keygen unknown key type -rsa 一个解决办法是去本地寻找.ssh文件,参考路径(C:\Users\Administrator.ssh),把 ...
- [题解] LuoguP4091 [HEOI2016/TJOI2016]求和
传送门 首先我们来看一下怎么求\(S(m,n)\). 注意到第二类斯特林数的组合意义就是将\(m\)个不同的物品放到\(n\)个没有区别的盒子里,不允许有空盒子的方案数. 那么将\(m\)个不同的物品 ...
- docker-compose(grafana influxdb) + telegraf 快速搭建简单监控
灵活实现方案: 1: telegraf 为go 语言写得占用内存小 收集主机各项监控数据 定时写入 时序DB influxdb ------------------------&qu ...
- 将.py文件转化成.exe
机子上已经安装好python,且配置好环境变量 编写好xx.py文件 安装pywin32.此处一定注意pywin32有32位和64位之分.可以在命令提示符里输入python来查看python的版本以及 ...
- Redis 详解 (七) AOF 持久化
目录 1.AOF简介 2.AOF 配置 3.开启 AOF 4.AOF 文件恢复 5. AOF 重写 6.AOF的优缺点 上一篇文章我们介绍了Redis的RDB持久化,RDB 持久化存在一个缺点是一定时 ...
- bzoj 1962: 模型王子
呵呵呵呵http://wenku.baidu.com/link?url=o0CPVzuBDLJMt0_7Qph1T7TtdFOzu7O-apIpvaWbIYMz8ZWqBneGqI8LGtLdqpuK ...
- 095-PHP遍历关联数组,并修改数组元素值
<?php $arr=array('I'=>1,'II'=>2,'III'=>3,'IV'=>4,'V'=>5); //定义一个数组 echo '修改之前数组信息: ...
- 导出execl
string filepath = Utils.GetMapPath("/upload/excel/"); filepath = filepath + fileName + &qu ...