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>
 9 #include<vector>
10 #include<map>
11 #include<stack>
12 #include<string>
13
14 using namespace std;
15
16 int n,m,k;
17 int f[101][101];
18 int link[101];
19 bool adj[101];
20 bool used[101];
21
22 bool work(int x){
23     for (int i=1;i<m;i++){
24             if (f[x][i] && adj[i]==false){
25                     adj[i]=true;
26                     if (!used[i] || work(link[i])){
27                             link[i]=x;
28                             used[i]=1;
29                             //printf("%d %d\n",x,i);
30                             return true;
31                     }
32             }
33     }
34     return false;
35 }
36
37 int main(){
38     while (scanf("%d%d%d",&n,&m,&k)==3){
39             memset(f,0,sizeof(f));
40             memset(used,0,sizeof(used));
41             for (int i=0;i<k;i++){
42                     int x,y,z;
43                     scanf("%d%d%d",&z,&x,&y);
44                     if (x!=0 && y!=0){
45                             f[x][y]=1;
46                     }
47             }
48             int ans=0;
49             for (int i=1;i<n;i++){
50                     memset(adj,0,sizeof(adj));
51                     if (work(i)) ans++;
52             }
53             //for (int i=1;i<m;i++) printf("%d %d\n",i,link[i]);
54             printf("%d\n",ans);
55     }
56     return 0;
57 }
58 /*
59 5 5 10
60 0 1 1
61 1 1 2
62 2 1 3
63 3 1 4
64 4 2 1
65 5 2 2
66 6 2 3
67 7 2 4
68 8 3 3
69 9 4 3
70 5 5 10
71 0 1 1
72 1 1 2
73 2 1 3
74 3 1 4
75 4 2 1
76 5 2 2
77 6 2 3
78 7 2 4
79 8 3 3
80 9 4 3
81 0
82 */

 1 //hdu 1151
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<cstring>
 7 #include<cstdlib>
 8 #include<queue>
 9 #include<vector>
10 #include<map>
11 #include<stack>
12 #include<string>
13
14 using namespace std;
15
16 int T;
17 int n,m;
18 bool f[121][121];
19 int link[121];
20 bool adj[121];
21 bool used[121];
22
23 bool work(int x){
24     for (int i=1;i<=n;i++){
25             if (f[x][i] && !adj[i]){
26                     adj[i]=1;
27                     if (!used[i] || work(i)){
28                             link[i]=x;
29                             used[i]=1;
30                             return true;
31                     }
32             }
33     }
34     return false;
35 }
36
37 int main(){
38     scanf("%d",&T);
39     for (int cas=1;cas<=T;cas++){
40             memset(used,0,sizeof(used));
41             memset(link,0,sizeof(link));
42             memset(f,0,sizeof(f));
43             scanf("%d",&n);
44             scanf("%d",&m);
45             for (int i=0;i<m;i++){
46                     int x,y;
47                     scanf("%d%d",&x,&y);
48                     f[x][y]=1;
49             }
50             int ans=n;
51             for (int i=1;i<=n;i++){
52                     memset(adj,0,sizeof(adj));
53                     if (work(i)) ans--;
54             }
55             printf("%d\n",ans);
56     }
57     return 0;
58 }
59 /*
60 2
61 4
62 3
63 3 4
64 1 3
65 2 3
66 3
67 3
68 1 3
69 1 2
70 2 3
71 */

时间: 2024-10-27 05:52:59

hdu 1150 Machine Schedule hdu 1151 Air Raid 匈牙利模版的相关文章

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(二分匹配,简单匈牙利算法)

题目链接: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(最小顶点覆盖)

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 最少点覆盖转化为最大匹配

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 最少点覆盖

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 (二分匹配)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

HDU——1150 Machine Schedule

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

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

加深了我对最小点覆盖的理解 将每个任务的两台机器连线,问题是选取最少的点来覆盖所有的连线,即最小点覆盖 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