POJ3436 ACM Computer Factory 拆點+網路最大流

來源:互聯網
上載者:User
這道題目的意思我沒看懂。看瞭解釋後,大概明白這題的意思是有一個工廠,它裡面有多個裝配電腦的線路,每個線路有一個最大的電腦容量數,然後有2p個值,分別對應輸入和輸出。0表示不能有,1表示必須有,2表示無所謂有和沒有。最終要我們求出最多的組裝數,和線路的串連數並輸出方案。 只有輸出全是1的才算組裝好一台電腦。 這裡我們要拆點,把一個線路拆成兩個點i和i+n,並且在兩點間連一條弧,容量為相應電腦最大容納量,然後如果輸入沒有1則i和源點相連容量INF,然後如果輸出全是1,則i+n和匯點相連容量為INF。然後如果i的輸出和j的輸入能夠對應滿足要求,則將i+n與j相連,容量為INF。最後求一個最大流即可。然後在對剩餘網路進行搜尋即可ACM Computer Factory
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4073   Accepted: 1363   Special Judge

Description

As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory.

Every ACM computer consists of P parts. When all these parts are present, the computer is ready and can be shipped to one of the numerous ACM contests.

Computer manufacturing is fully automated by using N various machines. Each machine removes some parts from a half-finished computer and adds some new parts (removing of parts is sometimes necessary as the parts cannot be added to a computer in
arbitrary order). Each machine is described by its performance (measured in computers per hour), input and output specification.

Input specification describes which parts must be present in a half-finished computer for the machine to be able to operate on it. The specification is a set ofP numbers 0, 1 or 2 (one number for each part), where 0 means that corresponding part
must not be present, 1 — the part is required, 2 — presence of the part doesn't matter.

Output specification describes the result of the operation, and is a set of
P
numbers 0 or 1, where 0 means that the part is absent, 1 — the part is present.

The machines are connected by very fast production lines so that delivery time is negligibly small compared to production time.

After many years of operation the overall performance of the ACM Computer Factory became insufficient for satisfying the growing contest needs. That is why ACM directorate decided to upgrade the factory.

As different machines were installed in different time periods, they were often not optimally connected to the existing factory machines. It was noted that the easiest way to upgrade the factory is to rearrange production lines. ACM directorate decided to
entrust you with solving this problem.

Input

Input file contains integers P N, then N descriptions of the machines. The description ofith machine is represented as by 2
P + 1 integers Qi Si,1Si,2...Si,PDi,1Di,2...Di,P, whereQi specifies performance,
Si,j — input specification for partj,
Di,k — output specification for partk.

Constraints

1 ≤ P ≤ 10, 1 ≤ N ≤ 50, 1 ≤ Qi ≤ 10000

Output

Output the maximum possible overall performance, then M — number of connections that must be made, thenM descriptions of the connections. Each connection between machines
A and B must be described by three positive numbers A B W, whereW is the number of computers delivered from
A to B per hour.

If several solutions exist, output any of them.

Sample Input

Sample input 13 415  0 0 0  0 1 010  0 0 0  0 1 130  0 1 2  1 1 13   0 2 1  1 1 1Sample input 23 55   0 0 0  0 1 0100 0 1 0  1 0 13   0 1 0  1 1 01   1 0 1  1 1 0300 1 1 2  1 1 1Sample input 32 2100  0 0  1 0200  0 1  1 1

Sample Output

Sample output 125 21 3 152 3 10Sample output 24 51 3 33 5 31 2 12 4 14 5 1Sample output 30 0
#include<iostream>#include<cstdio>using namespace std;#define MAXN 200 #define INF 0xFFFFFFstruct edge{int to,c,next;}e[99999];struct edge2{int u,v,f;}ee[99999];int pi[15][MAXN],po[15][MAXN];int que[1000], dis[MAXN],pre[MAXN];int head[MAXN],head2[MAXN];int st,ed,maxflow;int en;int n,p;void add(int a,int b,int c){e[en].to=b;e[en].c=c;e[en].next=head[a];head[a]=en++;e[en].to=a;e[en].c=0;e[en].next=head[b];head[b]=en++;} bool bfs() {memset(dis,-1,sizeof(dis));que[0]=st,dis[st]=1;int t=1,f=0;while(f<t){int j=que[f++];for(int k=head[j];k!=-1;k=e[k].next){int i=e[k].to;if(dis[i]==-1 && e[k].c){que[t++]=i;dis[i]=dis[j]+1;if(i==ed) return true;}}}return false; } int update()  {  int p,flow=INF;      for (int i=pre[ed];i!=-1;i=pre[i])if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;        for (int i=pre[ed];i!=-1;i=pre[i]) e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;       maxflow+=flow;     return p;}  void dfs(){  memset(pre,-1,sizeof(pre));memcpy(head2,head,sizeof(head2));      for(int i=st,j;i!=-1;)      {          int flag=false;          for(int k=head[i];k!=-1;k=e[k].next)              if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )            {                  pre[j]=i; head2[i]=k;  i=j; flag=true;                  if(i==ed) i=update();                  if(flag) break;            }          if (!flag) dis[i]=-1,i=pre[i];       }  } void solve(){bool flag=true;int temp;st=0,ed=n*2+1;en=0;maxflow=0;memset(head,-1,sizeof(head));for(int i=1;i<=n;i++){scanf("%d",&temp);add(i,i+n,temp);flag=true;for(int j=1;j<=p;j++){scanf("%d",&pi[i][j]);if(pi[i][j]==1)flag=false;}if(flag)add(st,i,INF);flag=true;for(int j=1;j<=p;j++){scanf("%d",&po[i][j]);if(!po[i][j])flag=false;}if(flag)add(i+n,ed,INF);}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){flag=true;if(i==j) continue;for(int k=1;k<=p;k++){if(po[i][k]==1 && pi[j][k]==0)flag=false;if(po[i][k]==0 && pi[j][k]==1)flag=false;if(!flag) break;}if(flag)add(i+n,j,INF);}}while(bfs())dfs();int counts=0;for(int i=1;i<=n;i++){for(int j=head[i+n];j!=-1;j=e[j].next){if(e[j].to==i || e[j].to==ed)continue;if(e[j^1].c){ee[counts].u=i;ee[counts].v=e[j].to;ee[counts++].f=e[j^1].c;}}}printf("%d %d\n",maxflow,counts);for(int i=0;i<counts;i++)printf("%d %d %d\n",ee[i].u,ee[i].v,ee[i].f);}int main(){while(scanf("%d%d",&p,&n)!=EOF) solve();return 0;} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.