Power Network

基本思路:建立s和t节点,对于所有的power station,建立从s到该节点的有向边,其capacity为power station的产量。对于所有的consumer,建立从该节点到t的有向边,其capacity为consumer的消费量。如此,将其转化为最大流问题,用Edmonds-Karp算法解决之。


代码:

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;


public class Main {
	public int[][] capacity = new int[102][102];
	public int nodeNumber;
	public int powerStationNumber;
	public int consumerNumber;
	public int transportLineNumber;
	public int startNode = 0;
	public int terminalNode;
	
	public Main() {
		for (int i = 0; i < capacity.length; i++)
			for (int j = 0; j <capacity[i].length; j++)
				capacity[i][j] = 0;
	}
	
	public int EdmondsKarp() {
		Queue<Integer> queue = new LinkedList<Integer>();
		boolean[] haveVisited = new boolean[nodeNumber + 2];
		int[] path = new int[nodeNumber + 2];
		int[][] flow = new int[nodeNumber + 2][nodeNumber + 2];
		int[] residualCapacity = new int[nodeNumber + 2];
		int con = 0;
		
		for (int i = 0; i < nodeNumber + 2; i++) {
			path[i] = -1;
			for (int j = 0; j < nodeNumber + 2; j++)
				flow[i][j] = 0;
		}
		
		while (true) {
			for (int i = 0; i < nodeNumber + 2; i++) {
				haveVisited[i] = false;
				residualCapacity[i] = Integer.MAX_VALUE;
			}
			
			haveVisited[startNode] = true;
			queue.clear();
			queue.offer(startNode);
			while (!queue.isEmpty()) {
				int currentNode = queue.remove();
				for (int i = 0; i < nodeNumber + 2; i++) {
					if (!haveVisited[i] && capacity[currentNode][i] > flow[currentNode][i]) {
						path[i] = currentNode;
						residualCapacity[i] = Math.min(residualCapacity[currentNode], capacity[currentNode][i] - flow[currentNode][i]);
						haveVisited[i] = true;
						queue.offer(i);
					}
				}
				if (haveVisited[terminalNode])
					break;
			}
			if (!haveVisited[terminalNode])
				break;
			for (int i = terminalNode; i != startNode; i = path[i]) {
				flow[path[i]][i] += residualCapacity[terminalNode];
				flow[i][path[i]] = -flow[path[i]][i];
			}
			con += residualCapacity[terminalNode];
		}
		
		return con;
	}
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		while (input.hasNext()) {
			Main poj = new Main();
			
			poj.nodeNumber = input.nextInt();
			poj.powerStationNumber = input.nextInt();
			poj.consumerNumber = input.nextInt();
			poj.transportLineNumber = input.nextInt();
			poj.startNode = 0;
			poj.terminalNode = poj.nodeNumber + 1;
			
			for (int i = 0; i < poj.transportLineNumber; i++) {
				int u, v, z;
				String buf = input.next();
				u = Integer.parseInt(buf.substring(1, buf.indexOf(',')));
				v = Integer.parseInt(buf.substring(buf.indexOf(',') + 1, buf.indexOf(')')));
				z = Integer.parseInt(buf.substring(buf.indexOf(')') + 1));
				poj.capacity[u + 1][v + 1] = z;
			}
			
			for (int i = 0; i < poj.powerStationNumber; i++) {
				int u, z;
				String buf = input.next();
				u = Integer.parseInt(buf.substring(1, buf.indexOf(')')));
				z = Integer.parseInt(buf.substring(buf.indexOf(')') + 1));
				poj.capacity[poj.startNode][u + 1] = z;
			}
			
			for (int i = 0; i < poj.consumerNumber; i++) {
				int u, z;
				String buf = input.next();
				u = Integer.parseInt(buf.substring(1, buf.indexOf(')')));
				z = Integer.parseInt(buf.substring(buf.indexOf(')') + 1));
				poj.capacity[u + 1][poj.terminalNode] = z;
			}
			
			System.out.println(poj.EdmondsKarp());
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值