HDU 1150

又一二分图题,虽然从题目不能直接看出来,但题目意思是求出最小重启次数,即找出最少的点将所有工作覆盖,即二分图的最小点覆盖。

最小点覆盖=最大匹配,所以直接是匈牙利算法,机器A、B分别看作二分图左右顶点,边表示可完成工作的两种模式。

 1 //最小点覆盖=最大匹配
 2
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 #define max 510
 7
 8 int stu[max][max],visit[max],linker[max],n,m,i,j,k,res;
 9
10 bool dfs(int i){
11     int j;
12     for(j=n;j<n+m;j++){
13         if(stu[i][j]==1&&visit[j]==0){
14             visit[j]=1;
15             if(linker[j]==-1||dfs(linker[j])){
16                 linker[j]=i;
17                 return true;
18             }
19         }
20     }
21     return false;
22 }
23
24 int main(){
25     while(scanf("%d",&n)!=EOF&&n!=0){
26         scanf("%d%d",&m,&k);
27         memset(stu,0,sizeof(stu));
28         memset(linker,-1,sizeof(linker));
29         res=0;
30         while(k){
31             scanf("%d%d%d",&i,&i,&j);
32             stu[i-1][j+n-1]=1;   //从0开始
33             k--;
34         }
35         for(i=0;i<n;i++){
36             memset(visit,0,sizeof(visit));
37             if(dfs(i)) res++;
38         }
39         printf("%d\n",res);
40     }
41     return 0;
42 }

时间: 2024-10-13 01:22:54

HDU 1150的相关文章

HDU 1150:Machine Schedule(二分匹配,匈牙利算法)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5371    Accepted Submission(s): 2658 Problem Description As we all know, machine scheduling is a very classical problem in compu

hdu 1150 Machine Schedule(二分图-最小顶点覆盖)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5424    Accepted Submission(s): 2691 Problem Description As we all know, machine scheduling is a very classical problem in compu

HDU 1150 最小点覆盖数

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5614    Accepted Submission(s): 2804 Problem Description As we all know, machine scheduling is a very classical problem in comput

hdu 1150 Machine Schedule hdu 1151 Air Raid 匈牙利模版

//两道大水……哦不 两道结论题 结论:二部图的最小覆盖数=二部图的最大匹配数 有向图的最小覆盖数=节点数-二部图的最大匹配数 1 //hdu 1150 2 #include<cstdio> 3 #include<iostream> 4 #include<cmath> 5 #include<algorithm> 6 #include<cstring> 7 #include<cstdlib> 8 #include<queue>

hdu 1150 Machine Schedule 最少点覆盖转化为最大匹配

Machine Schedule Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1150 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history.

hdu 1150 Machine Schedule(二分匹配,简单匈牙利算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1150 Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6733    Accepted Submission(s): 3375 Problem Description As we all know, mach

hdu - 1150 Machine Schedule (二分图匹配最小点覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=1150 有两种机器,A机器有n种模式,B机器有m种模式,现在有k个任务需要执行,没切换一个任务机器就需要重启一次, 如果任务i在机器A上执行,A机器需要一个对应的模式A,如果在机器B上执行,机器A需要一个模式B. 一直就是机器A在切换模式,现在让你安排一种合理的任务执行顺序,让机器重启次数最少. 每个任务之间连一条边.二分图的最小顶点覆盖就是求最少的点可以连接所有的边,然后转化成最大匹配即可. 这题就是初始状态

hdu 1150 Machine Schedule 最少点覆盖

Machine Schedule Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1150 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history.

hdu 1150 Machine Schedule(最小顶点覆盖)

pid=1150">Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5424    Accepted Submission(s): 2691 Problem Description As we all know, machine scheduling is a very classical pro

HDU 1150 - Machine Schedule

加深了我对最小点覆盖的理解 将每个任务的两台机器连线,问题是选取最少的点来覆盖所有的连线,即最小点覆盖 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <vector> 5 using namespace std; 6 const int maxn=105; 7 vector<int>map[105]; 8 int link[maxn],vis[m