基本思路:建立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());
}
}
}