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]能够描写叙述为"不为自身的最大首尾反复子串 ...
随机推荐
- STM32CubeIDE 编译C/C++程序
文章转自 https://www.cnblogs.com/skyofbitbit/p/3708216.html STM32CubeIDE 其实就是STM32CubeMx + eclipse 首先,W ...
- 剑指offer 按之字型顺序打印二叉树
题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 使用两个栈进行存储,我们在打印某一行节点 ...
- 机器学习(ML)八之正向传播、反向传播和计算图,及数值稳定性和模型初始化
正向传播 正向传播的计算图 通常绘制计算图来可视化运算符和变量在计算中的依赖关系.下图绘制了本节中样例模型正向传播的计算图,其中左下角是输入,右上角是输出.可以看到,图中箭头方向大多是向右和向上,其中 ...
- Day6 - 牛客203E
https://ac.nowcoder.com/acm/contest/203/E 埋坑不会做
- eshop-环境配置
1. iptables # Generated by iptables-save v1. :: #*filter #:INPUT ACCEPT [:] #:FORWARD ACCEPT [:] #:O ...
- 项目启动报错:Communications link failure
2017-12-29 10:43:19,776 ERROR [com.alibaba.druid.pool.DruidDataSource] - <init datasource error, ...
- PHP时间格式
date 用法: date(格式,[时间]); 如果没有时间参数,则使用当前时间.格式是一个字符串,其中以下字符有特殊意义: Y - 年,四位数字; 如: "1999" y - 年 ...
- oracle进入CDB
第一步:使用sys登陆 CONN sys/change_on_install AS SYSDBA; 第二步:查看现在的容器名称 SHOW con_name; 第三步:改变容器为PDB ALTER SE ...
- POJ 2823 滑动窗口 单调队列模板
我们从最简单的问题开始: 给定一个长度为N的整数数列a(i),i=0,1,...,N-1和窗长度k. 要求: f(i) = max{a(i-k+1),a(i-k+2),..., a(i)},i = 0 ...
- vue-cli 局域网可访问配置
vue-cli 使用官方脚手架搭建以后,本地的config配置已经没有了,默认打开localhost,无法ip访问 只要修改项目目录配置和防火墙配置 1.在项目根目录下面加一个文件vue.config ...