A: 石油采集
刚开始题目读错了,乱交了4发,然后终于读对题目,想用匈牙利算法跑2分匹配,但是比赛的时候不会跑,赛后学了一下,补了一下
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 55*55; 4 string str[55]; 5 int cnt = 0, tot, n; 6 int dx[4] = {1,-1,0,0}; 7 int dy[4] = {0,0,1,-1}; 8 int head[N], to[N], nt[N], link[N]; 9 bool vis[N]; 10 int id(int x, int y) 11 { 12 return x*n+y; 13 } 14 void add_edge(int u, int v) 15 { 16 to[tot] = v; 17 nt[tot] = head[u]; 18 head[u] = tot++; 19 } 20 bool hun(int u) 21 { 22 for(int i = head[u]; ~i; i = nt[i]){ 23 if(!vis[to[i]]){ 24 vis[to[i]] = 1; 25 if(-1 == link[to[i]]|| hun(link[to[i]])){ 26 link[u] = to[i]; 27 link[to[i]]=u; 28 return 1; 29 } 30 } 31 } 32 return 0; 33 } 34 int main() 35 { 36 ios::sync_with_stdio(false); 37 cin.tie(0); 38 cout.tie(0); 39 int T, Case = 1; 40 cin >> T; 41 while(T--) 42 { 43 tot = 0, cnt = 0; 44 memset(head, -1, sizeof(head)); 45 memset(link, -1, sizeof(link)); 46 cin >> n; 47 for(int i = 0; i < n; i++) 48 cin >> str[i]; 49 for(int i = 0; i < n; i++){ 50 for(int j = 0; j < n; j++){ 51 if(str[i][j] == '#'){ 52 for(int k = 0; k < 4; k++){ 53 int tx = i + dx[k]; 54 int ty = j + dy[k]; 55 if(tx < 0 || tx >= n || ty < 0 || ty >= n) continue; 56 if(str[tx][ty] == '#') add_edge(id(i,j), id(tx,ty)); 57 } 58 } 59 } 60 } 61 for(int i = 0; i < n; i++){ 62 for(int j = 0; j < n; j++){ 63 if(str[i][j] == '#'){ 64 if(-1 == link[id(i,j)]){ 65 memset(vis, 0, sizeof(vis)); 66 if(hun(id(i,j))) 67 cnt++; 68 } 69 } 70 } 71 } 72 cout <<"Case " << Case++ <<": " <<cnt << endl; 73 } 74 return 0; 75 }