IRRIGATION LINES |
Time Limit: 2000ms, Special Time Limit:5000ms, Memory Limit:65536KB |
Total submit users: 6, Accepted users: 6 |
Problem 13449 : No special judgement |
Problem description |
A plantation consists of several rectangular fields, and a field is further subdivided into square zones. Under the multi-zone crop rotation, some zones are planted with crops, while some other zones are left fallow during a season. The zones of a field are rotated in this manner so that every few seasons, a zone would rest and be fallow. Crops of a season are selected based on their type, form, shape, and sun or shade requirements. |
Input |
The first line contains an integer T (T ≤ 100) denoting the number of cases. Each case describes a layout of a planting field, which starts on a new line with a pair of positive integers M and N (1 ≤ M, N ≤ 100), indicating the dimension of the field, i.e., the number of rows and columns of zones, respectively. The integers are separated by space. The next M lines of each test case delineate the layout of the field containing either a 1 or a 0, where 1 indicates the zone is planted and a 0 if it is fallow. |
Output |
For each case, output “Case #X: Y” (without quotes) in a line where X is the case number, starting from 1, followed by a single space, and Y is the minimum number of irrigation lines that must be active. |
Sample Input |
2 4 4 0010 0101 0010 0000 5 4 1001 0010 1100 1110 0101 |
Sample Output |
Case #1: 2 Case #2: 4 |
Problem Source |
ACM ICPC Asia Kuala Lumpur Regional Contest 2014 |
题目大意:给出n*m个区域,1代表这里有庄家,现在要灌溉庄稼,灌溉管道只能横着和竖着铺,输出最少的灌溉管道。YY
这个真是好题!用二分图的最大匹配解决。二分图的最小顶点覆盖=最大匹配数,以横坐标和纵坐标建图,横纵坐标的交点为1 代表有关连。
/* *********************************************** Author :pk28 Created Time :2015/10/6 12:25:40 File Name :10.6hnul.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 10000+10 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; bool cmp(int a,int b){ return a>b; } struct node{ int v,next; }edge[maxn]; int pre[maxn],l,vis[maxn],match[maxn]; int tot,n,m; void init(){ l=0; memset(pre,-1,sizeof pre); memset(match,-1,sizeof match); } void add(int u,int v){ edge[l].v=v; edge[l].next=pre[u]; pre[u]=l++; } int dfs(int u){ for(int i=pre[u];i+1;i=edge[i].next){ int v=edge[i].v; if(!vis[v]){ vis[v]=1; if(match[v]==-1||dfs(match[v])){ match[v]=u; return 1; } } } return 0; } int hungary(){ tot=0; for(int i=1;i<=max(n,m);i++){ cle(vis); if(dfs(i))tot++; } return tot; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int t,a,cnt=0; cin>>t; while(t--){ init(); cin>>n>>m; getchar(); char s[110]; for(int i=1;i<=n;i++){ gets(s); for(int j=0;j<m;j++){ if(s[j]=='1'){ add(i,j+1); // add(j+1,i); } } } printf("Case #%d: %d ",++cnt,hungary()); } return 0; }