POJ - 3436 ACM Computer Factory 網絡流
阿新 • • 發佈:2018-10-07
max math 中間 bug sed line || mes comm
POJ-3436:http://poj.org/problem?id=3436
題意
組配計算機,每個機器的能力為x,只能處理一定條件的計算機,能輸出特定的計算機配置。進去的要求有1,進來的計算機這個位子就要求為1,進去的要求有0,進來的計算機這個位子就要求為0.
思路
因為點上有容量限制,所以把每個點拆掉,連一條容量為這個機器的能力的邊。源點向要求為0的機器連容量inf的邊,把能完全組裝好計算機的機器連向匯點。中間把符合條件的機器間連邊,容量為inf;
#include <algorithm> #include <iterator> #includePOJ 3436<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"; #definepb 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; }
POJ - 3436 ACM Computer Factory 網絡流