Pagini recente » Cod sursa (job #2451062) | Cod sursa (job #3241797) | Cod sursa (job #3180202) | Cod sursa (job #3004994) | Cod sursa (job #2710681)
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main {
private static int[] prio;
static class Vertex {
public int index_, val ;
Vertex (int index_ , int val ) {
this.index_ = index_ ;
this.val = val ;
}
}
static class Edge {
int to, f, cap, cost, rev;
Edge(int v, int cap, int cost, int rev) {
this.to = v;
this.cap = cap;
this.cost = cost;
this.rev = rev;
}
}
public static List<Edge>[] createGraph(int n) {
Vector<Edge>[] graph = new Vector[n];
for (int i = 0; i < n; i++)
graph[i] = new Vector<>();
return graph;
}
public static void addEdge(List<Edge>[] graph, int s, int t, int cap, int cost) {
graph[s].add(new Edge(t, cap, cost, graph[t].size()));
graph[t].add(new Edge(s, 0, -cost, graph[s].size() - 1));
}
static void bellmanFord(List<Edge>[] graph, int s, int[] dist) {
int n = graph.length;
Arrays.fill(dist, Integer.MAX_VALUE);
dist[s] = 0;
boolean[] inqueue = new boolean[n];
ArrayDeque<Integer> dq = new ArrayDeque<>();
dq.add(s);
while (0 < dq.size() ) {
int u = dq.poll();
inqueue[u] = false;
for (int i = 0; i < graph[u].size(); i++) {
Edge e = graph[u].get(i);
if (e.cap <= e.f)
continue;
int v = e.to;
int ndist = dist[u] + e.cost;
if (dist[v] > ndist) {
dist[v] = ndist;
if (!inqueue[v]) {
inqueue[v] = true;
dq.add(v);
}
}
}
}
}
static class VertexComparator implements Comparator<Vertex>{
public int compare (Vertex i , Vertex j ) {
if ( prio[i.index_] > prio[j.index_] )
return 1 ;
if ( prio[i.index_] < prio[j.index_] )
return -1 ;
return 0 ;
}
}
public static int[] minCostFlow(List<Edge>[] graph, int s, int t, int maxf) {
int n = graph.length;
prio = new int[n];
int[] curflow = new int[n];
int[] prevedge = new int[n];
int[] prevnode = new int[n];
int[] pot = new int[n];
bellmanFord(graph, s, pot);
int flow = 0;
int flowCost = 0;
while (flow < maxf) {
PriorityQueue<Vertex> q = new PriorityQueue<>(new VertexComparator());
q.add(new Vertex(s,0));
Arrays.fill(prio, Integer.MAX_VALUE);
prio[s] = 0;
boolean[] finished = new boolean[n];
curflow[s] = Integer.MAX_VALUE;
while (!finished[t] && !q.isEmpty()) {
Vertex cur = q.remove();
int u = cur.index_;
finished[u] = true;
for (int i = 0; i < graph[u].size(); i++) {
Edge e = graph[u].get(i);
if (e.f >= e.cap)
continue;
int v = e.to;
int nprio = prio[u] + e.cost + pot[u] - pot[v];
if (prio[v] > nprio) {
prio[v] = nprio;
q.add(new Vertex(v,nprio));
prevnode[v] = u;
prevedge[v] = i;
curflow[v] = Math.min(curflow[u], e.cap - e.f);
}
}
}
if (prio[t] == Integer.MAX_VALUE)
break;
for (int i = 0; i < n; i++)
if (finished[i])
pot[i] += prio[i] - prio[t];
int df = Math.min(curflow[t], maxf - flow);
flow += df;
for (int v = t; v != s; v = prevnode[v]) {
Edge e = graph[prevnode[v]].get(prevedge[v]);
e.f += df;
graph[v].get(e.rev).f -= df;
flowCost += df * e.cost;
}
}
return new int[]{flow, flowCost};
}
public static void main(String[] args) throws FileNotFoundException {
InputReader in = new InputReader(new FileInputStream("fmcm.in"));
PrintWriter out = new PrintWriter(new FileOutputStream("fmcm.out"));
int n = in.nextInt(), m = in.nextInt();
int source = in.nextInt() - 1, destination = in.nextInt() - 1;
List<Edge>[] graph = createGraph(n);
while (m-- > 0) {
int x = in.nextInt() - 1;
int y = in.nextInt() - 1;
int cost = in.nextInt();
int capacity = in.nextInt();
addEdge(graph, x, y, cost, capacity);
}
out.println(minCostFlow(graph, source, destination, Integer.MAX_VALUE)[1]);
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}