POJ 2239 Selecting Courses【最大匹配】

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   ar   

大意:

有一些課程,每個課程的上課時間在每周可能會有多節,你可以從中任選一個時間去聽課,

每周七天一天12節課  告訴你每節課的上課時間問在不衝突的情況下最多上幾節課?

分析:

左集合課程

右集合時間

邊為該節課對應的上課時間

求最大匹配就行了

 

代碼:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6  7 const int maxn = 305; 8 const int maxm = 12 * 7 + 10; 9 10 int mat[maxn][maxm];11 12 const int m = 12 * 7; int n; 13 14 int vis[maxm];15 int Link[maxm];16 bool Find(int u) {17     for(int i = 1; i <= m; i++) {18         if( mat[u][i] && !vis[i]) {19             vis[i] = 1;20             if(Link[i] == -1 || Find(Link[i])) {21                 Link[i] = u;22                 return true;23             }24         }25     }26     return false;27 }28 29 int solve() {30     memset(Link, -1, sizeof(Link));31     int cnt = 0;32     for(int i = 1; i <= n; i++) {33         memset(vis, 0, sizeof(vis));34         if(Find(i)) cnt++;35     }36     return cnt;37 }38 39 int main() {40     int t, a, b;41     while(EOF != scanf("%d",&n) ) {42         memset(mat, 0, sizeof(mat));43         for(int i = 1; i <= n; i++) {44             scanf("%d",&t);45             for(int j = 0; j < t; j++) {46                 scanf("%d %d",&a, &b);47                 mat[i][(a - 1) * 12 + b] = 1;48             }49         }50         printf("%d\n",solve());51     }52     return 0;53 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.