1. 程式人生 > >HDU 1045 Fire Net 二分圖建圖

HDU 1045 Fire Net 二分圖建圖

HDU 1045

題意:

  在一個n*n地圖中,有許多可以擋住子彈的牆,問最多可以放幾個炮臺,是的炮臺不會相互損害。炮臺會向四面發射子彈。

思路:

  把行列分開做,先處理行,把同一行中相互聯通的點縮成一個點。再處理列,同樣縮成一個點。然後把行列中,交點不是牆的點連一條邊。對這個圖跑網路流或者二分圖匹配即可。

 

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   
<cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include
<map> #include <set> #include <cassert> #include <unordered_map> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error "-std=c++11" // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue #define max3(a,b,c) max(max(a,b),c) typedef long long ll; typedef unsigned long long ull; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//這是一個大根堆q //priority_queue<int,vector<int>,greater<int> >q;//這是一個小根堆q #define fi first #define se second //#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用來壓行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ char mp[5][5]; int idx[5][5]; struct edge{ int u,v,cap,flag; edge(){} edge(int u,int v,int cap,int flag):u(u),v(v),cap(cap),flag(flag){} }es[100009]; int tot,s,t; vector<int>tab[2009]; int dis[2009],cur[2009]; void addedge(int u,int v,int cap){ // debug(u); tab[u].pb(tot); es[tot++] = edge(u,v,cap,1); tab[v].pb(tot); es[tot++] = edge(v,u,0,0); } bool bfs(){ queue<int>q; q.push(s); memset(dis,inf,sizeof(dis)); dis[s] = 0; while(!q.empty()){ int h = q.front(); q.pop(); for(int i=0; i<tab[h].size(); i++){ edge & e = es[tab[h][i]]; if(e.cap > 0 && dis[e.v] >= inf){ dis[e.v] = dis[h] + 1; q.push(e.v); } } } return dis[t] < inf; } int dfs(int x,int maxflow){ if(x == t || maxflow == 0) return maxflow; for(int i=cur[x] ; i<tab[x].size(); i++){ cur[x] = i; edge & e = es[tab[x][i]]; if(dis[e.v] == dis[x] + 1 && e.cap > 0){ int flow = dfs(e.v, min(maxflow, e.cap)); if(flow){ e.cap -= flow; es[tab[x][i] ^ 1].cap += flow; return flow; } } } return 0; } int dinic(){ int ans = 0; while(bfs()){ int flow; memset(cur,0,sizeof(cur)); do{ flow = dfs(s,inf); if(flow) ans += flow; }while(flow); } return ans; } int main(){ int n; scanf("%d", &n); while(~scanf("%d", &n) && n){ memset(idx, -1, sizeof(idx)); tot = 0; for(int i=0; i<n; i++) scanf("%s", mp[i]); s = 0, t = 1000; for(int i=s; i<=t; i++)tab[i].clear(); int totx = 1; for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(mp[i][j] == '.') idx[i][j] = totx; else totx++; } totx++; } int p = totx; for(int i=1; i<totx; i++) addedge(s, i, 1); for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(mp[j][i] == '.' ) { // vis[idx[j][i]] = 1; addedge(idx[j][i], totx, 1); } else if(mp[j][i] == 'X') totx++; } totx++; } for(int i=p; i<=totx; i++) addedge(i, t, 1); printf("%d\n", dinic()); } return 0; }
HDU 1045