poj3436-ACM Computer Factory - smartxxyx的专栏 - 博客频道 - CSDN.NET

文章价值(3) ?
阅读数(324) ?

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?j,?Di,k?— output specification for part?k.

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, 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 i'=2*i和i''=2*i+1
			 */
			for(int i=0;ij 连接成 i''->j'
			 */
			for(int i=0;i0)
					{
						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 queue=new LinkedList();
			queue.add(s);
			parent[s]=s;

			while(!queue.isEmpty())
			{
				int p=queue.poll();
				if(p==t)
					break;
				for(int i=0;i0)
					{
						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;
		}
	}
}




版权声明:本文为博主原创文章,未经博主允许不得转载。





(注:此网站文章大都来自公开的博客,如有原作者发现有侵权问题,请在此留言,我们会立刻删除相关文章。)

我的评价: