码迷,mamicode.com
首页 > 其他好文 > 详细

poj3436-ACM Computer Factory

时间:2014-05-23 00:41:58      阅读:355      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   c   code   

ACM Computer Factory
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5079   Accepted: 1740   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 of P 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 of ith machine is represented as by 2 P + 1 integers Qi Si,1 Si,2...Si,P Di,1 Di,2...Di,P, where Qi specifies performance, Si,j — input specification for part jDi,k — output specification for part k.

Constraints

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

Output

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

If several solutions exist, output any of them.

Sample Input

Sample input 1
3 4
15  0 0 0  0 1 0
10  0 0 0  0 1 1
30  0 1 2  1 1 1
3   0 2 1  1 1 1
Sample input 2
3 5
5   0 0 0  0 1 0
100 0 1 0  1 0 1
3   0 1 0  1 1 0
1   1 0 1  1 1 0
300 1 1 2  1 1 1
Sample input 3
2 2
100  0 0  1 0
200  0 1  1 1

Sample Output

Sample output 1
25 2
1 3 15
2 3 10
Sample output 2
4 5
1 3 3
3 5 3
1 2 1
2 4 1
4 5 1
Sample output 3
0 0

Hint

Bold texts appearing in the sample sections are informative and do not form part of the actual data.

这题的关键在于建图,然后最大流算法,而建图的关键在于拆点。将每个点拆为两个点,两点之间的流为performance。和其他点相连的边容量为INF。

import java.util.*;
public class ACMComputerFactory3436_maxflow_EK {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Scanner in=new Scanner(System.in);
		int T=0;
		while(in.hasNext())
		{
			//in.nextLine();
			int P=in.nextInt();
			int N=in.nextInt();
			int performance[]=new int[N];
			int input[][]=new int[N][P];
			int output[][]=new int[N][P];
			int M=2*N+2;
			int map[][]=new int[M][M];
			for(int i=0;i<map.length;i++)
			{
				Arrays.fill(map[i], 0);
			}
			
			for(int n=0;n<N;n++)
			{
				performance[n]=in.nextInt();
				for(int i=0;i<P;i++)
				{
					input[n][i]=in.nextInt();
				}
				
				for(int i=0;i<P;i++)
				{
					output[n][i]=in.nextInt();
				}
			}
			/**
			 * 拆点,i-> i'=2*i和i''=2*i+1
			 */
			for(int i=0;i<N;i++)
			{
				map[2*i][2*i+1]=performance[i];
				map[2*i+1][2*i]=performance[i];
			}
			
			/**
			 * 建图,i->j 连接成 i''->j'
			 */
			for(int i=0;i<N;i++)
			{
				for(int j=0;j<N;j++)
				{
					if(i!=j)
					{
						int c=0;
						for(int k=0;k<P;k++)
						{
							if(output[i][k]==input[j][k]||input[j][k]==2)
							{
								c++;
							}
						}
						if(c==P)
						{
							map[2*i+1][2*j]=Integer.MAX_VALUE;
						}
					}
				}
			}
			/**
			 * 连接源点
			 */
			for(int i=0;i<N;i++)
			{
				int c=0;
				for(int k=0;k<P;k++)
				{
					if(input[i][k]!=1)
						c++;
				}
				if(c==P)
				{
					map[M-2][2*i]=Integer.MAX_VALUE;
				}
			}
			/**
			 * 连接汇点
			 */
			for(int i=0;i<N;i++)
			{
				int c=0;
				for(int k=0;k<P;k++)
				{
					if(output[i][k]==1)
						c++;
				}
				if(c==P)
				{
					map[2*i+1][M-1]=Integer.MAX_VALUE;
				}
			}
			
			FordFulkerson ford=new FordFulkerson(M);
			int maxflow=ford.edmondsKarpMaxFlow(map, M-2, M-1);
			int flowNetwork[][]=ford.getFlowNetwork();
			int connections=0;
			StringBuilder sb=new StringBuilder();
			for(int i=0;i<M-2;i++)
			{
				for(int j=0;j<M-2;j++)
				{
					if(flowNetwork[i][j]>0)
					{
						if(i/2!=j/2)
						{	
							sb.append(i/2+1).append(" ").append(j/2+1).append(" ").append(flowNetwork[i][j]).append("\n");
							connections++;
						}
					}
				}
			}
			//System.out.println("Sample output "+(++T));
			System.out.println(maxflow +" "+connections);
			System.out.println(sb.toString());
			//in.nextLine();
		}
		
	}

	
	public static class FordFulkerson
	{
		private int residualNetwork[][]=null;
		private int flowNetwork[][]=null;
		
		public final int N;
		int parent[];
		public FordFulkerson(int N)
		{
			this.N=N;
			parent=new int[N];
		}
		/**
		 * 实现FordFulkerson方法的一种算法——edmondsKarp算法
		 * @param graph
		 * @param s
		 * @param t
		 * @return
		 */
		public int edmondsKarpMaxFlow(int graph[][],int s,int t)
		{
			int length=graph.length;
			int f[][]=new int[length][length];
			for(int i=0;i<length;i++)
			{
				Arrays.fill(f[i], 0);
			}
			int r[][]=residualNetwork(graph,f);
			int result=augmentPath(r,s,t);
			
			int sum=0;
			
			while(result!=-1)
			{
				int cur=t;
				while(cur!=s)
				{
					f[parent[cur]][cur]+=result;
					f[cur][parent[cur]]=-f[parent[cur]][cur];
					r[parent[cur]][cur]-=result;
					r[cur][parent[cur]]+=result;
					cur=parent[cur];
				}
				
				sum+=result;
				result=augmentPath(r,s,t);
			}
			
			residualNetwork=r;
			flowNetwork=f;
			
			return sum;
		}

		/**
		 * deepCopy
		 * @param c
		 * @param f
		 * @return
		 */
		private int[][] residualNetwork(int c[][],int f[][]) {
			int length=c.length;
			int r[][]=new int[length][length];
			for(int i=0;i<length;i++)
			{
				for(int j=0;j<length;j++)
				{
					r[i][j]=c[i][j]-f[i][j];
				}
			}
			
			return r;
		}

		/**
		 * 广度优先遍历,寻找增光路径,也是最短增广路径
		 * @param graph
		 * @param s
		 * @param t
		 * @return
		 */
		public int augmentPath(int graph[][],int s,int t)
		{
			
			int maxflow=Integer.MAX_VALUE;
			Arrays.fill(parent, -1);
			Queue<Integer> queue=new LinkedList<Integer>();
			queue.add(s);
			parent[s]=s;

			while(!queue.isEmpty())
			{
				int p=queue.poll();
				if(p==t)
					break;
				for(int i=0;i<graph.length;i++)
				{
					if(i!=p&&parent[i]==-1&&graph[p][i]>0)
					{
						if(maxflow>graph[p][i])
							maxflow=graph[p][i];
						//flow[i]=Math.min(flow[p], graph[p][i]);
						parent[i]=p;
						queue.add(i);
					}
				}
			}
			if(parent[t]==-1)
				return -1;
			return  maxflow;
			
		}

		public int[][] getResidualNetwork() {
			return residualNetwork;
		}

		public int[][] getFlowNetwork() {
			return flowNetwork;
		}
	}
}




poj3436-ACM Computer Factory,布布扣,bubuko.com

poj3436-ACM Computer Factory

标签:des   style   blog   class   c   code   

原文地址:http://blog.csdn.net/smartxxyx/article/details/26576809

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!