ACM-ICPC Live Archive 6811 - Irrigation Lines

来源:互联网 发布:软件下载系统网站源码 编辑:程序博客网 时间:2024/05/23 11:52
怎么读题时就没意识到“每行每列各有一个水阀”?这是把问题转化成二分图模型的关键啊

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4823


#include <iostream>#include <cstring>#include <cstdio>using namespace std; const int MAXN = 110;char graph[MAXN][MAXN];bool visited[MAXN];int nCase, cCase, use[MAXN], m, n; void init() {    memset(use, -1, sizeof(use));} void input() {    scanf("%d%d", &m, &n);    for (int i = 0; i < m; i++) {        scanf("%s", graph[i]);    }} bool find(int x) {    for (int j = 0; j < n; j++) {        if (graph[x][j] == '1' && !visited[j]) {            visited[j] = true;             if (use[j] == -1 || find(use[j])) {                use[j] = x;                return true;            }        }    }    return false;} int match() {    int count = 0;    for (int i = 0; i < m; i++) {        memset(visited, false, sizeof(visited));        if (find(i)) count++;    }    return count;} void solve() {    printf("Case #%d: %d\n", ++cCase, match());} int main() {    scanf("%d", &nCase);    while (nCase--) {        init();        input();        solve();     }    return 0;}


0 0
原创粉丝点击