POJ - 3436 ACM Computer Factory 网络流
POJ-3436:http://poj.org/problem?id=3436
题意
组配计算机,每个机器的能力为x,只能处理一定条件的计算机,能输出特定的计算机配置。进去的要求有1,进来的计算机这个位子就要求为1,进去的要求有0,进来的计算机这个位子就要求为0.
思路
因为点上有容量限制,所以把每个点拆掉,连一条容量为这个机器的能力的边。源点向要求为0的机器连容量inf的边,把能完全组装好计算机的机器连向汇点。中间把符合条件的机器间连边,容量为inf;
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <iomanip> #include <cstdlib> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <cctype> #include <queue> #include <cmath> #include <list> #include <map> #include <set> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ #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 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 = 0x7FFFFFFFLL; //2147483647 const ll nmos = 0x80000000LL; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18 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; } // #define _DEBUG; //*// #ifdef _DEBUG freopen("input", "r", stdin); // freopen("output.txt", "w", stdout); #endif /*-----------------------show time----------------------*/ const int maxn = 100; int p,n; struct node{ int d; int in[maxn],out[maxn]; }a[maxn]; struct edge { int u,v,cap; edge(){} edge(int u,int v,int cap): u(u),v(v),cap(cap){} }es[200009]; int tot,s,t; vector<int>tab[10009]; int dis[10009],cur[10009]; void addedge(int u,int v,int cap){ tab[u].pb(tot); es[tot++] = edge(u,v,cap); tab[v].pb(tot); es[tot++] = edge(v,u,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)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(){ scanf("%d%d", &p, &n); for(int i=1; i<=n; i++){ scanf("%d", &a[i].d); addedge(i,i+n,a[i].d); int t1 = 0,t2 = 0; for(int j=1; j<=p; j++)scanf("%d",&a[i].in[j]), t1 += a[i].in[j]==1 ? 1:0; for(int j=1; j<=p; j++)scanf("%d",&a[i].out[j]),t2 += a[i].out[j]>0?1 : 0; if(t1 == 0) addedge(0, i, inf); if(t2 == p) addedge(i+n,n+n+1,inf); } for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ if(i == j)continue; int flag = 1; for(int t = 1; t <= p; t++){ if(a[j].in[t] == 1 && a[i].out[t] == 0) flag = 0; if(a[j].in[t] == 0 && a[i].out[t] == 1) flag = 0; } if(flag)addedge(i+n,j,inf); } } s = 0,t = n+n+1; printf("%d ", dinic()); int rere = 0; vector<p3>v; for(int i=1; i<=n; i++){ for(int j = 0; j < tab[i+n].size(); j++){ edge e = es[tab[i+n][j]]; if(e.cap < inf && i!= e.v&&e.v != t){ //printf("%d %d %d\n",i,e.v,inf - e.cap); v.pb(p3(i,pii(e.v,inf - e.cap))); rere ++; } } } printf("%d\n", rere); for(int i=0; i<rere; i++){ printf("%d %d %d\n", v[i].fi,v[i].se.fi,v[i].se.se); } return 0; }
skr