If that, given a network diagram, and the source and sink, each side units is known maximum flow and traffic charges, which determine the maximum flow and minimum cost network in the case of maximum flow.
The first row contains four positive integers N, M, S, T, respectively, the number of points, there are the number of edges, number source, sink point number.
Next M lines contains four integer ui, vi, wi, fi, denotes the i th ui from there to the edge and arriving at VI, the right side of wi (i.e., the side of the maximum flow rate Wi), per unit flow rate fee fi.
One line containing two integers, followed by the maximum flow and minimum cost at maximum flow conditions.
Input # 1copy
4 5 4 3 4 2 30 2 4 3 20 3 2 3 20 1 2 1 30 9 1 3 40 5
Output # 1copy
50 280
#include<bits/stdc++.h>
const int maxn = 5e4 + 10;
int head[maxn];
int vis[maxn];
int dis[maxn];
int flow[maxn];
int pre[maxn];
int last[maxn];
int tot;
int n, m, s, t;
using namespace std;
struct node{
int to;
int next;
int f;
int w;
node() {}
node(int a, int b, int c, int d) : to(a), next(b), f(c), w(d) {}
}edge[maxn * 10];
void edgeadd(int a, int b, int c, int d){
edge[tot] = node(b, head[a], c, d);
head[a] = tot++;
edge[tot] = node(a, head[b], 0, -d);
head[b] = tot++;
}
void init(){
memset(head, -1, sizeof(head));
tot = 0;
}
void spfainit(){
memset(vis, 0, sizeof(vis));
memset(dis, 0X3f3f, sizeof(dis));
memset(flow, 0x3f3f, sizeof(flow));
}
bool spfa(){
spfainit();
queue<int> q;
q.push(s);
pre[t] = -1;
vis[s] = 1;
dis[s] = 0;
//flow[s] = 0;
while(!q.empty()){
int nw = q.front();
q.pop();
vis[nw] = 0;
for(int i = head[nw]; i != -1; i = edge[i].next){
int y = edge[i].to;
int w = edge[i].w;
int f = edge[i].f;
if(f && dis[y] > dis[nw] + w){
dis[y] = dis[nw] + w;
flow[y] = min(flow[nw], f);
pre[y] = nw;
last[y] = i;
if(!vis[y]){
q.push(y);
vis[y] = 1;
}
}
}
}
return pre[t] != -1;
}
void dinic(){
int maxflow = 0, minw = 0;
while(spfa()){
maxflow += flow[t];
minw += flow[t] * dis[t];
int x = t;
while(x != s){
edge[last[x]].f -= flow[t];
edge[(last[x] ^ 1)].f += flow[t];
x = pre[x];
}
}
cout << maxflow << " " << minw << endl;
}
int main(){
ios::sync_with_stdio(false);
cin >> n >> m >> s >> t;
init();
for(int i = 1; i <= m; i++){
int a, b, c, d;
cin >> a >> b >> c >> d;
edgeadd(a, b, c, d);
}
dinic();
return 0;
}
Luo Valley P3381 [Template] Minimum cost maximum flow Idea: In a network, there is a traffic on each side. f f fAnd expense w w w, Indicating that the flow rate and cost of each unit flow require it. ...
Minimum cost maximum flow, template questions This question: 1, minimum cost maximum flow concept: There are many flow laws that get the maximum flow, and the requirements for the smallest cost. 2, th...
The biggest stream of minimum cost is to use greedy thoughts, each time augmented by the route of minimum cost. With the cost per unit of traffic for each side, SPFA looks for the shortest path from s...
Original title address: Description of the topic As the title, a network map is given, as well as its source and sink points. Each edge is known for its maximum flow and unit flow cost, and the maximu...
Topic link: ...
P3381 [template] minimum cost maximum flow Description of the topic As the title, a network map is given, as well as its source and sink points. Each edge is known for its maximum flow and unit flow c...
Title description For example, give a network diagram, as well as its source and sink points. Each side knows its maximum flow rate and unit flow rate, and finds the maximum flow rate of the network a...
Title description For example, give a network diagram, as well as its source and sink points. Each side knows its maximum flow rate and unit flow rate, and finds the maximum flow rate of the network a...
Title description For example, give a network diagram, as well as its source and sink points. Each side knows its maximum flow rate and unit flow rate, and finds the maximum flow rate of the network a...