text
stringlengths 49
983k
|
|---|
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define INF (1e9+1)
//#define INF (1LL<<59)
#define MAX_V 100
struct edge { int to, cap, cost,rev; };
int V; // ????????°
vector<edge> G[MAX_V];
vector<int> h(MAX_V); //??????????????£???
vector<int> dist(MAX_V);// ???????????¢
int prevv[MAX_V], preve[MAX_V];// ??´??????????????¨???
// from??????to??????????????????cap????????????cost????????????????????????????????????
void add_edge(int from, int to, int cap, int cost) {
G[from].push_back((edge){to, cap, cost, (int)G[to].size()});
G[to].push_back((edge){from, 0, -cost, (int)G[from].size() - 1});
}
//?????????????????????????????????????????´????????????
int shortest_path(int s,vector<int> &d){
int v = d.size();
rep(i,d.size())d[i]=INF;
d[s]=0;
rep(loop,v){
bool update = false;
rep(i,MAX_V){
for(auto e:G[i]){
if(d[i]!=INF && d[e.to] > d[i]+e.cost){
d[e.to] = d[i]+e.cost;
update = true;
}
}
}
if(!update)break;
if(loop==v-1)return true; //negative_cycle
}
return false;
}
// s??????t????????????f???????°??????¨???????±???????
// ??????????????´??????-1?????????
int min_cost_flow(int s, int t, int f) {
int res = 0;
rep(i,h.size())h[i]=0;
shortest_path(s, dist);
while (f > 0) {
// ?????????????????????????????¨??????h?????´??°??????
priority_queue<pii, vector<pii>, greater<pii> > que;
rep(i,dist.size())dist[i]=INF;
dist[s] = 0;
que.push(pii(0, s));
while (!que.empty()) {
pii p = que.top(); que.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (int i = 0; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v;
preve[e.to] = i;
que.push(pii(dist[e.to], e.to));
}
}
}
if (dist[t] == INF) return -1; //????????\???????????????
for (int v = 0; v < V; v++) h[v] += dist[v];
// s-t????????????????????£??????????????????
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]]; e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main(){
int e,f;
cin>>V>>e>>f;
rep(i,e){
int u,v,c,d;
cin>>u>>v>>c>>d;
add_edge(u, v, c, d);
}
cout<<min_cost_flow(0, V-1, f)<<endl;
}
|
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=105;
const int maxq=1005;
struct edge{int to,cap,cost,rev;};
int n,m,k;
int d[maxn];
int a[maxn];
int p1[maxn];
int p2[maxn];
bool inq[maxn];
vector<edge>G[maxn];
void add_edge(int u,int v,int c,int w) {
G[u].pb(edge{v,c,w,int(G[v].size())});
G[v].pb(edge{u,0,-w,int(G[u].size()-1)});
}
int mcmf(int s,int t) {
int flow=0 , cost=0;
while (1) {
memset(d,0x3f,sizeof(d));
d[s]=0; a[s]=max(0,k-flow);
int qh=0,qt=0,q[maxq];
q[qt++]=s; inq[s]=1;
while (qh<qt) {
int u=q[qh++];
inq[u]=0;
for (int i=0;i<G[u].size();i++) {
edge e=G[u][i];
if (d[e.to]>d[u]+e.cost && e.cap) {
d[e.to]=d[u]+e.cost;
a[e.to]=min(a[u],e.cap);
p1[e.to]=u;
p2[e.to]=i;
if (!inq[e.to]) {
q[qt++]=e.to;
inq[e.to]=1;
}
}
}
}
if (d[t]==INF || !a[t]) break;
flow+=a[t];
cost+=a[t]*d[t];
for (int v=t,u=p1[t];v!=s;v=u,u=p1[u]) {
int id=p2[v];
G[u][id].cap-=a[t];
id=G[u][id].rev;
G[v][id].cap+=a[t];
}
}
if (flow<k) return -1;
else return cost;
}
int main() {
cin>>n>>m>>k;
for (int i=0;i<m;i++) {
int u,v,c,w; cin>>u>>v>>c>>w;
add_edge(u,v,c,w);
}
cout<<mcmf(0,n-1)<<'\n';
}
|
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <bitset>
#include <complex>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
typedef unsigned long long ull;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
const int INF = 1 << 27;
const int MAX_V = 101;
//////////////////////////////
// ????°??????¨???
struct edge { int to, cap, cost, rev; };
int V;
vector<edge> G[MAX_V]; //??°???????????£??\???????????¨???
int h[MAX_V]; //??????????????£???
int dist[MAX_V]; //???????????¢
int prevv[MAX_V], preve[MAX_V]; // ??´??????????????¨???
void add_edge(int from, int to, int cap, int cost){
G[from].push_back((edge){to, cap, cost, (int)G[to].size()});
G[to].push_back((edge){from, 0, -cost, (int)(G[from].size()) - 1});
}
// s??????t????????????f???????°??????¨???
// ??????????????´??????-1?????????
int min_cost_flow(int s, int t, int f){
int res = 0;
fill(h, h + V, 0);
while(f > 0){
priority_queue<pi, vector<pi>, greater<pi> > que;
fill(dist, dist + V, INF);
dist[s] = 0;
que.push(pi(0, s));
while(!que.empty()){
pi p = que.top(); que.pop();
int v = p.second;
if(dist[v] < p.first) continue;
REP(i, G[v].size()){
edge &e = G[v][i];
if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(pi(dist[e.to], e.to));
}
}
}
if(dist[t] == INF) return -1;
REP(v, V) h[v] += dist[v];
int d = f;
for(int v = t; v != s; v = prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap);
f -= d;
res += d * h[t];
for(int v = t; v != s; v = prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main() {
int E, F; cin >>V >>E >>F;
REP(i, E){
int a,b,c,d; cin >> a >> b >> c >> d;
add_edge(a,b,c,d);
}
cout << min_cost_flow(0, V-1, F) <<endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll inf = 1e14;
class min_cost_flow{
private:
int N;
struct edge{int to; ll cap,cost; int rev; bool is_rev;};
vector<vector<edge>> G;
vector<ll> h,dist,prevv,preve;
public:
min_cost_flow(int n){
N = n;
G = vector<vector<edge>>(N);
h = dist = prevv = preve = vector<ll>(N,0);
}
void add_edge(int from,int to,ll cap,ll cost){
G[from].push_back((edge){to,cap,cost,(int) G[to].size(),false});
G[to].push_back((edge){from,0,-cost,(int) G[from].size()-1,true});
}
ll answer(int s,int t,ll f){
ll res = 0;
fill(h.begin(),h.end(),0);
while(f>0){
priority_queue<P,vector<P>,greater<P>> Q;
fill(dist.begin(),dist.end(),inf);
dist[s] = 0;
Q.push(P(0,s));
while(!Q.empty()){
P p = Q.top(); Q.pop();
int v = p.second;
if(dist[v]<p.first) continue;
for(int i=0;i<G[v].size();i++){
edge &e = G[v][i];
if(e.cap>0 && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
dist[e.to] = dist[v]+e.cost+h[v]-h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
Q.push(P(dist[e.to],e.to));
}
}
}
if(dist[t]==inf) return -1;
for(int v=0;v<N;v++) h[v] += dist[v];
ll d = f;
for(int v=t;v!=s; v=prevv[v]){
d = min(d,G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d*h[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
};
int N,M,F;
int main(){
cin >> N >> M >> F;
min_cost_flow flow(N+1);
int u,v,c,d;
for(int i=0;i<M;i++){
cin >> u >> v >> c >> d;
flow.add_edge(u,v,c,d);
}
cout << flow.answer(0,N-1,F) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) for(auto&& x : v){cout << x << " ";} cout << endl
#define printVV(vv) for(auto&& v : vv){for(auto&& x : v){cout << x << " ";}cout << endl;}
#define printP(p) cout << p.first << " " << p.second << endl
#define printVP(vp) for(auto&& p : vp) printP(p);
typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<Pii> vp;
const int inf = 1e9;
const int mod = 1e9 + 7;
template<typename T> using Graph = vector<vector<T>>;
struct edge { int to, cap, cost, rev; };
int V;
const int MAX_V = 100;
vector<edge> G[MAX_V];
int dist[MAX_V];
int prevv[MAX_V], preve[MAX_V];
void add_edge(int from, int to, int cap, int cost) {
G[from].emplace_back((edge){to, cap, cost, (int)G[to].size()});
G[to].emplace_back((edge){from, 0, -cost, (int)G[from].size() - 1});
}
int min_cost_flow(int s, int t, int f) {
int res = 0;
while (f > 0) {
fill(dist, dist + V, inf);
dist[s] = 0;
bool update = true;
while (update) {
update = false;
for (int v = 0; v < V; v++) {
if (dist[v] == inf) continue;
for (int i = 0; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if (dist[t] == inf) {
return -1;
}
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * dist[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int E, F;
cin >> V >> E >> F;
rep(i, E) {
int u, v, c, d;
cin >> u >> v >> c >> d;
add_edge(u, v, c, d);
}
cout << min_cost_flow(0, V - 1, F) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define fth(t) std::get<3>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))
using ll = long long;
using P = std::tuple<int,int>;
const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
int V, E, F;
// to, rev, cap, cost
std::vector<std::tuple<int, int, int, int>> G[200];
void addEdge(int u, int v, int cap, int cost){
G[u].emplace_back(v, G[v].size(), cap, cost);
G[v].emplace_back(u, G[u].size() - 1, 0, -cost);
}
int dist[1500];
tuple<int, int, int> prevs[1500];
const int INF = numeric_limits<int>::max() / 2;
void bellmanFord(int s){
std::fill(dist, dist+V, INF);
std::fill(prevs, prevs+V, std::make_tuple(-1, -1, -1));
dist[s] = 0;
for(int i=0;i<V;++i){
bool updated = false;
for(int u=0;u<V;++u){
for(int k=0;k<G[u].size();++k){
const auto& p = G[u][k];
int v, rev, cap, c;
std::tie(v, rev, cap, c) = p;
if(cap <= 0){continue;}
if(dist[v] > dist[u] + c){
updated = true;
dist[v] = dist[u] + c;
prevs[v] = std::make_tuple(u, k, rev);
}
}
}
if(!updated){break;}
}
}
int f(){
ll res = 0;
while(F > 0){
bellmanFord(0);
if(dist[V-1] == INF){
return -1;
}
int v = V - 1;
int d = F;
while(v != 0){
int pv, e_idx;
tie(pv, e_idx, ignore) = prevs[v];
d = min(d, thd(G[pv][e_idx]));
v = pv;
}
F -= d;
res += 1ll * d * dist[V-1];
v = V - 1;
while(v != 0){
int pv, e_idx, rev;
tie(pv, e_idx, rev) = prevs[v];
thd(G[pv][e_idx]) -= d;
thd(G[v][rev]) += d;
v = pv;
}
}
return res;
}
int main(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cin >> V >> E >> F;
for(int i=0;i<E;++i){
int u, v, c, d;
std::cin >> u >> v >> c >> d;
addEdge(u, v, c, d);
}
ll res = f();
std::cout << res << std::endl;
}
|
#include <vector>
#include <queue>
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
typedef long long int ll;
// Primal-Dual Algorithm
// ref: https://tjkendev.github.io/procon-library/python/min_cost_flow/primal-dual.html
class MinCostFlow {
public:
const ll INF = 1e18;
const ll MAX_COST = 1e9+9;
struct edge {
int to;
ll cap;
ll cost;
ll res;
int r_edge;
};
int N;
vector<vector<edge>> G;
MinCostFlow(int n): N(n), G(n) {}
void add_edge(int from, int to, int cap, int cost){
G[from].push_back({to, cap, cost, cap, (int)G[to].size()});
G[to].push_back({from, 0, -cost, 0, (int)G[from].size()-1});
}
ll flow(int S, int T, int flow){
ll res = 0;
vector<ll> H(N); // ポテンシャル
vector<int> pre_v(N), pre_e(N);
while(flow > 0){
vector<ll> D(N, INF);
priority_queue<pair<ll,int>> que;
D[S] = 0;
que.emplace(0, S);
while(!que.empty()){
ll d = que.top().first;
ll i = que.top().second;
que.pop();
if(D[i] < d) continue;
rep(v, G[i].size()) if(G[i][v].res > 0) {
edge &e = G[i][v];
if (D[e.to] > D[i] + e.cost + H[i] - H[e.to]) {
D[e.to] = D[i] + e.cost + H[i] - H[e.to];
pre_v[e.to] = i;
pre_e[e.to] = v;
que.emplace(D[e.to], e.to);
}
}
}
if( D[T] == INF ) return -1;
rep(i,N) H[i] += D[i];
ll f = flow;
for(int i=T; i != S; i = pre_v[i]) f = min(f, G[pre_v[i]][pre_e[i]].res);
flow -= f;
res += f * H[T];
for(int i=T; i != S; i = pre_v[i]){
edge &e = G[pre_v[i]][pre_e[i]];
e.res -= f;
G[i][e.r_edge].res += f;
}
}
return res;
}
void reset(){
for(auto &g: G) for(auto &e: g) e.res = e.cap;
}
};
// verify: AOJ GRL_6_B
#include <iostream>
int main(){
int V, E, F;
cin >> V >> E >> F;
MinCostFlow fl(V);
rep(i,E){
int u, v, c, d;
cin >> u >> v >> c >> d;
fl.add_edge(u, v, c, d);
}
cout << fl.flow(0, V-1, F) << endl;
}
|
#define DEBUG 1
#include <iostream>
#include <vector>
#include <tuple>
#include <queue>
using namespace std;
typedef long long ll;
// ------------------------------------------------------------
typedef tuple<ll, int> Info;
struct Edge
{
int to;
ll cap, cost;
int rev;
};
class MinCostFlow
{
int N;
vector<Edge> *G;
ll *h, *dist;
int *prev_v, *prev_e;
ll INFTY = 10000000000000010;
public:
MinCostFlow() {}
MinCostFlow(int n) : N(n)
{
G = new vector<Edge>[n];
h = new ll[n];
dist = new ll[n];
prev_v = new int[n];
prev_e = new int[n];
}
void add_edge(int from, int to, ll cap, ll cost)
{
G[from].push_back((Edge){to, cap, cost, (int)G[to].size()});
G[to].push_back((Edge){from, 0, -cost, (int)G[from].size() - 1});
}
ll min_cost_flow(int s, int t, ll f)
{
ll res = 0;
fill(h, h + N, 0);
while (f > 0)
{
priority_queue<Info, vector<Info>, greater<Info>> Q;
fill(dist, dist + N, INFTY);
dist[s] = 0;
Q.push(Info(0, s));
while (!Q.empty())
{
Info p = Q.top();
Q.pop();
int v = get<1>(p);
if (dist[v] < get<0>(p))
{
continue;
}
for (auto i = 0; i < (int)G[v].size(); i++)
{
Edge &e = G[v][i];
ll tmp = dist[v] + e.cost + h[v] - h[e.to];
if (e.cap > 0 && dist[e.to] > tmp)
{
dist[e.to] = tmp;
prev_v[e.to] = v;
prev_e[e.to] = i;
Q.push(Info(dist[e.to], e.to));
}
}
}
if (dist[t] == INFTY)
{
return -1;
}
for (auto v = 0; v < N; v++)
{
h[v] += dist[v];
}
ll d = f;
for (auto v = t; v != s; v = prev_v[v])
{
d = min(d, G[prev_v[v]][prev_e[v]].cap);
}
f -= d;
res += d * h[t];
for (auto v = t; v != s; v = prev_v[v])
{
Edge &e = G[prev_v[v]][prev_e[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
};
// ------------------------------------------------------------
int N, M;
ll F;
MinCostFlow flow;
int main()
{
cin >> N >> M >> F;
flow = MinCostFlow(N);
for (auto i = 0; i < M; i++)
{
int u, v;
ll c, d;
cin >> u >> v >> c >> d;
flow.add_edge(u, v, c, d);
}
cout << flow.min_cost_flow(0, N - 1, F) << endl;
}
|
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int,int> P;//shortest pass,vertex number
const int MAX=100;
const int INF=1<<25;
class edge
{
public:
int to,cap,cost,rev;
edge(){}
edge(int to,int cap,int cost,int rev):to(to),cap(cap),cost(cost),rev(rev){}
};
int V;
vector<edge> G[MAX];
int h[MAX];
int dist[MAX];
int prevv[MAX],preve[MAX];
void add_edge(int from,int to,int cap,int cost)
{
G[from].push_back(edge(to,cap,cost,G[to].size()));
G[to].push_back(edge(from,0,-cost,G[from].size()-1));
return;
}
int mincostflow(int s,int t,int f)
{
int res=0;
memset(h,0,sizeof(h));
while(f>0)
{
priority_queue<P> Q;
fill(dist,dist+V,INF);
dist[s]=0;
Q.push(P(0,s));
while(!Q.empty())
{
P p=Q.top();Q.pop();
int v=p.second;
if(dist[v]<p.first*(-1))continue;
for(int i=0;i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to])
{
dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
prevv[e.to]=v;
preve[e.to]=i;
Q.push(P(dist[e.to]*(-1),e.to));
}
}
}
if(dist[t]==INF)return -1;
for(int v=0;v<V;v++)h[v]+=dist[v];
int d=f;
for(int v=t;v!=s;v=prevv[v])d=min(d,G[prevv[v]][preve[v]].cap);
f-=d;
res+=d*h[t];
for(int v=t;v!=s;v=prevv[v])
{
edge &e=G[prevv[v]][preve[v]];
e.cap-=d;
G[v][e.rev].cap+=d;
}
}
return res;
}
int main()
{
int E,F;cin>>V>>E>>F;
int u,v,c,d;
for(int i=0;i<E;i++)
{
cin>>u>>v>>c>>d;
add_edge(u,v,c,d);
}
cout<<mincostflow(0,V-1,F)<<endl;
return 0;
}
|
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<ctime>
using namespace std;
typedef long long ll;
bool debug = false;
const int NIL = -1;
const int INF = 1000000000;
const int NUM = 100010;
clock_t START, END;
int V, E, F;
struct Edge {
int from, to, cap, flow, cost;
Edge(int u, int v, int c, int f, int w) :from(u), to(v), cap(c), flow(f), cost(w) {}
};
struct Graph {
int n, m;
vector<Edge> edges;
vector<int> G[NUM];
int inq[NUM];
int d[NUM];
int a[NUM];
int p[NUM];
void init(int n) {
this->n = n;
for (int i = 0; i < n; i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap, int cost) {
edges.push_back(Edge(from, to, cap, 0, cost));
edges.push_back(Edge(to, from, 0, 0, -cost));
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BellmanFord(int s, int t, int& flow, ll& cost) {
for (int i = 0; i < n; i++)
d[i] = INF;
memset(inq, 0, sizeof(inq));
d[s] = 0;
inq[s] = 1;
p[s] = 0;
a[s] = INF;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
inq[u] = 0;
for (int i = 0; i < G[u].size(); i++) {
Edge& e = edges[G[u][i]];
if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if (!inq[e.to]) {
Q.push(e.to);
inq[e.to] = 1;
}
}
}
}
if (d[t] == INF)
return false;
flow += a[t];
if (a[t] <= F) {
cost += (ll)d[t] * (ll)a[t];
F -= a[t];
}
else {
cost += (ll)d[t] * (ll)(F);
F = 0;
}
for (int u = t; u != s; u = edges[p[u]].from) {
edges[p[u]].flow += a[t];
edges[p[u] ^ 1].flow -= a[t];
}
return true;
}
int MincostMaxflow(int s, int t, ll& cost) {
int flow = 0;
cost = 0;
while (BellmanFord(s, t, flow, cost));
return flow;
}
};
Graph solve;
int main(void)
{
if (debug) {
START = clock();
freopen("in29.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
ll COST;
int u, v, c, d, f;
cin >> V >> E >> F;
f = F;
solve.init(V);
for (int i = 0; i < E; i++) {
scanf("%d%d%d%d", &u, &v, &c, &d);
solve.AddEdge(u, v, c, d);
}
if (solve.MincostMaxflow(0, V - 1, COST) >= f)
cout << COST << endl;
else
cout << "-1" << endl;
if (debug) {
END = clock();
double endtime = (double)(END - START) / 1000;
printf("total time = %lf s", endtime);
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <limits>
#include <vector>
using namespace std;
typedef int T; // cost type
typedef int U; // flow type
T INF = numeric_limits<T>::max(); // <limits>
struct edge {
int to, rev;
T cost;
U cap;
edge(int to, U cap, int rev, T cost) : to(to), rev(rev), cost(cost), cap(cap) {}
};
struct primal_dual {
int N;
vector<vector<edge> > graph;
vector<int> prev_v, prev_e;
vector<T> min_cost;
primal_dual() {}
primal_dual(int _N) { init(_N); }
void init(int _N) {
N = _N;
graph.resize(N);
prev_v.resize(N);
prev_e.resize(N);
min_cost.resize(N);
}
void add_edge(int u, int v, U cap, T cost) {
graph[u].push_back(edge(v, cap, graph[v].size(), cost));
graph[v].push_back(edge(u, 0, graph[u].size()-1, -cost));
}
T min_cost_flow(int s, int t, U F) {
T val = 0;
while (F > 0) {
fill(min_cost.begin(), min_cost.end(), INF);
min_cost[s] = 0;
bool updated = true;
while (updated) {
updated = false;
for (int v = 0; v < N; ++v) {
if (min_cost[v] == INF) continue;
for (int j = 0; j < graph[v].size(); ++j) {
edge& e = graph[v][j];
T cost = min_cost[v] + e.cost;
if (cost < min_cost[e.to] && e.cap > 0) {
updated = true;
min_cost[e.to] = cost;
prev_v[e.to] = v;
prev_e[e.to] = j;
}
}
}
}
if (min_cost[t] == INF) {
return (T)-1; // fail
}
U f = F;
for (int v = t; v != s; v = prev_v[v]) {
f = min(f, graph[prev_v[v]][prev_e[v]].cap);
}
F -= f;
val += (T)f * min_cost[t];
for (int v = t; v != s; v = prev_v[v]) {
edge& e = graph[prev_v[v]][prev_e[v]];
e.cap -= f;
graph[v][e.rev].cap += f;
}
}
return val;
}
};
int V, E, F;
primal_dual pd;
int main() {
cin >> V >> E >> F;
pd.init(V);
for (int j = 0; j < E; ++j) {
int u, v, c, d;
cin >> u >> v >> c >> d;
pd.add_edge(u, v, c, d);
}
cout << pd.min_cost_flow(0, V-1, F) << endl;
return 0;
}
|
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int MAX=100;
const int INF=1<<25;
class edge
{
public:
int to,cap,cost,rev;
edge(){}
edge(int to,int cap,int cost,int rev):to(to),cap(cap),cost(cost),rev(rev){}
};
int V;
vector<edge> G[MAX];
int dist[MAX];
int prevv[MAX],preve[MAX];
void add_edge(int from,int to,int cap,int cost)
{
G[from].push_back(edge(to,cap,cost,G[to].size()));
G[to].push_back(edge(from,0,-cost,G[from].size()-1));
return;
}
int mincostflow(int s,int t,int f)
{
int res=0;
while(f>0)
{
fill(dist,dist+V,INF);
dist[s]=0;
bool update=true;
while(update)
{
update=false;
for(int v=0;v<V;v++)
{
if(dist[v]==INF)continue;
for(int i=0;i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>0&&dist[e.to]>dist[v]+e.cost)
{
dist[e.to]=dist[v]+e.cost;
prevv[e.to]=v;
preve[e.to]=i;
update=true;
}
}
}
}
if(dist[t]==INF)return -1;
int d=f;
for(int v=t;v!=s;v=prevv[v])d=min(d,G[prevv[v]][preve[v]].cap);
f-=d;
res+=d*dist[t];
for(int v=t;v!=s;v=prevv[v])
{
edge &e=G[prevv[v]][preve[v]];
e.cap-=d;
G[v][e.rev].cap+=d;
}
}
return res;
}
int main()
{
int E,F;cin>>V>>E>>F;
int u,v,c,d;
for(int i=0;i<E;i++)
{
cin>>u>>v>>c>>d;
add_edge(u,v,c,d);
}
cout<<mincostflow(0,V-1,F)<<endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PI;
typedef pair<LL, LL> PLL;
const LL MOD = 1000000007LL;
const int inf = 1e9;
const LL INF = 1e18;
const int MAX_V = 100;
struct edge {
int to, cap, cost, rev;
edge(int to, int cap, int cost, int rev) :to(to), cap(cap), cost(cost), rev(rev) {}
};
vector<edge> G[MAX_V];
int h[MAX_V];
int dist[MAX_V];
int prevv[MAX_V], preve[MAX_V];
int V;
void add_edge(int from, int to, int cap, int cost) {
G[from].emplace_back(to, cap, cost, G[to].size());
G[to].emplace_back(from, 0, -cost, G[from].size() - 1);
}
int min_cost_flow(int s, int t, int f) {
int res = 0;
fill(h, h + V, 0);
while (f > 0) {
priority_queue<PI, vector<PI>, greater<PI>> que;
fill(dist, dist + V, inf);
dist[s] = 0;
que.push(PI(0, s));
while (!que.empty()) {
PI p = que.top(); que.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(PI(dist[e.to], e.to));
}
}
}
if (dist[t] == inf) return -1;
for (int v = 0; v < V; v++) h[v] += dist[v];
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d*h[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main() {
cin >> V;
int E, F;
cin >> E >> F;
for (int i = 0; i < E; i++) {
int u, v, c, d;
cin >> u >> v >> c >> d;
add_edge(u, v, c, d);
}
cout << min_cost_flow(0, V - 1, F) << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define MAX_V 10000
#define INF 1000000001
typedef pair<int,int> P;
struct edge { int to,cap,cost,rev; };
int V;
vector<edge> G[MAX_V];
int h[MAX_V];
int dist[MAX_V];
int prevv[MAX_V],preve[MAX_V];
void init_edge(){
for(int i=0;i<V;i++)G[i].clear();
}
void add_edge(int from,int to,int cap,int cost){
G[from].push_back((edge){to,cap,cost,(int)G[to].size()});
G[to].push_back((edge){from,0,-cost,(int)G[from].size()-1});
}
int min_cost_flow(int s,int t,int f){
int res = 0;
fill(h,h+V,0);
while(f>0){
priority_queue< P, vector<P>, greater<P> > que;
fill( dist, dist+V , INF );
dist[s]=0;
que.push(P(0,s));
while(!que.empty()){
P p = que.top(); que.pop();
int v = p.second;
if(dist[v]<p.first)continue;
for(int i=0;i<(int)G[v].size();i++){
edge &e = G[v][i];
if(e.cap>0&&dist[e.to] > dist[v]+e.cost+h[v]-h[e.to]){
dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
prevv[e.to]=v;
preve[e.to]=i;
que.push(P(dist[e.to],e.to));
}
}
}
if(dist[t]==INF){
return -1;
}
for(int v=0;v<V;v++)h[v]+=dist[v];
int d=f;
for(int v=t;v!=s;v=prevv[v]){
d=min(d,G[prevv[v]][preve[v]].cap);
}
f-=d;
res+=d*h[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main(){
int E,F,a,b,c,d;
cin>>V>>E>>F;
while(E--){
cin>>a>>b>>c>>d;
add_edge(a,b,c,d);
}
cout<<min_cost_flow(0,V-1,F)<<endl;
return 0;
}
|
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int MAX_V = 200;
struct edge{
int to, cap, cost, rev;
};
int V;
vector<edge> G[MAX_V];
int dist[MAX_V]; // 頂点にflowを1流すときのコストの総和
int prevv[MAX_V], preve[MAX_V]; // prev_vertex, prev_edge
const int INF = 1<<30;
void add_edge(int from, int to, int cap, int cost){
G[from].push_back((edge){to, cap, cost, (int)G[to].size()});
G[to].push_back((edge){from, 0, -cost, (int)G[from].size()-1});
}
// flow from s to t, the amount of that is f
int min_cost_flow(int s, int t, int f){
int res = 0;
while(f > 0){
for(int i = 0; i < V; i++) dist[i] = INF;
dist[s] = 0;
bool update = true;
// bellman-ford
while(update){
update = false;
for(int v = 0; v < V; v++){
if(dist[v] == INF) continue;
for(int i = 0; i < G[v].size(); i++){
edge &e = G[v][i];
if(e.cap > 0 && dist[e.to] > dist[v] + e.cost){
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if(dist[t] == INF){
return -1;
}
int d = f;
// 辺を逆に辿る、流せるだけ流す
for(int v = t; v != s; v = prevv[v]){
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * dist[t];
for(int v = t; v != s; v = prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d; // 順方向に流したので-
G[v][e.rev].cap += d; // 逆方向に流したので+
}
}
return res;
}
int main(){
int e, f;
cin >> V >> e >> f;
for(int i = 0; i < e; i++){
int a, b, c, d;
cin >> a >> b >> c >> d;
add_edge(a, b, c, d);
}
cout << min_cost_flow(0, V-1, f) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)x.size())
using pii = pair<int, int>;
using vvi = vector<vector<int>>;
using vi = vector<int>;
constexpr int INF = 1001001001001001001LL;
struct MinCostFlow {
struct edge { int to, cap, cost, rev; };
int V;
vector<vector<edge>> G;
vi dist, prevv, preve;
MinCostFlow(int V) : V(V), G(V), dist(V), prevv(V), preve(V) {}
void add_edge(int from, int to, int cap, int cost) {
G[from].push_back((edge){ to, cap, cost, sz(G[to]) });
G[to ].push_back((edge){ from, 0, -cost, sz(G[from]) - 1 });
}
// 流せない場合は-1を返す
int get(int s, int t, int f) {
int res = 0;
while (f > 0) {
fill(all(dist), INF);
dist[s] = 0;
bool update = true;
while (update) {
update = false;
rep(v, 0, V) {
if (dist[v] == INF) continue;
rep(i, 0, sz(G[v])) {
auto &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if (dist[t] == INF) {
return -1;
}
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * dist[t];
for (int v = t; v != s; v = prevv[v]) {
auto &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
};
signed main() {
int V, E, F;
cin >> V >> E >> F;
MinCostFlow min_cost_flow(V);
rep(i, 0, E) {
int vs, vt, cap, cost;
cin >> vs >> vt >> cap >> cost;
min_cost_flow.add_edge(vs, vt, cap, cost);
}
cout << min_cost_flow.get(0, V - 1, F) << endl;
}
|
#include <bits/stdc++.h>
using Int = int64_t;
using UInt = uint64_t;
using C = std::complex<double>;
#define rep(i, n) for(Int i = 0; i < (Int)(n); ++i)
#define rep2(i, l, r) for(Int i = (Int)(l); i < (Int)(r); ++i)
#define guard(x) if( not (x) ) continue;
#ifndef LOCAL_
#define fprintf if( false ) fprintf
#endif
template<typename T>
using RQ = std::priority_queue<T, std::vector<T>, std::greater<T>>;
int main() {
Int v, e, f;
std::cin >> v >> e >> f;
std::vector<Int> ss(e+1), ts(e+1), cs(e+1), fs(e+1), bs(e+1);
rep2(i,1,e+1) {
Int a, b, c, d;
std::cin >> a >> b >> c >> d;
ss[i] = a, ts[i] = b, cs[i] = d, fs[i] = 0, bs[i] = c;
}
std::vector<std::vector<Int>> es(v);
rep2(i,1,e+1) {
Int s = ss[i], t = ts[i];
es[s].emplace_back(i);
es[t].emplace_back(-i);
}
Int res = 0;
Int source = 0, sink = v-1;
std::vector<Int> hs(v);
while( f > 0 ) {
RQ<std::pair<Int,Int>> q;
q.emplace(0, source);
std::vector<Int> ps(v, -1), xs(v, -1), ys(v);
xs[source] = 0;
ys[source] = f;
while( not q.empty() ) {
Int d, s; std::tie(d, s) = q.top(); q.pop();
guard( d == xs[s] );
for(Int i : es[s]) {
Int k = std::abs(i);
Int t = i > 0 ? ts[k] : ss[k];
Int tf = i > 0 ? bs[k] : fs[k];
guard( tf > 0 );
Int nd = d + (i>0?cs[k]:-cs[k]) + hs[s] - hs[t];
guard( xs[t] == -1 or xs[t] > nd );
xs[t] = nd;
ps[t] = i;
ys[t] = std::min(ys[s], tf);
q.emplace(nd, t);
}
}
Int tf = ys[sink];
f -= tf;
if( f > 0 and tf == 0 ) {
res = -1;
break;
}
rep(i, v) hs[i] += xs[i];
for(Int i=sink, k=ps[i]; i!=source; i=(k>0?ss[k]:ts[-k]), k=ps[i]) {
Int ak = std::abs(k);
if( k > 0 ) {
fs[ak] += tf;
bs[ak] -= tf;
}
else {
fs[ak] -= tf;
bs[ak] += tf;
}
}
res += tf * hs[sink];
if( f == 0 ) break;
}
printf("%ld\n", res);
}
|
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)
#define all(x) x.begin(),x.end()
struct edge {
i64 from;
i64 to;
i64 cap;
i64 cost;
i64 rev;
};
i64 INF = 1e18;
i64 capacity_scaling_bflow(std::vector<std::vector<edge>>& g, std::vector<i64> b) {
i64 ans = 0;
i64 U = *std::max_element(begin(b), end(b));
i64 delta = (1 << ((int)(std::log2(U)) + 1));
int n = g.size();
std::vector<i64> e = b;
std::vector<i64> p(n, 0);
int zero = 0;
for(auto x : e) {
if(x == 0) zero++;
}
for(;delta > 0; delta >>= 1) {
if(zero == n) break;
for(int s = 0;s < n;s++) {
if(!(e[s] >= delta)) continue;
std::vector<std::size_t> pv(n, -1);
std::vector<std::size_t> pe(n, -1);
std::vector<i64> dist(n, INF);
using P = std::pair<i64,i64>;
std::priority_queue<P,std::vector<P>,std::greater<P>> que;
dist[s] = 0;
que.push({dist[s], s});
while(!que.empty()) {
int v = que.top().second;
i64 d = que.top().first;
que.pop();
if(dist[v] < d) continue;
for(std::size_t i = 0;i < g[v].size();i++) {
const auto& e = g[v][i];
std::size_t u = e.to;
if(e.cap == 0) continue;
if(dist[u] > dist[v] + e.cost + p[v] - p[u]) {
dist[u] = dist[v] + e.cost + p[v] - p[u];
pv[u] = v;
pe[u] = i;
que.push({dist[u], u});
}
}
}
for(int i = 0;i < n;i++) {
if(pv[i] != -1) p[i] += dist[i];
}
int t = 0;
for(;t < n;t++) {
if(!(e[s] >= delta)) break;
if(e[t] <= -delta && pv[t] != -1) {
std::size_t u = t;
for(;pv[u] != -1;u = pv[u]) {
ans += delta * g[pv[u]][pe[u]].cost;
g[pv[u]][pe[u]].cap -= delta;
g[u][g[pv[u]][pe[u]].rev].cap += delta;
}
e[u] -= delta;
e[t] += delta;
if(e[u] == 0) zero++;
if(e[t] == 0) zero++;
}
}
}
}
if(zero == n) return ans;
else return -1e18;
}
int main() {
i64 N,M,F;
cin >> N >> M >> F;
vector<vector<edge>> g(N + M);
vector<i64> e(N + M, 0);
int s = 0;
int t = N - 1;
e[s + M] = F;
e[t + M] = -F;
for(int i = 0;i < M;i++) {
i64 a,b,c,d;
cin >> a >> b >> c >> d;
e[i] = c;
e[a + M] -= c;
g[i].push_back({i, a + M, (i64)1e18, 0, (i64)g[a + M].size()});
g[a + M].push_back({a + M, i, 0, 0, (i64)g[i].size() - 1});
g[i].push_back({i, b + M, (i64)1e18, d, (i64)g[b + M].size()});
g[b + M].push_back({b + M, i, 0, -d, (i64)g[i].size() - 1});
}
i64 ans = capacity_scaling_bflow(g, e);
if(ans == -1e18) {
cout << -1 << endl;
}
else {
cout << ans << endl;
}
}
|
//#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a<=x&&x<b)
#define MP make_pair
#define PB push_back
const int INF = (1LL<<30);
const ll LLINF = (1LL<<60);
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;
//#define int ll
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
#define MAX_V 10000
struct edge { int to, cap, cost, rev; };
int V;
vector<edge> G[MAX_V];
int h[MAX_V];
int dist[MAX_V];
int prevv[MAX_V], preve[MAX_V];
void add_edge(int from, int to, int cap, int cost) {
G[from].PB({to, cap, cost, (int)G[to].size()});
G[to].PB({from, 0, -cost, (int)G[from].size()-1});
}
// s??????t????????????f???????°??????¨???
int min_cost_flow(int s, int t, int f) {
int res = 0;
fill(h, h+V, 0);
while(f > 0) {
priority_queue<PII, vector<PII>, greater<PII>> que;
fill(dist, dist+V, INF);
dist[s] = 0;
que.push({0, s});
while(que.size()) {
PII p = que.top(); que.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(int i=0; i<(int)G[v].size(); ++i) {
edge &e = G[v][i];
if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push({dist[e.to], e.to});
}
}
}
// ????????\???????????????
if(dist[t] == INF) return -1;
REP(v, V) h[v] += dist[v];
int d = f;
for(int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for(int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
signed main(void)
{
int e, f;
cin >> V >> e >> f;
REP(i, e) {
int u, v, c, d;
cin >> u >> v >> c >> d;
add_edge(u, v, c, d);
}
cout << min_cost_flow(0, V-1, f) << endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
typedef unordered_map<int,unordered_map<int,int>> double_map;
int dfs(
int now, int end, int flow, int *cost,
vector<bool> &arrived, vector<int> &dp, double_map &g, double_map &costs
) {
if(now==end||flow==0) return flow;
arrived[now] = true;
for(pair<int,int> p : costs[now]) {
int next = p.first;
int next_cost = p.second;
if(arrived[next]) continue;
if(dp[next]-dp[now]==next_cost) {
int res = dfs(next,end,min(flow,g[now][next]),cost,arrived,dp,g,costs);
if(res>0) {
g[now][next] -= res;
g[next][now] += res;
costs[next][now] = -costs[now][next];
if(g[now][next]==0) {
costs[now].erase(next);
}
*cost += res*next_cost;
return res;
}
}
}
return 0;
}
void calc(int v, double_map &costs, vector<int> &dp, int s) {
dp[s] = 0;
vector<int> vf(v,1<<30);
for(int loop = 0; loop < v; loop++) {
for(int now = 0; now < v; now++) {
if(dp[now]==1<<30) continue;
vf[now] = min(vf[now],dp[now]);
for(pair<int,int> p : costs[now]) {
int next = p.first;
int next_cost = p.second;
vf[next] = min(vf[next],dp[now]+next_cost);
}
}
dp.swap(vf);
}
}
int min_cost_flow(int v, double_map &g, double_map &costs, int f, int s, int e) {
int cost = 0;
int ans = 0;
while(ans<f) {
vector<int> dp(v,1<<30);
calc(v,costs,dp,s);
vector<bool> arrived(v);
int flow = dfs(s,e,f-ans,&cost,arrived,dp,g,costs);
if(flow<=0) {
return -1;
}
else ans += flow;
}
return cost;
}
int main() {
int v,e,f;
double_map g,costs;
cin >> v >> e >> f;
for(int i = 0; i < e; i++) {
int u,v2,c,d;
cin >> u >> v2 >> c >> d;
if(c<=0) continue;
g[u][v2] = c;
costs[u][v2] = d;
}
int cost = min_cost_flow(v,g,costs,f,0,v-1);
cout << cost << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> struct PrimalDual {
struct Edge {
int to, rev;
T cap, cost;
};
const int n;
const T inf = numeric_limits<T>::max();
VV<Edge> g;
V<T> pot, dist;
V<> pv, pe;
PrimalDual(int n) : n(n), g(n), pot(n), dist(n), pv(n), pe(n) {}
void add_edge(int from, int to, T cap, T cost) {
assert(from != to);
assert(cap >= 0);
assert(cost >= 0);
g[from].emplace_back(Edge{to, (int) g[to].size(), cap, cost});
g[to].emplace_back(Edge{from, (int) g[from].size() - 1, 0, -cost});
}
void dijkstra(int s) {
using P = pair<T, int>;
priority_queue< P, V<P>, greater<P> > pque;
fill(begin(dist), end(dist), inf);
pque.emplace(dist[s] = 0, s);
while (!pque.empty()) {
T d; int v;
tie(d, v) = pque.top(); pque.pop();
if (d > dist[v]) continue;
for (int i = 0; i < g[v].size(); ++i) {
const Edge& e = g[v][i];
if (e.cap <= 0 or dist[e.to] <= dist[v] + e.cost - (pot[e.to] - pot[v])) continue;
pv[e.to] = v;
pe[e.to] = i;
pque.emplace(dist[e.to] = dist[v] + e.cost - (pot[e.to] - pot[v]), e.to);
}
}
}
T min_cost_flow(int s, int t, T f) {
assert(s != t);
assert(f >= 0);
T res = 0;
fill(begin(pot), end(pot), 0);
while (f > 0) {
dijkstra(s);
if (dist[t] == inf) return -1;
for (int v = 0; v < n; ++v) pot[v] += dist[v];
T d = f;
for (int v = t; v != s; v = pv[v]) {
d = min(d, g[pv[v]][pe[v]].cap);
}
f -= d;
res += d * pot[t];
for (int v = t; v != s; v = pv[v]) {
Edge& e = g[pv[v]][pe[v]];
e.cap -= d;
g[v][e.rev].cap += d;
}
}
return res;
}
};
int main() {
cin.tie(nullptr); ios::sync_with_stdio(false);
int n, m, f; cin >> n >> m >> f;
PrimalDual<int> pd(n);
for (int i = 0; i < m; ++i) {
int from, to, cap, cost; cin >> from >> to >> cap >> cost;
pd.add_edge(from, to, cap, cost);
}
cout << pd.min_cost_flow(0, n - 1, f) << '\n';
}
|
#include<bits/stdc++.h>
using namespace std;
using UL=unsigned int;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(UL i=0; i<(n); i++)
template<class T>
using nega_queue = priority_queue<T,vector<T>,greater<T>>;
struct Edge{ UL u,v; UL c; int d; UL r; };
UL N,M;
Edge J[2000];
vector<UL> E[100];
UL F;
int fAns = 0;
UL fD[100];
UL fPE[100];
UL fP[100]={};
void fDijkstra(){
rep(i,N) fD[i]=fPE[i]=~0u;
nega_queue<pair<UL,pair<UL,UL>>> Q; Q.push({0,{0,~0u}});
while(Q.size()){
UL d=Q.top().first;
UL p=Q.top().second.first;
UL pre=Q.top().second.second;
Q.pop();
if(fD[p]!=~0u) continue;
fD[p]=d;
fPE[p]=pre;
for(UL j:E[p]){
if(J[j].c==0)continue;
Q.push({d+J[j].d+fP[p]-fP[J[j].v],{J[j].v,j}});
}
}
rep(i,N) fP[i]+=fD[i];
}
void flow(){
fDijkstra();
if(fD[N-1]==~0u){ F=0; fAns=-1; return; }
UL p=N-1;
UL c=F;
while(p!=0){
c=min(c,J[fPE[p]].c);
p=J[fPE[p]].u;
}
p=N-1;
while(p!=0){
J[fPE[p]].c-=c;
J[J[fPE[p]].r].c+=c;
fAns += (int)c * J[fPE[p]].d;
p=J[fPE[p]].u;
}
F-=c;
}
int main(){
scanf("%u%u%u",&N,&M,&F);
rep(i,M){
UL u,v,c; int d; scanf("%u%u%u%d",&u,&v,&c,&d);
E[u].push_back(i);
E[v].push_back(M+i);
J[i]=Edge{u,v,c,d,M+i};
J[M+i]=Edge{v,u,0,-d,i};
}
while(F) flow();
printf("%d\n",fAns);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<P> vp;
typedef vector<ll> vll;
const int INF = 99999999;
const int MAX_V = 100000;
struct edge { int to, cap, cost, rev; };
int V, E, F;
vector<edge> G[MAX_V];
int h[MAX_V]; // potential
int dist[MAX_V];
int prevv[MAX_V], preve[MAX_V]; // privious vertex, edge
void add_edge(int from, int to, int cap, int cost) {
G[from].emplace_back((edge){to, cap, cost, (int)G[to].size()});
G[to].emplace_back((edge){from, 0, -cost, (int)G[from].size() - 1});
}
// get min cost flow from s to t
// if we cannot flow f, then return -1
int min_cost_flow(int s, int t, int f) {
int res = 0;
fill(h, h + V, 0);
while (f > 0) {
// update h by dijkstra
priority_queue<P, vector<P>, greater<P>> que;
fill(dist, dist + V, INF);
dist[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (dist[v] < p.first) continue;
rep(i, G[v].size()) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P(dist[e.to], e.to));
}
}
}
if (dist[t] == INF) {
// no more flow
return -1;
}
rep(v, V) h[v] += dist[v];
// flow as much as possible along the minimum path from s to t
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, (int)G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
signed main() {
cin >> V >> E >> F;
rep(i, E) {
int u, v, c, d;
cin >> u >> v >> c >> d;
add_edge(u, v, c, d);
}
cout << min_cost_flow(0, V - 1, F) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=103,INF=1<<30;
typedef pair<int,int> P;
struct edge{int to,cap,cost,rev;};
int V;
vector<edge> G[MAX];
int h[MAX];
int dist[MAX];
int prevv[MAX],preve[MAX];
void add_edge(int from,int to,int cap,int cost){
G[from].push_back((edge){to,cap,cost,int(G[to].size())});
G[to].push_back((edge){from,0,-cost,int(G[from].size()-1)});
}
int min_cost_flow(int s,int t,int f){
int res=0;
fill(h,h+V,0);
while(f>0){
priority_queue<P,vector<P>,greater<P>> que;
fill(dist,dist+V,INF);
dist[s]=0;
que.push(P(0,s));
while(!que.empty()){
P p=que.top();que.pop();
int v=p.second;
if(dist[v]<p.first) continue;
for(int i=0;i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
prevv[e.to]=v;
preve[e.to]=i;
que.push(P(dist[e.to],e.to));
}
}
}
if(dist[t]==INF){
return -1;
}
for(int v=0;v<V;v++){
h[v]+=dist[v];
}
int d=f;
for(int v=t;v!=s;v=prevv[v]){
d=min(d,G[prevv[v]][preve[v]].cap);
}
f-=d;
res+=d*h[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e=G[prevv[v]][preve[v]];
e.cap-=d;
G[v][e.rev].cap+=d;
}
}
return res;
}//sからtへの流量fの最小費用流、流せない場合は-1
int main(){
int E,F;cin>>V>>E>>F;
for(int i=0;i<E;i++){
int a,b,c,d;cin>>a>>b>>c>>d;
add_edge(a,b,c,d);
}
int ans=min_cost_flow(0,V-1,F);
cout<<ans<<endl;
}
|
#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;
typedef pair<int, int> pii;
class MinimumCostFlow {
struct edge {
int to, cap, cost, rev;
edge(int to_, int cap_, int cost_, int rev_)
: to(to_), cap(cap_), cost(cost_), rev(rev_) {}
};
int V;
vector<vector<edge>> G;
vector<int> h, dist, prevv, preve;
public:
MinimumCostFlow(int _V) : V(_V), G(_V), h(_V), dist(_V), prevv(_V), preve(_V) {}
void add(int from, int to, int cap, int cost) {
G[from].push_back(edge(to, cap, cost, G[to].size()));
G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
}
int calc(int s, int t, int f) {
int res = 0;
fill(h.begin(), h.end(), 0);
while (f > 0) {
priority_queue<pii, vector<pii>, greater<pii>> que;
fill(dist.begin(), dist.end(), INF);
dist[s] = 0;
que.push(pii(0, s));
while (!que.empty()) {
pii p = que.top(); que.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (size_t i = 0; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(pii(dist[e.to], e.to));
}
}
}
if (dist[t] == INF) return -1;
for (int v = 0; v < V; v++)
h[v] += dist[v];
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
};
int main()
{
int V, E, F;
cin >> V >> E >> F;
MinimumCostFlow mcf(V);
for (int i = 0, u, v, c, d; i < E; i++) {
cin >> u >> v >> c >> d;
mcf.add(u, v, c, d);
}
cout << mcf.calc(0, V - 1, F) << endl;
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#include <limits>
using namespace std;
//BEGIN
template <typename T, typename E>
struct PrimalDual {
struct edge {
int to, rev;
T cap;
E cost;
edge() {}
edge(int to, T cap, E cost, int rev) :to(to), cap(cap), cost(cost), rev(rev) {}
};
const E INF = numeric_limits<E>::max();
vector<vector<edge> > G;
vector<E> h, dist;
vector<int> prevv, preve;
PrimalDual() {}
PrimalDual(int n) :G(n), h(n), dist(n), prevv(n), preve(n) {}
void add_edge(int from, int to, T cap, E cost) {
G[from].emplace_back(to, cap, cost, G[to].size());
G[to].emplace_back(from, 0, -cost, G[from].size() - 1);
}
E min_cost_flow(int s, int t, T f) {
E res = 0;
priority_queue<pair<E, int>, vector<pair<E, int> >, greater<pair<E, int> > > pq;
while (f > 0) {
fill(dist.begin(), dist.end(), INF);
pq.emplace(0, s);
dist[s] = 0;
while (!pq.empty()) {
pair<E, int> p = pq.top();
pq.pop();
if (dist[p.second] < p.first) continue;
for (int i = 0; i < G[p.second].size(); ++i) {
edge& e = G[p.second][i];
E ncost = dist[p.second] + e.cost + h[p.second] - h[e.to];
if (e.cap > 0 && dist[e.to] > ncost) {
dist[e.to] = ncost;
prevv[e.to] = p.second; preve[e.to] = i;
pq.emplace(dist[e.to], e.to);
}
}
}
if (dist[t] == INF) return -1;
for (int v = 0; v < h.size(); ++v)
if (dist[v] < INF) h[v] += dist[v];
T d = f;
for (int v = t; v != s; v = prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap);
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
edge& e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
};
//END
int main() {
int V, E, F; cin >> V >> E >> F;
PrimalDual<int, int> G(V);
for (int i = 0; i < E; ++i) {
int u, v, c, d; cin >> u >> v >> c >> d;
G.add_edge(u, v, c, d);
}
cout << G.min_cost_flow(0, V - 1, F) << endl;
return 0;
}
/*
created: 2020-01-06
https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/6/GRL_6_B
*/
|
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i,n) for(int i=0;i<(int)n;++i)
//debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
template < typename T >
void vprint(T &v){
REP(i, v.size()){
cout << v[i] << " ";
}
cout << endl;
}
struct edge{
ll to, cap, cost, rev;
edge(ll to, ll cap, ll cost, ll rev): to(to), cap(cap), cost(cost), rev(rev) {}
};
typedef vector<edge> edges;
typedef vector<edges> graph;
ll INF= LLONG_MAX/3;
void add_edge(graph &G, ll from, ll to, ll cap, ll cost){
G[from].push_back(edge(to, cap, cost, G[to].size()));
G[to].push_back(edge(from, 0, -cost, G[from].size()-1));
}
ll SSC(graph &G, vector<ll> &excess){
ll V = G.size();
map<ll, ll> E, D;
REP(i, V){
if(excess[i]>0) E[i] = excess[i];
if(excess[i]<0) D[i] = excess[i];
}
ll total_cost = 0;
vector<ll> pot(V, 0);
vector<ll> prevv(V);
vector<ll> preve(V);
while(!E.empty()){
ll s, t;
s = E.begin()->first;
// dijkstra
priority_queue< pll, vector<pll>, greater<pll> > pq;
bool feasibility_flag = false;
vector<ll> dist(V, INF);
vector<bool> visited(V, false);
dist[s] = 0;
pq.push(pll(0, s));
while(!pq.empty()){
pll p = pq.top();
pq.pop();
ll v = p.second;
visited[v] = true;
if(excess[v]<0){
t = v;
feasibility_flag = true;
break;
}
if(dist[v]<p.first) continue;
for(int i=0;i<G[v].size();i++){
edge &e = G[v][i];
if(e.cap>0 && dist[e.to]>dist[v]+e.cost+pot[v]-pot[e.to]){
dist[e.to] = dist[v] + e.cost + pot[v] - pot[e.to];
prevv[e.to] = v;
preve[e.to] = i;
pq.push(pll(dist[e.to], e.to));
}
}
}
// check fesibility
if(!feasibility_flag) return -1;
for(auto itr=D.begin();itr!=D.end();itr++)
// update potential
for(int i=0;i<V;i++) pot[i] += (visited[i] ? dist[i] : dist[t]);
// get flow amount
ll f = min(E[s], -D[t]);
ll d = f;
for(int v=t;v!=s;v=prevv[v]){
d = min(d, G[prevv[v]][preve[v]].cap);
}
// update excess and deficit
E[s] -= d;
if(E[s]==0) E.erase(s);
D[t] += d;
if(D[t]==0) D.erase(t);
// update total cost
total_cost += pot[t]*d;
// update residual graph
for(int v=t;v!=s;v=prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return total_cost;
}
int main(){
ll V, E, F;
cin >> V >> E >> F;
graph G(V);
REP(i, E){
ll u, v, c, d;
cin >> u >> v >> c >> d;
add_edge(G, u, v, c, d);
}
vector<ll> excess(V, 0);
excess[0] = F;
excess[V-1] = -F;
ll res = SSC(G, excess);
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = (int)j;i <= (int)k;i ++)
#define debug(x) cerr<<#x<<":"<<x<<endl
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define NUM 100
struct Edge{
Edge(int arg_to,int arg_capacity,int arg_cost,int arg_rev_index){
to = arg_to;
capacity = arg_capacity;
cost = arg_cost;
rev_index = arg_rev_index;
}
int to,capacity,cost,rev_index;
};
int V;
vector<Edge> G[NUM];
int dist[NUM];
int pre_node[NUM],pre_edge[NUM];
void add_edge(int from,int to,int capacity,int cost){
G[from].push_back(Edge(to,capacity,cost,G[to].size()));
G[to].push_back(Edge(from,0,-cost,G[from].size()-1));
}
int min_cost_flow(int source,int sink,int flow){
int ret = 0;
while(flow > 0){
for(int i = 0; i < V; i++)dist[i] = BIG_NUM;
dist[source] = 0;
bool update = true;
while(update){
update = false;
for(int node_id = 0; node_id < V; node_id++){
if(dist[node_id] == BIG_NUM)continue;
for(int i = 0; i < G[node_id].size(); i++){
Edge &e = G[node_id][i];
if(e.capacity > 0 && dist[e.to] > dist[node_id]+e.cost){
dist[e.to] = dist[node_id]+e.cost;
pre_node[e.to] = node_id;
pre_edge[e.to] = i;
update = true;
}
}
}
}
if(dist[sink] == BIG_NUM){
return -1;
}
int tmp_flow = flow;
for(int node_id = sink; node_id != source; node_id = pre_node[node_id]){
tmp_flow = min(tmp_flow,G[pre_node[node_id]][pre_edge[node_id]].capacity);
}
flow -= tmp_flow;
ret += tmp_flow*dist[sink];
for(int node_id = sink; node_id != source; node_id = pre_node[node_id]){
Edge &e = G[pre_node[node_id]][pre_edge[node_id]];
e.capacity -= tmp_flow;
G[node_id][e.rev_index].capacity += tmp_flow;
}
}
return ret;
}
int main(){
int E,F;
scanf("%d %d %d",&V,&E,&F);
int from,to,capacity,cost;
for(int loop = 0; loop < E; loop++){
scanf("%d %d %d %d",&from,&to,&capacity,&cost);
add_edge(from,to,capacity,cost);
}
printf("%d\n", min_cost_flow(0,V-1,F));
return 0;
}
|
// AOJ GRL_6_B: Network Flow - Minimum Cost Flow
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define Get(t, i) get<(i)>((t))
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for(auto& a : b)
const int inf = 1 << 25;
const ll INF = 1LL << 55;
struct edge
{
int to, capacity, cost, rev;
edge(){}
edge(int to, int capacity, int cost, int rev):to(to), capacity(capacity), cost(cost), rev(rev){}
};
struct PrimalDual
{
vector< vector<edge> > graph;
vector<int> potential, mincost, prevv, preve;
PrimalDual(int V):graph(V), potential(V), mincost(V), prevv(V), preve(V){}
void add_edge(int from, int to, int capacity, int cost)
{
graph[from].push_back(edge(to, capacity, cost, (int)graph[to].size()));
graph[to].push_back(edge(from, 0, -cost, (int)graph[from].size()-1));
}
int min_cost_flow(int source, int sink, int f)
{
int ret = 0;
fill(potential.begin(), potential.end(), 0);
fill(prevv.begin(), prevv.end(), -1);
fill(preve.begin(), preve.end(), -1);
while(f > 0) {
priority_queue<Pi, vector<Pi>, greater<Pi> > que;
fill(mincost.begin(), mincost.end(), inf);
mincost[source] = 0;
que.push(Pi(0, source));
while(!que.empty()) {
Pi p = que.top(); que.pop();
int v = p.second;
if(mincost[v] < p.first) continue;
for(int i = 0; i < (int)graph[v].size(); i++) {
edge& e = graph[v][i];
int dual_cost = mincost[v] + e.cost + potential[v] - potential[e.to];
if(e.capacity > 0 && dual_cost < mincost[e.to]) {
mincost[e.to] = dual_cost;
prevv[e.to] = v; preve[e.to] = i;
que.push(Pi(mincost[e.to], e.to));
}
}
}
if(mincost[sink] == inf) return -1;
for(int v = 0; v < (int)graph.size(); v++) potential[v] += mincost[v];
int d = f;
for(int v = sink; v != source; v = prevv[v]) d = min(d, graph[prevv[v]][preve[v]].capacity);
f -= d;
ret += d * potential[sink];
for(int v = sink; v != source; v = prevv[v]) {
edge& e = graph[prevv[v]][preve[v]];
e.capacity -= d;
graph[v][e.rev].capacity += d;
}
}
return ret;
}
};
int main()
{
int V, E, F;
cin >> V >> E >> F;
PrimalDual mnf(V);
while(E--) {
int u, v, c, d;
cin >> u >> v >> c >> d;
mnf.add_edge(u, v, c, d);
}
cout << mnf.min_cost_flow(0, V-1, F) << endl;
return 0;
}
|
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
using Weight=long long;
static const Weight INF=1LL<<57;
template <class T>
using gp_queue=priority_queue<T, deque<T>, less<T>>;
struct Arc {
// accessible to the reverse edge
size_t src, dst;
Weight capacity, cost, flow;
size_t rev;
Arc() {}
Arc(size_t src, size_t dst, Weight capacity, Weight cost=0):
src(src), dst(dst), capacity(capacity), cost(cost), flow(0), rev(-1)
{}
Weight residue() const {
return capacity - flow;
}
};
bool operator<(const Arc &e, const Arc &f) {
if (e.cost != f.cost) {
return e.cost > f.cost;
} else if (e.capacity != f.capacity) {
return e.capacity > f.capacity;
} else {
return e.src!=f.src? e.src<f.src : e.dst<f.dst;
}
}
using Arcs=vector<Arc>;
using Node=vector<Arc>;
using FlowNetwork=vector<Node>;
void connect(FlowNetwork &g, size_t s, size_t d, Weight c=1, Weight k=0) {
g[s].push_back(Arc(s, d, c, k));
g[d].push_back(Arc(d, s, 0, -k));
g[s].back().rev = g[d].size()-1;
g[d].back().rev = g[s].size()-1;
}
void join(FlowNetwork &g, size_t s, size_t d, Weight c=1, Weight k=0) {
g[s].push_back(Arc(s, d, c, k));
g[d].push_back(Arc(d, s, c, k));
g[s].back().rev = g[d].size()-1;
g[d].back().rev = g[s].size()-1;
}
Weight mincost_flow(FlowNetwork &g, size_t s, size_t t, Weight F=INF) {
size_t V=g.size();
vector<Arc *> prev(V, NULL);
Weight mcost=0;
while (F > 0) {
vector<Weight> d(V, INF); d[s]=0;
for (bool updated=true; updated;) {
updated = false;
for (size_t v=0; v<V; ++v) {
if (d[v] == INF) continue;
for (Arc &e: g[v]) {
if (e.capacity <= 0) continue;
if (d[e.dst] > d[e.src] + e.cost) {
d[e.dst] = d[e.src] + e.cost;
prev[e.dst] = &e;
updated = true;
}
}
}
}
if (d[t] == INF)
return -1;
Weight f=F;
for (Arc *e=prev[t]; e!=NULL; e=prev[e->src])
f = min(f, e->capacity);
F -= f;
mcost += f * d[t];
for (Arc *e=prev[t]; e!=NULL; e=prev[e->src]) {
e->capacity -= f;
g[e->dst][e->rev].capacity += f;
}
}
return mcost;
}
int main() {
size_t V, E;
Weight F;
scanf("%zu %zu %lld", &V, &E, &F);
FlowNetwork g(V);
for (size_t i=0; i<E; ++i) {
size_t u, v;
Weight c, d;
scanf("%zu %zu %lld %lld", &u, &v, &c, &d);
connect(g, u, v, c, d);
}
printf("%lld\n", mincost_flow(g, 0, V-1, F));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
struct min_cost_flow_graph {
struct edge {
int from, to;
T cap, f;
U cost;
};
vector<edge> edges;
vector<vector<int>> g;
int n, st, fin;
T required_flow, flow;
U cost;
min_cost_flow_graph(int n, int st, int fin, T required_flow)
: n(n), st(st), fin(fin), required_flow(required_flow) {
assert(0 <= st && st < n && 0 <= fin && fin < n && st != fin);
g.resize(n);
flow = 0;
cost = 0;
}
void clear_flow() {
for (const edge &e : edges) {
e.f = 0;
}
flow = 0;
cost = 0;
}
void add(int from, int to, T cap = 1, T rev_cap = 0, U cost = 1) {
assert(0 <= from && from < n && 0 <= to && to < n);
g[from].emplace_back(edges.size());
edges.push_back({from, to, cap, 0, cost});
g[to].emplace_back(edges.size());
edges.push_back({to, from, rev_cap, 0, -cost});
}
U min_cost_flow() {
vector<U> h(n, 0);
while (flow < required_flow) {
vector<int> preve(n);
vector<U> dist(n, numeric_limits<U>::max() / 2);
priority_queue<pair<U, int>, vector<pair<U, int>>, greater<pair<U, int>>> pq;
dist[st] = 0;
pq.emplace(dist[st], st);
while (!pq.empty()) {
U expected = pq.top().first;
int i = pq.top().second;
pq.pop();
if (expected != dist[i]) {
continue;
}
for (int id : g[i]) {
const edge &e = edges[id];
if (0 < e.cap - e.f && dist[e.from] + e.cost + h[e.from] - h[e.to] < dist[e.to]) {
dist[e.to] = dist[e.from] + e.cost + h[e.from] - h[e.to];
preve[e.to] = id;
pq.emplace(dist[e.to], e.to);
}
}
}
if (dist[fin] == numeric_limits<U>::max() / 2) {
return -1;
}
for (int i = 0; i < n; i++) {
h[i] += dist[i];
}
T d = required_flow - flow;
for (int v = fin; v != st; v = edges[preve[v]].from) {
d = min(d, edges[preve[v]].cap - edges[preve[v]].f);
}
flow += d;
cost += d * h[fin];
for (int v = fin; v != st; v = edges[preve[v]].from) {
edges[preve[v]].f += d;
edges[preve[v] ^ 1].f -= d;
}
}
return cost;
}
};
int main() {
int n, m, f;
cin >> n >> m >> f;
min_cost_flow_graph<int, int> g(n, 0, n - 1, f);
for (int i = 0; i < m; i++) {
int from, to, cap, cost;
cin >> from >> to >> cap >> cost;
g.add(from, to, cap, 0, cost);
}
cout << g.min_cost_flow() << endl;
return 0;
}
|
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
namespace Flow {
struct Edge {
int to, capacity, cost, reverse;
Edge(int to, int capacity, int cost, int reverse) :
to(to), capacity(capacity), cost(cost), reverse(reverse) {}
};
template <int MaxN> class PrimalDual {
int n = MaxN;
vector<Edge> g[MaxN];
public:
void clear(int n) {
this->n = n;
for (int i = 0; i < n; ++i)
g[i].clear();
}
void push(int from, int to, int capacity = 1, int cost = 0) {
g[from].emplace_back(to, capacity, cost, g[to].size());
g[to].emplace_back(from, 0, -cost, g[from].size() - 1);
}
pair<int, int> flow(int s, int t, int f = 1 << 30) {
int flow = f, cost = 0;
static int prevv[MaxN], preve[MaxN], dist[MaxN], h[MaxN];
fill(h, h + n, 0);
while (flow > 0) {
fill(dist, dist + n, 1 << 30);
dist[s] = 0;
using P = pair<int, int>;
priority_queue<P, vector<P>, greater<P>> q;
for (q.emplace(0, s); q.size(); ) {
auto p = q.top();
q.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (auto& e : g[v]) {
int d = dist[v] + e.cost + h[v] - h[e.to];
if (e.capacity > 0 && dist[e.to] > d) {
dist[e.to] = d;
prevv[e.to] = v;
preve[e.to] = &e - &g[v][0];
q.emplace(d, e.to);
}
}
}
if (dist[t] == 1 << 30) break;
for (int v = 0; v < n; ++v)
h[v] += dist[v];
int d = flow;
for (int v = t; v != s; v = prevv[v])
d = min(d, g[prevv[v]][preve[v]].capacity);
flow -= d;
cost += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
auto& e = g[prevv[v]][preve[v]];
e.capacity -= d;
g[v][e.reverse].capacity += d;
}
}
return { f - flow, cost };
}
};
}
int main() {
int n, m, f;
cin >> n >> m >> f;
Flow::PrimalDual<100> pd;
pd.clear(n);
for (int i = 0; i < m; ++i) {
int a, b, c, d;
cin >> a >> b >> c >> d;
pd.push(a, b, c, d);
}
auto p = pd.flow(0, n - 1, f);
cout << (p.first == f ? p.second : -1) << endl;
return 0;
}
|
//最短経路長が短い経路に流せるだけ流す。
//ただし、順辺のコストをcとしたときに逆辺のコストは-cにする (フローを押し戻すとき、コストも押し戻すため)。
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int VMAX = 100;
struct Edge {
int from, to, flow, cost, rev;
Edge(int from, int to, int flow, int cost, int rev) {
this->from = from;
this->to = to;
this->flow = flow;
this->cost = cost;
this->rev = rev;
}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &g, int from, int to, int flow, int cost) {
g[from].push_back(Edge(from, to, flow, cost, g[to].size()));
g[to].push_back(Edge(to, from, 0, -cost, g[from].size() - 1));
}
int dfs(Graph &g, int s, int t, int flow, bool yet[], int level[]) {
yet[s] = false;
if (s == t) return flow;
for (int i = 0; i < g[s].size(); i++) {
int v = g[s][i].to;
if (g[s][i].flow > 0 && yet[v] && level[v] == level[s] + g[s][i].cost) {
int res = dfs(g, v, t, min(flow, g[s][i].flow), yet, level);
if (res > 0) {
int r = g[s][i].rev;
g[s][i].flow -= res;
g[v][r].flow += res;
return res;
}
}
}
return 0;
}
int minCostflow(Graph &g, int s, int t, int flow) {
const int INF_DIST = 11451419;
const int INF_FLOW = 11451419;
const int ERROR_RETURN = -1;
int level[VMAX];
bool yet[VMAX];
queue<int> que;
int i;
int ret = 0;
while (true) {
for (i = 0; i < g.size(); i++) level[i] = INF_DIST;
que.push(s); level[s] = 0;
while (!que.empty()) {
int v = que.front(); que.pop();
for (i = 0; i < g[v].size(); i++) {
int nv = g[v][i].to;
if (g[v][i].flow > 0 && level[nv] > level[v] + g[v][i].cost) {
level[nv] = level[v] + g[v][i].cost;
que.push(nv);
}
}
}
if (level[t] >= INF_DIST) break;
while (true) {
for (i = 0; i < g.size(); i++) yet[i] = true;
int f = dfs(g, s, t, INF_FLOW, yet, level);
if (f == 0) break;
flow -= f;
ret += f * level[t];
if (flow <= 0) {
ret += flow * level[t];
return ret;
}
}
}
return -1;
}
int n, m, f;
int u, v, c, d;
Graph g;
int main() {
cin >> n >> m >> f;
g.resize(n);
for (int i = 0; i < m; i++) {
cin >> u >> v >> c >> d;
add_edge(g, u, v, c, d);
}
cout << minCostflow(g, 0, n - 1, f) << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
struct PrimalDual{
const int INF = 1<<28;
typedef pair<int,int> P;
struct edge{
int to,cap,cost,rev;
edge(){}
edge(int to,int cap,int cost,int rev):to(to),cap(cap),cost(cost),rev(rev){}
};
int V;
vector<vector<edge> > G;
vector<int> h,dist,prevv,preve;
PrimalDual(){}
PrimalDual(int V):V(V){init();}
void init(){
for(int i=0;i<(int)G.size();i++) G[i].clear();
G.clear();
h.clear();
dist.clear();
prevv.clear();
preve.clear();
G.resize(V);
h.resize(V);
dist.resize(V);
prevv.resize(V);
preve.resize(V);
}
void add_edge(int from,int to,int cap,int cost){
G[from].push_back(edge(to,cap,cost,G[to].size()));
G[to].push_back(edge(from,0,-cost,G[from].size()-1));
}
int min_cost_flow(int s,int t,int f){
int res=0;
fill(h.begin(),h.begin()+V,0);
while(f>0){
priority_queue<P,vector<P>,greater<P> > que;
fill(dist.begin(),dist.begin()+V,INF);
dist[s]=0;
que.push(P(0,s));
while(!que.empty()){
P p=que.top();que.pop();
int v=p.second;
if(dist[v]<p.first) continue;
for(int i=0;i<(int)G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
prevv[e.to]=v;
preve[e.to]=i;
que.push(P(dist[e.to],e.to));
}
}
}
if(dist[t]==INF){
return -1;
}
for(int v=0;v<V;v++) h[v]+=dist[v];
int d=f;
for(int v=t;v!=s;v=prevv[v]){
d=min(d,G[prevv[v]][preve[v]].cap);
}
f-=d;
res+=d*h[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e=G[prevv[v]][preve[v]];
e.cap-=d;
G[v][e.rev].cap+=d;
}
}
return res;
}
};
int main(){
int v,e,f;
cin>>v>>e>>f;
PrimalDual pd(v);
for(int i=0;i<e;i++){
int u,v,c,d;
cin>>u>>v>>c>>d;
pd.add_edge(u,v,c,d);
}
cout<<pd.min_cost_flow(0,v-1,f)<<endl;
return 0;
}
/*
verified on 2017/06/29
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B
*/
|
#include <iostream>
#include <vector>
#include <numeric>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
using namespace std;
//template<typename Int = int>
using Int = int;
struct PrimalDual {
struct Edge {
int dst;
Int cap, flow, cost;
int rev;
bool isRev;
Edge(int dst, Int cap, Int flow, Int cost, int rev, bool isRev)
:dst(dst), cap(cap), flow(flow), cost(cost), rev(rev), isRev(isRev) {
}
};
int n;
vector<vector<Edge>> g;
PrimalDual(int n_) : n(n_), g(vector<vector<Edge>>(n_)) {}
void addEdge(int src, int dst, Int cap, Int cost) {
g[src].emplace_back(dst, cap, 0, cost, g[dst].size(), false);
g[dst].emplace_back(src, cap, cap, -cost, g[src].size() - 1, true);
}
vector<Int> h, dist;
vector<int> prevv, preve;
Int solve(int s, int t, Int f) {
Int res = 0;
h.assign(n, 0);
dist.assign(n, 0);
prevv.assign(n, 0);
preve.assign(n, 0);
while (f > 0) {
if (!dijkstra(s,t)) return -1;
for (int i = 0; i < n; ++i) h[i] += dist[i];
Int d = f;
for (int v = t; v != s; v = prevv[v]) {
Edge &e = g[prevv[v]][preve[v]];
d = min(d, residue(e));
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
Edge &e = g[prevv[v]][preve[v]];
e.flow += d;
g[v][e.rev].flow -= d;
}
}
return res;
}
bool dijkstra(int s, int t) {
constexpr Int INF = numeric_limits<Int>::max();
dist.assign(n, INF);
dist[s] = 0;
priority_queue<pair<Int, int>> q;
fill(dist.begin(), dist.end(), INF);
dist[s] = 0;
q.emplace(0, s);
while (q.size()) {
int d, v;
tie(d, v) = q.top();
q.pop();
d = -d;
if (dist[v] < d) continue;
for (int i = 0; i < (int)g[v].size(); ++i) {
Edge &e = g[v][i];
if (residue(e) > 0 && dist[e.dst] > dist[v] + e.cost + h[v] - h[e.dst]) {
dist[e.dst] = dist[v] + e.cost + h[v] - h[e.dst];
prevv[e.dst] = v;
preve[e.dst] = i;
q.emplace(-dist[e.dst], e.dst);
}
}
}
return dist[t] != INF;
}
Int residue(const Edge& e) { return e.cap - e.flow; }
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, f;
cin >> n >> m >> f;
PrimalDual pd(n);
for (int i = 0; i < m; i++) {
int u, v, c, d;
cin >> u >> v >> c >> d;
pd.addEdge(u, v, c, d);
}
cout << pd.solve(0, n - 1, f) << endl;
}
|
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
template<typename F,typename C>
struct MinCostFlow{
struct Edge{
int to;
F cap;
C cost;
int rev;
};
const C INF;
vector<vector<Edge>> v;
vector<C> pot,dist;
vector<int> prevv,preve;
MinCostFlow(int n):v(n),INF(numeric_limits<C>::max()){}
void add_edge(int from,int to,F cap,C cost){
v[from].emplace_back((Edge){to,cap,cost,(int)v[to].size()});
v[to].emplace_back((Edge){from,0,-cost,(int)v[from].size()-1});
}
C min_cost_flow(int s,int t,F f){
int n=v.size();
C ret=0;
using P = pair<C,int>;
priority_queue<P,vector<P>,greater<P>> que;
pot.assign(n, 0);
prevv.assign(n, -1);
preve.assign(n, -1);
while(f>0){
dist.assign(n, INF);
que.emplace(0,s);
dist[s]=0;
while(!que.empty()){
int cur=que.top().second;
C d=que.top().first;
que.pop();
if(dist[cur]<d)continue;
rep(i,v[cur].size()){
Edge &e = v[cur][i];
C next_cost = dist[cur]+e.cost+pot[cur]-pot[e.to];
if(e.cap>0 && dist[e.to]>next_cost){
dist[e.to]=next_cost;
prevv[e.to]=cur;
preve[e.to]=i;
que.emplace(dist[e.to],e.to);
}
}
}
if(dist[t]==INF)return -1;
rep(i,n)pot[i]+=dist[i];
F add_flow = f;
for(int u=t;u!=s;u=prevv[u]){
add_flow=min(add_flow,v[prevv[u]][preve[u]].cap);
}
f-=add_flow;
ret+=add_flow*pot[t];
for(int u=t;u!=s;u=prevv[u]){
Edge& e=v[prevv[u]][preve[u]];
e.cap -=add_flow;
v[e.to][e.rev].cap += add_flow;
}
}
return ret;
}
};
int main(){
int n,m,f;
cin>>n>>m>>f;
MinCostFlow<int,int> mcf(n);
rep(i,m){
int a,b,c,d;
cin>>a>>b>>c>>d;
mcf.add_edge(a, b, c, d);
}
cout<<mcf.min_cost_flow(0, n-1, f)<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using Int = int64_t;
using UInt = uint64_t;
using C = std::complex<double>;
#define rep(i, n) for(Int i = 0; i < (Int)(n); ++i)
#define rep2(i, l, r) for(Int i = (Int)(l); i < (Int)(r); ++i)
#define guard(x) if( not (x) ) continue;
#ifndef LOCAL_
#define fprintf if( false ) fprintf
#endif
template<typename T>
using RQ = std::priority_queue<T, std::vector<T>, std::greater<T>>;
int main() {
Int v, e, f;
std::cin >> v >> e >> f;
std::vector<Int> ss(e+1), ts(e+1), cs(e+1), fs(e+1), bs(e+1);
rep2(i,1,e+1) {
Int a, b, c, d;
std::cin >> a >> b >> c >> d;
ss[i] = a, ts[i] = b, cs[i] = d, fs[i] = 0, bs[i] = c;
}
std::vector<std::vector<Int>> es(v);
rep2(i,1,e+1) {
Int s = ss[i], t = ts[i];
es[s].emplace_back(i);
es[t].emplace_back(-i);
}
Int res = 0;
Int source = 0, sink = v-1;
std::vector<Int> hs(v);
while( f > 0 ) {
RQ<std::pair<Int,Int>> q;
q.emplace(0, source);
std::vector<Int> ps(v, -1), xs(v, -1), ys(v);
xs[source] = 0;
ys[source] = f;
std::vector<bool> visited(v);
while( not q.empty() ) {
Int d, s; std::tie(d, s) = q.top(); q.pop();
guard( d == xs[s] );
assert( not visited[s] );
visited[s] = true;
for(Int i : es[s]) {
Int k = std::abs(i);
Int t = i > 0 ? ts[k] : ss[k];
Int tf = i > 0 ? bs[k] : fs[k];
guard( tf > 0 );
Int nd = d + (i>0?cs[k]:-cs[k]) + hs[s] - hs[t];
guard( xs[t] == -1 or xs[t] > nd );
assert( nd >= 0 );
xs[t] = nd;
ps[t] = i;
ys[t] = std::min(ys[s], tf);
assert( not visited[t] );
q.emplace(nd, t);
}
}
Int tf = ys[sink];
fprintf(stderr, "tf = %ld\n", tf);
f -= tf;
if( f > 0 and tf == 0 ) {
res = -1;
break;
}
rep(i, v) hs[i] += xs[i];
for(Int i=sink, k=ps[i]; i!=source; i=(k>0?ss[k]:ts[-k]), k=ps[i]) {
Int ak = std::abs(k);
fprintf(stderr, "i=%ld, k=%ld\n", i, k);
if( k > 0 ) {
fs[ak] += tf;
bs[ak] -= tf;
}
else {
fs[ak] -= tf;
bs[ak] += tf;
}
}
res += tf * hs[sink];
if( f == 0 ) break;
rep2(i,1,e+1) {
fprintf(stderr, "%ld -> %ld : cost=%ld : ->=%ld : <-=%ld\n", ss[i], ts[i], cs[i], fs[i], bs[i]);
}
}
printf("%ld\n", res);
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<P> vp;
typedef vector<ll> vll;
const int INF = 99999999;
const int MAX_V = 100000;
struct edge { int to, cap, cost, rev; };
int V, E, F;
vector<edge> G[MAX_V];
int h[MAX_V]; // potential
int dist[MAX_V];
int prevv[MAX_V], preve[MAX_V]; // privious vertex, edge
void add_edge(int from, int to, int cap, int cost) {
G[from].emplace_back((edge){to, cap, cost, (int)G[to].size()});
G[to].emplace_back((edge){from, 0, -cost, (int)G[from].size() - 1});
}
// get min cost flow from s to t
// if we can flow f, then return -1
int min_cost_flow(int s, int t, int f) {
int res = 0;
fill(h, h + V, 0);
while (f > 0) {
// update h by dijkstra
priority_queue<P, vector<P>, greater<P>> que;
fill(dist, dist + V, INF);
dist[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (dist[v] < p.first) continue;
rep(i, G[v].size()) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P(dist[e.to], e.to));
}
}
}
if (dist[t] == INF) {
// no more flow
return -1;
}
rep(v, V) h[v] += dist[v];
// flow as much as possible along the minimum path from s to t
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, (int)G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
signed main() {
cin >> V >> E >> F;
rep(i, E) {
int u, v, c, d;
cin >> u >> v >> c >> d;
add_edge(u, v, c, d);
}
cout << min_cost_flow(0, V - 1, F) << endl;
}
|
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <tuple>
#include <cassert>
#include <exception>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i = (a);i < (n);i++)
#define per(i,a,n) for(ll i = (a);i > (n);i--)
#define lep(i,a,n) for(ll i = (a);i <= (n);i++)
#define pel(i,a,n) for(ll i = (a);i >= (n);i--)
#define clr(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define sz size()
#define print(X) cout << (X) << endl
#define fprint(NUM,X) cout << fixed << setprecision(NUM) << (X) << endl
#define fprints(NUM,X,Y) cout << fixed << setprecision(NUM) << (X) << " " << (Y) << endl
static const int INF = 1e+9+7;
ll n,m,l;
string s,t;
ll d[200010],dp[1010][1010];
double w[1000],v[1000];
double box[200010];
struct edge{ int to,cap,cost,rev; };
static const int MAX = 100000;
int V;
vector<edge> graph[MAX];
int dist[MAX];
int prevv[MAX],preve[MAX];
void add_edge(int from,int to,int cap,int cost){
graph[from].push_back((edge){ to,cap,cost,(int)graph[to].size() });
graph[to].push_back((edge){ from,0,-cost,(int)graph[from].size()-1 });
}
int min_cost_flow(int s,int t,int f){
int res = 0;
while(f > 0){
fill(dist,dist + V,INF);
dist[s] = 0;
bool update = true;
while(update){
update = false;
for(int v = 0;v < V;v++){
if(dist[v] == INF)continue;
for(int i = 0;i < graph[v].size();i++){
edge &e = graph[v][i];
if(e.cap > 0 && dist[e.to] > dist[v] + e.cost){
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if(dist[t] == INF)return -1;
int d = f;
for(int v = t;v != s;v = prevv[v]){
d = min(d,graph[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * dist[t];
for(int v = t;v != s;v = prevv[v]){
edge &e = graph[prevv[v]][preve[v]];
e.cap -= d;
graph[v][e.rev].cap += d;
}
}
return res;
}
int main(){
cin >> V >> n >> m;
rep(i,0,n){
int a,b,c,e;
cin >> a >> b >> c >> e;
add_edge(a,b,c,e);
}
print(min_cost_flow(0,V-1,m));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
struct min_cost_flow_graph {
struct edge {
int from, to;
T cap, f;
U cost;
};
vector<edge> edges;
vector<vector<int>> g;
int n, st, fin;
T required_flow, flow;
U cost;
min_cost_flow_graph(int n, int st, int fin, T required_flow)
: n(n), st(st), fin(fin), required_flow(required_flow) {
assert(0 <= st && st < n && 0 <= fin && fin < n && st != fin);
g.resize(n);
flow = 0;
cost = 0;
}
void clear_flow() {
for (const edge &e : edges) {
e.f = 0;
}
flow = 0;
cost = 0;
}
void add(int from, int to, T cap = 1, T rev_cap = 0, U cost = 1) {
assert(0 <= from && from < n && 0 <= to && to < n);
g[from].emplace_back(edges.size());
edges.push_back({from, to, cap, 0, cost});
g[to].emplace_back(edges.size());
edges.push_back({to, from, rev_cap, 0, -cost});
}
U min_cost_flow() {
while (flow < required_flow) {
vector<int> prevv(n), preve(n);
vector<U> dist(n, numeric_limits<U>::max() / 4);
dist[st] = 0;
for (bool update = true; update; ) {
update = false;
for (int i = 0; i < n; i++) {
for (int id : g[i]) {
const edge &e = edges[id];
if (0 < e.cap - e.f && dist[e.from] + e.cost < dist[e.to]) {
dist[e.to] = dist[e.from] + e.cost;
prevv[e.to] = e.from;
preve[e.to] = id;
update = true;
}
}
}
}
if (dist[fin] == numeric_limits<U>::max() / 4) {
return -1;
}
T d = required_flow - flow;
for (int v = fin; v != st; v = prevv[v]) {
d = min(d, edges[preve[v]].cap - edges[preve[v]].f);
}
flow += d;
cost += d * dist[fin];
for (int v = fin; v != st; v = prevv[v]) {
edges[preve[v]].f += d;
edges[preve[v] ^ 1].f -= d;
}
}
return cost;
}
};
int main() {
int n, m, f;
cin >> n >> m >> f;
min_cost_flow_graph<int, int> g(n, 0, n - 1, f);
for (int i = 0; i < m; i++) {
int from, to, cap, cost;
cin >> from >> to >> cap >> cost;
g.add(from, to, cap, 0, cost);
}
cout << g.min_cost_flow() << endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
* 最小費用流
* O(FVE)
*/
struct MinCostFlow{
const int inf = std::numeric_limits<int>::max()/3;
struct edge{
int to,cap,cost,rev;
edge(){}
edge(int t,int ca,int co,int r) : to(t),cap(ca),cost(co),rev(r){}
};
int V; // # of node
vector<vector<edge>> G;
MinCostFlow(){}
MinCostFlow(int V_){ init(V_); }
void init(int V_){
V = V_;
G.resize(V);
}
void add_edge(int from,int to,int cap,int cost){
G[from].emplace_back(to,cap,cost,G[to].size());
G[to].emplace_back(from,0,-cost,G[from].size()-1);
}
// s->tに向かって流量f流すときの最小費用
// もしs->tに向かってf流せない場合は-1
int min_cost_flow(int s,int t,int f){
int res=0;
vector<int> dist(V,inf);
vector<int> prevv(V),preve(V);
while(f>0){
dist.assign(V,inf);
dist[s] = 0;
bool update=true;
while(update){
update=false;
for(int v=0;v<V;v++){
if(dist[v]==inf) continue;
for(int i=0;i<(int)G[v].size();i++){
edge &e = G[v][i];
if(e.cap>0 and dist[e.to] > dist[v]+e.cost){
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if(dist[t] == inf) return -1; // 流量fを流すことは不可能
int d = f;
for(int v=t;v!=s;v=prevv[v]) d = min(d,G[prevv[v]][preve[v]].cap);
f -= d;
res += d * dist[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
};
int main(){
int V,E,F; cin >> V >> E >> F;
MinCostFlow graph(V);
for(int i=0;i<E;i++){
int u,v,c,d;
cin >> u >> v >> c >> d;
graph.add_edge(u,v,c,d);
}
cout << graph.min_cost_flow(0,V-1,F) << endl;
}
|
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <cassert>
typedef long long ll;
using namespace std;
#define debug(x) cerr << #x << " = " << x << endl;
#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010
/* Minimum Cost Flow */
struct MinimumCostFlow{
typedef pair<int,int> P;
struct edge {
int to, cap, cost, rev;
edge(int a, int b, int c, int d):to(a),cap(b),cost(c),rev(d){}
};
vector<vector<edge> > G;
vector<int> h, dist, prevv, preve;
int V;
MinimumCostFlow(int v):G(v,vector<edge>()),prevv(v,0),preve(v,0),V(v){}
int add_edge(int from,int to,int cap, int cost){
int id = G[from].size();
G[from].push_back(edge(to,cap,cost,G[to].size()));
G[to].push_back(edge(from,0,-cost,G[from].size() - 1));
return id;
}
int solve(int s, int t, int f){
int res = 0;
h.assign(V,0);
while(f > 0){
priority_queue<P, vector<P>, greater<P> > pq;
dist.assign(V, INF);
dist[s] = 0;
pq.push(P(0, s));
while(pq.size()){
P p = pq.top(); pq.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(int i = 0; i < G[v].size(); i++){
edge &e = G[v][i];
int d = dist[v] + e.cost + h[v] - h[e.to];
if(e.cap > 0 && dist[e.to] > d){
dist[e.to] = d;
prevv[e.to] = v;
preve[e.to] = i;
pq.push(P(dist[e.to], e.to));
}
}
}
if(dist[t] == INF) return -1;
for(int v = 0; v < V; v++) h[v] += dist[v];
int d = f;
for(int v = t; v != s; v = prevv[v])
d = min(d, G[prevv[v]][preve[v]].cap);
f -= d;
res += d * h[t];
for(int v = t; v != s; v = prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
};
int main(){
int v,e,f;
int a,b,c,d;
scanf("%d%d%d",&v,&e,&f);
MinimumCostFlow flow(v);
for(int i=0;i<e;i++){
scanf("%d%d%d%d",&a,&b,&c,&d);
flow.add_edge(a,b,c,d);
}
cout << flow.solve(0, v-1, f) << endl;
return 0;
}
|
#include <iostream>
#include <fstream>
#include <cassert>
#include <typeinfo>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#include <random>
#include <complex>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<50;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
class Network{
private:
int V;
struct edge{
int to,cap,cost,rev;
};
vector<vector<edge> > g;
vi h,d,pv,pe;
public:
Network(int v){
V=v;
g=vector<vector<edge> >(v);
}
void add_edge(int s,int t,int cap,int cost){
g[s].push_back(edge{t,cap,cost,(int)g[t].size()});
g[t].push_back(edge{s,0,-cost,(int)g[s].size()-1});
}
int min_cost_flow(int s,int t,int f){
int res=0;
h=pv=pe=vi(V);
while(f>0){
priority_queue<P> q;
d=vi(V,inf);
d[s]=0;
q.push({0,s});
while(!q.empty()){
P p=q.top();
q.pop();
int v=p.second;
if(d[v]<-p.first) continue;
for(int i=0;i<g[v].size();i++){
edge &e=g[v][i];
if(e.cap>0&&d[e.to]>d[v]+e.cost+h[v]-h[e.to]){
d[e.to]=d[v]+e.cost+h[v]-h[e.to];
pv[e.to]=v;
pe[e.to]=i;
q.push({-d[e.to],e.to});
}
}
}
if(d[t]==inf) return -1;
for(int i=0;i<V;i++) h[i]+=d[i];
int D=f;
for(int i=t;i!=s;i=pv[i]) D=min(D,g[pv[i]][pe[i]].cap);
f-=D;
res+=D*h[t];
for(int i=t;i!=s;i=pv[i]){
edge &e=g[pv[i]][pe[i]];
e.cap-=D;
g[i][e.rev].cap+=D;
}
}
return res;
}
};
int n,m,f;
int main(){
cin>>n>>m>>f;
Network net(n);
for(int i=0;i<m;i++){
int v,u,c,d;
cin>>v>>u>>c>>d;
net.add_edge(v,u,c,d);
}
cout<<net.min_cost_flow(0,n-1,f)<<endl;
}
|
#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#define llint long long
#define inf 1e18
using namespace std;
typedef pair<llint, llint> P;
struct edge{
llint to, cap, cost, rev;
edge(){}
edge(llint a, llint b, llint c, llint d){
to = a, cap = b, cost = c, rev = d;
}
};
llint n, m, F;
llint S, T;
vector<edge> G[205];
llint dist[205];
llint prevv[205], preve[205];
llint h[205];
void BellmanFord()
{
for(int i = 0; i <= T; i++) dist[i] = inf;
dist[S] = 0, prevv[S] = -1;
bool update = true;
while(update){
update = false;
for(int i = 0; i <= T; i++){
for(int j = 0; j < G[i].size(); j++){
if(G[i][j].cap == 0) continue;
if(dist[G[i][j].to] > dist[i] + G[i][j].cost){
dist[G[i][j].to] = dist[i] + G[i][j].cost;
prevv[G[i][j].to] = i;
preve[G[i][j].to] = j;
update = true;
}
}
}
}
}
void Dijkstra()
{
for(int i = 0; i <= T; i++) dist[i] = inf;
dist[S] = 0, prevv[S] = -1;
priority_queue< P, vector<P>, greater<P> > Q;
Q.push( make_pair(0, S) );
llint v, d;
while(Q.size()){
d = Q.top().first;
v = Q.top().second;
Q.pop();
if(dist[v] < d) continue;
for(int i = 0; i < G[v].size(); i++){
if(G[v][i].cap == 0) continue;
llint u = G[v][i].to, c = h[v] - h[u] + G[v][i].cost;
if(dist[u] > d + c){
dist[u] = d + c;
prevv[u] = v;
preve[u] = i;
Q.push( make_pair(dist[u], u) );
}
}
}
}
void add_edge(llint from, llint to, llint cap, llint cost)
{
G[from].push_back( edge(to, cap, cost, G[to].size()) );
G[to].push_back( edge(from, 0, -cost, G[from].size()-1) );
}
int main()
{
cin >> n >> m >> F;
llint u, v, c, d;
for(int i = 1; i <= m; i++){
cin >> u >> v >> c >> d;
add_edge(u, v, c, d);
}
S = 0, T = n-1;
BellmanFord();
for(int i = 0; i <= T; i++) h[i] = dist[i];
llint f = F, ans = 0;
while(f > 0){
Dijkstra();
if(dist[T] >= inf) break;
llint p = T, flow = f;
while(prevv[p] != -1){
flow = min(flow, G[prevv[p]][preve[p]].cap);
p = prevv[p];
}
p = T;
while(prevv[p] != -1){
G[prevv[p]][preve[p]].cap -= flow;
G[p][G[prevv[p]][preve[p]].rev].cap += flow;
p = prevv[p];
}
f -= flow;
ans += (dist[T] + h[T] - h[S]) * flow;
for(int i = 0; i <= T; i++) h[i] += dist[i];
}
if(f > 0) ans = -1;
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <functional>
using namespace std;
const int MAX_V = 100010;
using Capacity = int;
using Cost = int;
const auto inf = numeric_limits<Capacity>::max() / 8;
struct Edge {
int dst;
Capacity cap, cap_orig;
Cost cost;
int revEdge; bool isRev;
Edge(int dst, Capacity cap, Cost cost, int revEdge, bool isRev)
:dst(dst), cap(cap), cap_orig(cap), cost(cost), revEdge(revEdge), isRev(isRev) {
}
};
struct PrimalDual {
int n;
vector<vector<Edge> > g;
PrimalDual(int n_) : n(n_), g(vector<vector<Edge> >(n_)) {}
void add_edge(int src, int dst, Capacity cap, Cost cost) { // ?????????
g[src].emplace_back(dst, cap, cost, g[dst].size(), false);
g[dst].emplace_back(src, 0, -cost, g[src].size() - 1, true);
}
Cost solve(int s, int t, int f) {
Cost res = 0;
// vector<Cost> h(g.size()), dist(g.size());
// vector<int> prevv(g.size()), preve(g.size());
static Cost h[MAX_V], dist[MAX_V];
static int prevv[MAX_V], preve[MAX_V];
for(int i = 0; i < n; i++) {
h[i] = 0;
}
while(f > 0) {
typedef pair<Cost, int> pcv;
priority_queue<pcv, vector<pcv>, greater<pcv> > q;
for(int i = 0; i < n; i++) {
dist[i] = inf;
}
dist[s] = 0;
q.emplace(pcv(0, s));
while(q.size()) {
pcv p = q.top(); q.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(int i = 0; i < g[v].size(); i++) {
Edge &e = g[v][i];
if(e.cap > 0 && dist[e.dst] > dist[v] + e.cost + h[v] - h[e.dst]) {
dist[e.dst] = dist[v] + e.cost + h[v] - h[e.dst];
prevv[e.dst] = v;
preve[e.dst] = i;
q.emplace(pcv(dist[e.dst], e.dst));
}
}
}
if(dist[t] == inf) {
return -1;
}
for(int v = 0; v < n; v++) {
h[v] += dist[v];
}
// s-t ????????????????????£??????????????????
int d = f;
for(int v = t; v != s; v = prevv[v]) {
d = min(d, g[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for(int v = t; v != s; v = prevv[v]) {
Edge &e = g[prevv[v]][preve[v]];
e.cap -= d;
g[v][e.revEdge].cap += d;
}
}
return res;
}
// ??????????????????=???????????????-?????¨??????????????¨???
void view() {
for(int i = 0; i < g.size(); i++) {
for(int j = 0; j < g[i].size(); j++) {
if(!g[i][j].isRev) {
Edge& e = g[i][j];
printf("%3d->%3d (flow:%d)\n", i, e.dst, e.cap_orig - e.cap);
}
}
}
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int V, E, F;
cin >> V >> E >> F;
PrimalDual pd(V);
for(int i = 0; i < E; i++) {
int u, v, c, d;
cin >> u >> v >> c >> d;
pd.add_edge(u, v, c, d);
}
int res = pd.solve(0, V - 1, F);
if(res == inf) {
cout << -1 << endl;
}
else {
cout << res << endl;
}
}
|
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
constexpr int INF = 1000000001;
constexpr int MAX = 10000;
typedef pair<int, int> P;
struct Edge {
int to;
int cap;
int cost;
int rev;
Edge(int to, int cap, int cost, int rev)
: to(to), cap(cap), cost(cost), rev(rev) {}
};
int V, E, F;
vector<Edge> G[MAX];
int h[MAX];
int dist[MAX];
int prevv[MAX], preve[MAX];
auto add_edge(int from, int to, int cap, int cost) -> void {
G[from].push_back(Edge(to, cap, cost, G[to].size()));
G[to].push_back(Edge(from, 0, -cost, G[from].size() - 1));
}
auto min_cost_flow(int s, int t, int f) -> int {
auto res = 0;
fill(h, h + V, 0);
while(f > 0) {
priority_queue<P, vector<P>, greater<P>> que;
fill(dist, dist + V, INF);
dist[s] = 0;
que.push(P(0, s));
while(!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (auto i = 0; i < G[v].size(); i++) {
Edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P(dist[e.to], e.to));
}
}
}
if (dist[t] == INF) {
return -1;
}
for (int v = 0; v < V; v++) {
h[v] += dist[v];
}
int d = f;
for (auto v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (auto v = t; v != s; v = prevv[v]) {
Edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
auto main(int argc, char const *argv[]) -> int {
int n;
cin>>V>>E>>F;
while(E--){
int u, v, c, d;
cin>>u>>v>>c>>d;
add_edge(u,v,c,d);
}
cout<<min_cost_flow(0, V - 1, F)<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
using namespace std;
const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};
using edge=struct {int to,cap,cost,rev;};
using G=vector<vector<edge>>;
const int inf=1<<29;
void add_edge(G &graph,int a,int b,int c,int d){
graph[a].push_back({b,c,d,int(graph[b].size())});
graph[b].push_back({a,0,-d,int(graph[a].size()-1)});
}
int min_cost_flow(G &graph,int s,int t,int f){
int res=0;
while(f){
int n=graph.size(),update;
vector<int> dist(n,inf),pv(n,0),pe(n,0);
dist[s]=0;
rep(loop,n){
update=false;
rep(v,n)rep(i,graph[v].size()){
edge &e=graph[v][i];
if(e.cap>0 && chmin(dist[e.to],dist[v]+e.cost)){
pv[e.to]=v,pe[e.to]=i;
update=true;
}
}
if(!update) break;
}
if(dist[t]==inf) return -1;
int d=f;
for(int v=t;v!=s;v=pv[v]) chmin(d,graph[pv[v]][pe[v]].cap);
f-=d,res+=d*dist[t];
for(int v=t;v!=s;v=pv[v]){
edge &e=graph[pv[v]][pe[v]];
e.cap-=d;
graph[v][e.rev].cap+=d;
}
}
return res;
}
int main(void){
int n,m,f;
cin >> n >> m >> f;
G graph(n);
rep(i,m){
int a,b,c,d;
cin >> a >> b >> c >> d;
add_edge(graph,a,b,c,d);
}
cout << min_cost_flow(graph,0,n-1,f) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using FLOW = int;
using COST = int;
const int MAX_V = 100;
const COST INF = 1000000000;
struct Edge {
int rev, from, to;
FLOW cap, icap;
COST cost;
Edge(int r, int f, int t, FLOW cp, COST cs)
: rev(r), from(f), to(t), cap(cp), icap(cp), cost(cs) {}
};
struct Graph {
int V;
vector<Edge> list[MAX_V];
Graph(int n = 0) { init(n); }
void init(int n = 0) {
V = n;
for(auto &&v : list)
v.clear();
}
void resize(int n = 0) { V = n; }
void reset() {
for(auto &&v : list)
for(auto &&e : v)
e.cap = e.icap;
}
inline vector<Edge> &operator[](int i) { return list[i]; }
Edge &redge(Edge &e) { return list[e.to][e.rev + (e.from == e.to)]; }
void addedge(int from, int to, FLOW cap, COST cost) {
list[from].emplace_back(list[to].size(), from, to, cap, cost);
list[to].emplace_back(list[from].size() - 1, to, from, 0, -cost);
}
};
COST MinCostFlow(Graph &G, int s, int t, FLOW f) {
COST dist[MAX_V];
int prevv[MAX_V];
int preve[MAX_V];
COST res = 0;
COST h[MAX_V];
memset(h, 0, sizeof(h));
using P = pair<COST, int>;
priority_queue<P, vector<P>, greater<P>> que;
while(f > 0) {
fill(dist, dist + G.V, INF);
dist[s] = 0;
que.emplace(0, s);
while(!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(int i = 0; i < (int)G[v].size(); i++) {
Edge &e = G[v][i];
if(e.cap > 0 &&
dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.emplace(dist[e.to], e.to);
}
}
}
if(dist[t] == INF) return -1;
for(int v = 0; v < G.V; v++)
h[v] += dist[v];
FLOW d = f;
for(int v = t; v != s; v = prevv[v])
d = min(d, G[prevv[v]][preve[v]].cap);
f -= d;
res += h[t] * d;
for(int v = t; v != s; v = prevv[v]) {
Edge &e = G[prevv[v]][preve[v]];
Edge &re = G.redge(e);
e.cap -= d;
re.cap += d;
}
}
return res;
}
int main() {
int n, m;
FLOW f;
cin >> n >> m >> f;
Graph G(n);
for(int i = 0; i < m; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
G.addedge(a, b, c, d);
}
cout << MinCostFlow(G, 0, n - 1, f) << "\n";
return 0;
}
|
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
struct MinCostFlow{
struct edge{
ll to,cap,cost,rev;
};
const ll MAX_V=200100;
const ll Inf=1e18;
ll V;
vector<vector<edge>>E;
vi dist,h,prevv,preve;
MinCostFlow(ll V):V(V),E(V),dist(V),h(V),prevv(V),preve(V){}
void addEdge(ll from, ll to, ll cap, ll cost){
E[from].pb({to,cap,cost,(ll)E[to].size()});
E[to].pb({from,0,-cost,(ll)E[from].size()-1});
}
void dijkstra(ll s){
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>que;
fill(ALL(dist),Inf);
dist[s] = 0;
que.push(pair<ll,ll>(0,s));
while(!que.empty()){
pair<ll,ll> p = que.top();
que.pop();
ll v = p.second;
if(dist[v] < p.first)continue;
for(int j = 0;j < E[v].size();j++){
edge t = E[v][j];
if(t.cap > 0&&dist[t.to] > dist[v] + t.cost + h[v] - h[t.to]){
dist[t.to] = dist[v] + t.cost + h[v] - h[t.to];
prevv[t.to] = v;preve[t.to] = j;
que.push(pair<ll,ll>(dist[t.to],t.to));
}
}
}
}
ll execution(ll s,ll t,ll f){
ll ret = 0;
fill(ALL(h),0);
while(f>0){
dijkstra(s);
if(dist[t] == Inf)return -1;
for(ll v = 0;v < V;v++)h[v] += dist[v];
ll d = f;
for(ll v = t; v != s; v = prevv[v]){
d = min(d, E[prevv[v]][preve[v]].cap);
}
f -= d;
ret += d * h[t];
for(int v = t; v != s; v = prevv[v]){
edge &e = E[prevv[v]][preve[v]];
e.cap -= d;
E[v][e.rev].cap += d;
}
}
return ret;
}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll v,e,f;cin>>v>>e>>f;
MinCostFlow mcf(v);
for(int i=0;i<e;i++){
ll f,t,c,d;cin>>f>>t>>c>>d;
mcf.addEdge(f,t,c,d);
}
cout<<mcf.execution(0,v-1,f)<<endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1<<30
#define LINF 1LL<<60
#define MAX_V 100000
struct edge{
int to;
int cap;
int cost;
int rev;
edge(){}
edge(int to,int cap,int cost,int rev):to(to),cap(cap),cost(cost),rev(rev){}
};
int V;
vector<edge> G[MAX_V];
int dist[MAX_V];
int prevv[MAX_V],preve[MAX_V];
void add_edge(int from,int to,int cap,int cost){
G[from].push_back(edge(to,cap,cost,(int)G[to].size()));
G[to].push_back(edge(from,0,-cost,(int)G[from].size()-1));
}
int min_cost_flow(int s,int t,int f){
int res=0;
while(f>0){
fill(dist,dist+V,INF);
dist[s]=0;
bool update = true;
while(update){
update = false;
for(int v=0; v<V ;v++){
if(dist[v]==INF) continue;
for(int i=0; i<G[v].size(); i++){
edge &e = G[v][i];
if(e.cap > 0 && dist[e.to] > dist[v]+e.cost) {
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if(dist[t]==INF) return -1;
int d = f;
for(int v=t; v!=s; v=prevv[v])d=min(d,G[prevv[v]][preve[v]].cap);
f -= d;
res += d*dist[t];
for(int v=t; v!=s; v=prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main() {
int E, F; cin >> V >> E >> F;
for (int i = 0; i < E;i++) {
int u, v, c, d; cin >> u >> v >> c >> d;
add_edge(u,v,c,d);
}
cout << min_cost_flow(0,V-1,F) << endl;
return 0;
}
|
#include <vector>
#include <queue>
#include <set>
using namespace std;
struct Graph {
struct edge {
int to;
int cap;
int cost;
int rev;
};
int n;
vector<vector<edge>> edges;
Graph(int N) {
n = N;
edges.resize(n, vector<edge>());
}
int size() const { return n; }
vector<edge> &operator[](int v) { return edges[v]; }
};
int minimumCostFlow(Graph& g,int s,int t,int f)
{
const int n = g.size();
using Pi = pair<int,int>;
priority_queue<Pi,vector<Pi>,greater<Pi>> que;
vector<int> prevv(n,-1);
vector<int> preve(n,-1);
vector<int> potential(n,0);
int res = 0;
while(f > 0){
vector<int> dist(n,1e9);
que.push({0,s});
dist[s] = 0;
while(!que.empty()){
Pi p = que.top();
que.pop();
if(dist[p.second] < p.first) continue;
for(int i = 0;i < g[p.second].size();i++){
auto& e = g[p.second][i];
int next = dist[p.second] + e.cost + potential[p.second] - potential[e.to];
if(e.cap > 0 && dist[e.to] > next){
dist[e.to] = next;
prevv[e.to] = p.second;
preve[e.to] = i;
que.push({dist[e.to],e.to});
}
}
}
if(dist[t] == 1e9) return -1;
for(int v = 0;v < n;v++){
potential[v] += dist[v];
}
int d = f;
for(int v = t;v != s;v = prevv[v]){
d = min(d,g[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * potential[t];
for(int v = t;v != s;v = prevv[v]){
auto& e = g[prevv[v]][preve[v]];
e.cap -= d;
g[v][e.rev].cap += d;
}
}
return res;
}
#include <iostream>
int main()
{
int v;int e;int f;
cin >> v >> e >> f;
Graph g(v);
for(int i = 0;i < e;i++)
{
int u,v,c,d;
cin >> u >> v >> c >> d;
g[u].push_back({v,c,d,(int)g[v].size()});
g[v].push_back({u,0,-d,(int)g[u].size() - 1});
}
int ans = minimumCostFlow(g,0,v - 1,f);
cout << ans << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX_V = 100;
const int INF = 1e9;
struct edge
{
int to,cap,cost,rev;
};
int V;
vector<edge> G[MAX_V];
int dist[MAX_V];
int prevv[MAX_V],preve[MAX_V];
void add_edge(int from,int to, int cap, int cost)
{
G[from].push_back((edge){to,cap,cost,(int)G[to].size()});
G[to].push_back((edge){from,0,-cost,(int)G[from].size()-1});
}
int min_cost_frow(int s,int t,int f)
{
int res = 0;
while(f>0)
{
fill(dist,dist+V,INF);
dist[s] = 0;
bool update = true;
while(update)
{
update = false;
for(int v = 0;v<V;v++)
{
if(dist[v] == INF)continue;
for(int i = 0;i<G[v].size();i++)
{
edge &e = G[v][i];
if(e.cap>0&&dist[e.to]>dist[v] + e.cost)
{
dist[e.to] = dist[v]+e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if(dist[t]==INF)
{
return -1;
}
int d = f;
for(int v = t;v != s;v = prevv[v])
{
d = min(d,G[prevv[v]][preve[v]].cap);
}
f -=d;
res += d*dist[t];
for(int v= t;v !=s;v=prevv[v])
{
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main()
{
int E,F;
cin >> V >> E >> F;
for(int i = 0;i<E;i++)
{
int u,v,c,d;
cin >> u >> v >> c >> d;
add_edge(u,v,c,d);
}
cout<<min_cost_frow(0,V-1,F)<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<P> vp;
typedef vector<ll> vll;
const int INF = 99999999;
const int MAX_V = 100000;
struct edge { int to, cap, cost, rev; };
int V, E, F;
vector<edge> G[MAX_V];
int dist[MAX_V];
int prevv[MAX_V], preve[MAX_V]; // privious vertex, edge
void add_edge(int from, int to, int cap, int cost) {
G[from].emplace_back((edge){to, cap, cost, (int)G[to].size()});
G[to].emplace_back((edge){from, 0, -cost, (int)G[from].size() - 1});
}
// get min cost flow from s to t
// if we can flow not at all, then return -1
int min_cost_flow(int s, int t, int f) {
int res = 0;
while (f > 0) {
fill(dist, dist + V, INF);
dist[s] = 0;
bool update = true;
while (update) {
update = false;
rep(v, V) {
if (dist[v] == INF) continue;
rep(i, G[v].size()) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if (dist[t] == INF) {
// no more flow
return -1;
}
// flow as much as possible along minimum path from s to t
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, (int)G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * dist[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
signed main() {
cin >> V >> E >> F;
rep(i, E) {
int u, v, c, d;
cin >> u >> v >> c >> d;
add_edge(u, v, c, d);
}
cout << min_cost_flow(0, V - 1, F) << endl;
}
|
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
const int INF = 1e9;
struct edge { int to, cap, cost, rev; };
int V;
const int MAX_V = 100;
vector<edge> G[MAX_V];
int dist[MAX_V];
int prevv[MAX_V], preve[MAX_V];
void add_edge(int from, int to, int cap, int cost) {
G[from].push_back((edge){to, cap, cost, (int)G[to].size()});
G[to].push_back((edge){from, 0, -cost, (int)G[from].size() - 1});
}
int min_cost_flow(int s, int t, int f) {
int res = 0;
while (f > 0) {
fill(dist, dist + V, INF);
dist[s] = 0;
bool update = true;
while (update) {
update = false;
for (int v = 0; v < V; v++) {
if (dist[v] == INF) continue;
for (int i = 0; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if (dist[t] == INF) {
return -1;
}
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * dist[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main(void){
cin >> V;
int e, f; cin >> e >> f;
rep(i, e){
int u, v, c, d;
cin >> u >> v >> c >> d;
add_edge(u, v, c, d);
}
printf("%d\n", min_cost_flow(0, V - 1, f));
}
|
#include <math.h>
#include <bits/stdc++.h>//一括で読み込み
using namespace std;
#define MAX_V 500
typedef long long ll;
typedef pair<int,int> P;
struct edge{int to, cap, cost, rev;};
int V;
vector<edge> G[MAX_V];
int h[MAX_V];
int dist[MAX_V];
int prevv[MAX_V],preve[MAX_V];
void add_edge(int from, int to, int cap, int cost)
{
G[from].push_back((edge){to, cap, cost, (int)G[to].size()});
G[to].push_back((edge){from, 0, -cost, (int)G[from].size()-1});
}
int min_cost_flow(int s, int t, int f)
{
int res = 0;
fill(h, h+V, 0);
while(f > 0)
{
priority_queue<P,vector<P>,greater<P>> que;
fill(dist, dist+V, 1<<28);
dist[s] = 0;
que.push(P(0,s));
while(!que.empty())
{
P p = que.top();
que.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(int i = 0; i < G[v].size(); i++)
{
edge &e=G[v][i];
if(e.cap > 0 && dist[e.to] > dist[v]+e.cost+h[v]-h[e.to])
{
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P(dist[e.to], e.to));
}
}
}
if(dist[t] == (1 << 28))
{
return -1;
}
for(int v = 0; v < V; v++) h[v] += dist[v];
int d = f;
for(int v = t; v != s; v = prevv[v])
{
d = min(d,G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d*h[t];
for(int v = t; v != s; v = prevv[v])
{
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main()
{
int e,f,u,v,c,d,i;
cin >> V;//Vは頂点数(500以下)
cin >> e >> f;
for(i = 0; i < e; i++)
{
cin >> u >> v >> c >> d;
add_edge(u,v,c,d);//辺の追加(開始、終了頂点と容量、コスト)
}
cout << min_cost_flow(0, V-1, f) << endl;//最小費用流(開始、終了頂点と流量)
return 0;
}
|
#include <bits/stdc++.h>
#define fread_siz 1024
inline int get_c(void)
{
static char buf[fread_siz];
static char *head = buf + fread_siz;
static char *tail = buf + fread_siz;
if (head == tail)
fread(head = buf, 1, fread_siz, stdin);
return *head++;
}
inline int get_i(void)
{
register int ret = 0;
register int neg = false;
register int bit = get_c();
for (; bit < 48; bit = get_c())
if (bit == '-')neg ^= true;
for (; bit > 47; bit = get_c())
ret = ret * 10 + bit - 48;
return neg ? -ret : ret;
}
template <class T>
inline T min(T a, T b)
{
return a < b ? a : b;
}
const int inf = 2e9;
const int maxn = 2005;
int n, m;
int s, t;
int flow;
int edges;
int hd[maxn];
int nt[maxn];
int to[maxn];
int vl[maxn];
int fl[maxn];
inline void add(int u, int v, int f, int w)
{
nt[edges] = hd[u]; to[edges] = v; fl[edges] = f; vl[edges] = +w; hd[u] = edges++;
nt[edges] = hd[v]; to[edges] = u; fl[edges] = 0; vl[edges] = -w; hd[v] = edges++;
}
int dis[maxn];
int pre[maxn];
inline bool spfa(void)
{
static int que[maxn];
static int inq[maxn];
static int head, tail;
memset(dis, 0x3f, sizeof(dis));
head = 0, tail = 0;
que[tail++] = s;
pre[s] = -1;
inq[s] = 1;
dis[s] = 0;
while (head != tail)
{
int u = que[head++], v; inq[u] = 0;
for (int i = hd[u]; ~i; i = nt[i])
if (dis[v = to[i]] > dis[u] + vl[i] && fl[i])
{
dis[v] = dis[u] + vl[i];
pre[v] = i ^ 1;
if (!inq[v])
inq[v] = 1, que[tail++] = v;
}
}
return dis[t] < 0x3f3f3f3f;
}
inline int expend(void)
{
int newFlow = inf;
for (int i = pre[t]; ~i; i = pre[to[i]])
newFlow = min(newFlow, fl[i ^ 1]);
for (int i = pre[t]; ~i; i = pre[to[i]])
fl[i] += newFlow, fl[i^1] -= newFlow;
return flow -= newFlow, newFlow * dis[t];
}
inline int mcmf(void)
{
int ret = 0;
while (spfa())
ret += expend();
return flow ? -1 : ret;
}
signed main(void)
{
n = get_i();
m = get_i();
flow = get_i();
memset(hd, -1, sizeof(hd));
for (int i = 1; i <= m; ++i)
{
int u = get_i();
int v = get_i();
int f = get_i();
int w = get_i();
add(u, v, f, w);
}
s = n, t = n - 1;
add(s, 0, flow, 0);
printf("%d\n", mcmf());
}
|
//#include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
using namespace std;
#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
//#define int long long
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
typedef long long ll;
//typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
struct edge
{
int s, t, c, d,rev;
edge(int s, int t, int c, int d,int rev) :s(s), t(t), c(c),d(d), rev(rev) {}
edge() { s = -1, t = -1, c = -1, rev = -1; d = -1; }
};
vector<vector<edge>> G;
int dist[101];
int prevv[101], preve[101];
int min_cost_flow(int s, int t, int f)
{
int res = 0;
while (f > 0)
{
rep(i, 0, 101)dist[i] = INF;
dist[s] = 0;
while (true)
{
bool update = false;
rep(i, 0, SZ(G))
{
if (dist[i] == INF)
{
continue;
}
rep(j, 0, SZ(G[i]))
{
edge e = G[i][j];
if (e.c > 0 && dist[e.t] > dist[i] + e.d)
{
dist[e.t] = dist[i] + e.d;
prevv[e.t] = i;
preve[e.t] = j;
update = true;
}
}
}
if (!update)
{
break;
}
}
if (dist[t] == INF)
{
return -1;
}
int tf = f;
for (int i = t; i != s; i = prevv[i])
{
chmin(tf, G[prevv[i]][preve[i]].c);
}
f -= tf;
res += tf*dist[t];
for (int i = t; i != s; i = prevv[i])
{
G[prevv[i]][preve[i]].c -= tf;
G[i][G[prevv[i]][preve[i]].rev].c += tf;
}
}
return res;
}
signed main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int V, E, F,s, t, c, d;
cin >> V >> E >> F;
G.resize(V);
rep(i, 0, E)
{
cin >> s >> t >> c >> d;
G[s].push_back(edge(s, t, c, d,SZ(G[t])));
G[t].push_back(edge(t, s, 0, -d,SZ(G[s]) - 1));
}
cout << min_cost_flow(0,V-1,F) << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;
typedef pair <int, int> P;
class PrimalDual {
const int INF = 1 << 30;
struct Edge {
int to, cap, cost, rev;
};
int n;
vector<vector<Edge>> G;
vector<int> potential; // ポテンシャル
vector<int> dist; // 最短距離
vector<int> prevv, preve; // 直前の頂点と辺
void dijkstra(int s) {
fill(dist.begin(), dist.end(), INF);
priority_queue<P, vector<P>, greater<P>> q;
dist[s] = 0;
q.push(P(0, s));
while (!q.empty()) {
P p = q.top();
q.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (int i = 0; i < G[v].size(); i++) {
Edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] + potential[e.to] > dist[v] + e.cost + potential[v]) {
dist[e.to] = dist[v] + e.cost + potential[v] - potential[e.to];
prevv[e.to] = v;
preve[e.to] = i;
q.push(P(dist[e.to], e.to));
}
}
}
}
public:
PrimalDual(int n) : n(n), G(n), potential(n), dist(n), prevv(n), preve(n) {}
void add_edge(int from, int to, int cap, int cost) {
G[from].push_back({to, cap, cost, (int)G[to].size()});
G[to].push_back({from, 0, -cost, (int)G[from].size() - 1});
}
int min_cost_flow(int s, int t, int flow) {
fill(potential.begin(), potential.end(), 0);
int ret = 0;
while (flow) {
dijkstra(s);
if (dist[t] == INF) return -1;
for (int v = 0; v < n; v++) potential[v] += dist[v];
int f = flow;
for (int v = t; v != s; v = prevv[v]) f = min(f, G[prevv[v]][preve[v]].cap);
flow -= f;
ret += f * potential[t];
for (int v = t; v != s; v = prevv[v]) {
Edge &e = G[prevv[v]][preve[v]];
e.cap -= f;
G[v][e.rev].cap += f;
}
}
return ret;
}
};
int main() {
int V, E, F;
cin >> V >> E >> F;
PrimalDual pd(V);
for (int e = 0; e < E; e++) {
int u, v, c, d;
cin >> u >> v >> c >> d;
pd.add_edge(u, v, c, d);
}
cout << pd.min_cost_flow(0, V - 1, F) << "\n";
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define INF (1e9+1)
//#define INF (1LL<<59)
#define MAX_V 100
struct edge { int to, cap, cost,rev; };
int V; // ????????°
vector<edge> G[MAX_V];
int h[MAX_V]; //??????????????£???
int dist[MAX_V];// ???????????¢
int prevv[MAX_V], preve[MAX_V];// ??´??????????????¨???
// from??????to??????????????????cap????????????cost????????????????????????????????????
void add_edge(int from, int to, int cap, int cost) {
G[from].push_back((edge){to, cap, cost, (int)G[to].size()});
G[to].push_back((edge){from, 0, -cost, (int)G[from].size() - 1});
}
// s??????t????????????f???????°??????¨???????±???????
// ??????????????´??????-1?????????
int min_cost_flow(int s, int t, int f) {
int res = 0;
fill(h, h + V, 0); // h????????????
while (f > 0) {
// ?????????????????????????????¨??????h?????´??°??????
priority_queue<pii, vector<pii>, greater<pii> > que; fill(dist, dist + V, INF);
dist[s] = 0;
que.push(pii(0, s));
while (!que.empty()) {
pii p = que.top(); que.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (int i = 0; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v;
preve[e.to] = i;
que.push(pii(dist[e.to], e.to));
}
}
}
if (dist[t] == INF) return -1; //????????\???????????????
for (int v = 0; v < V; v++) h[v] += dist[v];
// s-t????????????????????£??????????????????
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]]; e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main(){
int e,f;
cin>>V>>e>>f;
rep(i,e){
int u,v,c,d;
cin>>u>>v>>c>>d;
add_edge(u, v, c, d);
}
cout<<min_cost_flow(0, V-1, f)<<endl;
}
|
#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <utility>
#include <complex>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <tuple>
#include <bitset>
#include <algorithm>
#include <random>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef complex<ld> compd;
#define rep(i,n) for(int i=0;i<n;i++)
#define srep(i,a,n) for(int i=a;i<n;i++)
#define REP(i,n) for(int i=0;i<=n;i++)
#define SREP(i,a,n) for(int i=a;i<=n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define RREP(i,n) for(int i=n;i>=0;i--)
#define all(a) (a).begin(),(a).end()
#define mp(a,b) make_pair(a,b)
#define mt make_tuple
#define pb push_back
#define fst first
#define scn second
#define bicnt(x) __buildin__popcount(x)
#define gcd(a,b) __gcd__(a,b)
#define debug(x) cout<<"debug: "<<x<<endl
#define DEBUG 0
const ll inf = (ll)1e9;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;
const int dx[] = { 0,1,0,-1 };
const int dy[] = { 1,0,-1,0 };
struct edge {
int to, cap, cost, rev;
edge(int to_, int cap_, int cost_, int rev_) {
to = to_;
cap = cap_;
cost = cost_;
rev = rev_;
}
};
int n;
vector<edge> e[1010];
int h[1010];
int dist[1010];
int prevv[1010], preve[1010];
void add_edge(int s, int t, int cap, int cost) {
e[s].push_back(edge(t, cap, cost, e[t].size()));
e[t].push_back(edge(s, 0, -cost, e[s].size() - 1));
}
int min_cost_flow(int s, int t, int f) {
int ret = 0;
REP(i, n) h[i] = 0;
while (f > 0) {
priority_queue<pii> pq;
pq.push(mp(0, -s));
REP(i, n) dist[i] = inf;
dist[s] = 0;
while (!pq.empty()) {
auto it = pq.top(); pq.pop();
int v = -it.second, cost = -it.first;
if (dist[v] < cost) continue;
rep(i, e[v].size()) {
edge* to = &e[v][i];
if (to->cap > 0 && dist[to->to] > dist[v] + to->cost + h[v] - h[to->to]) {
dist[to->to] = dist[v] + to->cost + h[v] - h[to->to];
prevv[to->to] = v;
preve[to->to] = i;
pq.push(mp(-dist[to->to], -to->to));
}
}
}
if (dist[t] == inf) return -1;
rep(i, n) h[i] += dist[i];
int d = f;
for (int i = t; i != s; i = prevv[i]) {
d = min(d, e[prevv[i]][preve[i]].cap);
}
f -= d;
ret += d*h[t];
for (int i = t; i != s; i = prevv[i]) {
edge* to = &e[prevv[i]][preve[i]];
to->cap -= d;
e[i][to->rev].cap += d;
}
}
return ret;
}
int main() {
int e, f; cin >> n >> e >> f;
rep(i, e) {
int s, t, c, d; cin >> s >> t >> c >> d;
add_edge(s, t, c, d);
}
cout << min_cost_flow(0, n - 1, f) << endl;
return 0;
}
|
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
using Weight=long long;
static const Weight INF=1LL<<57;
template <class T>
using gp_queue=priority_queue<T, deque<T>, less<T>>;
struct Arc {
// accessible to the reverse edge
size_t src, dst;
Weight capacity, cost, flow;
size_t rev;
Arc() {}
Arc(size_t src, size_t dst, Weight capacity, Weight cost=0):
src(src), dst(dst), capacity(capacity), cost(cost), flow(0), rev(-1)
{}
Weight residue() const {
return capacity - flow;
}
};
bool operator<(const Arc &e, const Arc &f) {
if (e.cost != f.cost) {
return e.cost > f.cost;
} else if (e.capacity != f.capacity) {
return e.capacity > f.capacity;
} else {
return e.src!=f.src? e.src<f.src : e.dst<f.dst;
}
}
using Arcs=vector<Arc>;
using Node=vector<Arc>;
using FlowNetwork=vector<Node>;
void connect(FlowNetwork &g, size_t s, size_t d, Weight c=1, Weight k=0) {
g[s].push_back(Arc(s, d, c, k));
g[d].push_back(Arc(d, s, 0, -k));
g[s].back().rev = g[d].size()-1;
g[d].back().rev = g[s].size()-1;
}
void join(FlowNetwork &g, size_t s, size_t d, Weight c=1, Weight k=0) {
g[s].push_back(Arc(s, d, c, k));
g[d].push_back(Arc(d, s, c, k));
g[s].back().rev = g[d].size()-1;
g[d].back().rev = g[s].size()-1;
}
pair<Weight, Weight> mincost_flow(
FlowNetwork &g, size_t s, size_t t, Weight F=INF
) {
size_t V=g.size();
vector<Arc *> prev(V, NULL);
pair<Weight, Weight> total; // <cost, flow>
while (F > total.second) {
vector<Weight> d(V, INF); d[s]=0;
for (size_t i=0; i<V; ++i) {
for (size_t v=0; v<V; ++v) {
if (d[v] == INF) continue;
for (Arc &e: g[v]) {
if (e.capacity <= 0) continue;
if (d[e.dst] > d[e.src] + e.cost) {
d[e.dst] = d[e.src] + e.cost;
prev[e.dst] = &e;
}
}
}
}
if (d[t] == INF) {
total.first = -1;
return total;
}
Weight f=F-total.second;
for (Arc *e=prev[t]; e!=NULL; e=prev[e->src])
f = min(f, e->capacity);
total.second += f;
total.first += f * d[t];
for (Arc *e=prev[t]; e!=NULL; e=prev[e->src]) {
e->capacity -= f;
g[e->dst][e->rev].capacity += f;
}
}
return total;
}
int main() {
size_t V, E;
Weight F;
scanf("%zu %zu %lld", &V, &E, &F);
FlowNetwork g(V);
for (size_t i=0; i<E; ++i) {
size_t u, v;
Weight c, d;
scanf("%zu %zu %lld %lld", &u, &v, &c, &d);
connect(g, u, v, c, d);
}
printf("%lld\n", mincost_flow(g, 0, V-1, F).first);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
constexpr int IINF = INT_MAX;
struct edge{
int to, cap, cost, rev;
};
int V, E, F;
vector<vector<edge> > G;
vector<int> h, dist, prevv, preve;
void add_edge(int from, int to, int cap, int cost){
G[from].push_back((edge){to, cap, cost, int(G[to].size())});
G[to].push_back((edge){from, 0, -cost, int(G[from].size())-1});
}
// 最小費用流
int min_cost_flow(int s, int t, int f){
h.resize(G.size());
dist.resize(G.size());
prevv.resize(G.size());
preve.resize(G.size());
int res = 0;
fill(h.begin(), h.end(), 0);
while(f > 0){
priority_queue<P, vector<P>, greater<P> > que;
fill(dist.begin(), dist.end(), IINF);
dist[s] = 0;
que.push({0,s});
while(!que.empty()){
P p = que.top();
que.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(int i=0;i<int(G[v].size());i++){
auto &e = G[v][i];
if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push({dist[e.to], e.to});
}
}
}
if(dist[t] == IINF){
return -1;
}
for(int v=0; v<G.size(); v++) h[v] += dist[v];
int d = f;
for(int v=t; v!=s; v=prevv[v]){
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d*h[t];
for(int v=t; v!=s; v=prevv[v]){
auto &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main() {
cin >> V >> E >> F;
G.resize(V);
for(int i=0;i<E;i++){
int u, v, c, d;
cin >> u >> v >> c >> d;
add_edge(u, v, c, d);
}
cout << min_cost_flow(0, V-1, F) << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=105;
struct Edge {
int from,to,cap,flow,cost;
Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w) {}
};
int n,m,tf;
vector<Edge>edges;
vector<int>G[maxn];
bool inq[maxn];
int d[maxn];
int a[maxn];
int p[maxn];
inline void addedge (int u,int v,int c,int w) {
edges.pb(Edge(u,v,c,0,w));
edges.pb(Edge(v,u,0,0,-w));
int id=edges.size()-2;
G[u].pb(id);
G[v].pb(id+1);
}
inline bool spfa (int s,int t,int& flow,int& cost) {
for (int i=0;i<n;i++) d[i]=INF;
d[s]=0; inq[s]=1; a[s]=max(0,tf-flow);
queue<int>q;
q.push(s);
while (q.size()) {
int u=q.front(); q.pop();
inq[u]=0;
for (int id:G[u]) {
Edge& e=edges[id];
if (e.cap>e.flow && d[e.to]>d[u]+e.cost) {
d[e.to]=d[u]+e.cost;
p[e.to]=id;
a[e.to]=min(a[u],e.cap-e.flow);
if (!inq[e.to]) {inq[e.to]=1;q.push(e.to);}
}
}
}
if (d[t]==INF) return 0;
flow+=a[t];
cost+=d[t]*a[t];
// cout<<flow<<' '<<cost<<'\n';
for (int u=t;u!=s;u=edges[p[u]].from) {
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
}
return 1;
}
inline int mcmf (int s,int t) {
int flow=0 , cost=0;
while (spfa(s,t,flow,cost) && flow<tf);
if (flow<tf) return -1;
else return cost;
}
int main() {
cin>>n>>m>>tf;
for (int i=0;i<m;i++) {
int u,v,c,w; cin>>u>>v>>c>>w;
addedge(u,v,c,w);
}
cout<<mcmf(0,n-1)<<'\n';
}
|
#include <bits/stdc++.h>
using namespace std;
struct PrimalDual{
const int INF = 1<<28;
typedef pair<int,int> P;
struct edge{
int to,cap,cost,rev;
edge(){}
edge(int to,int cap,int cost,int rev):to(to),cap(cap),cost(cost),rev(rev){}
};
int n;
vector<vector<edge> > G;
vector<int> h,dist,prevv,preve;
PrimalDual(){}
PrimalDual(int sz):n(sz),G(sz),h(sz),dist(sz),prevv(sz),preve(sz){}
void add_edge(int from,int to,int cap,int cost){
G[from].push_back(edge(to,cap,cost,G[to].size()));
G[to].push_back(edge(from,0,-cost,G[from].size()-1));
}
int flow(int s,int t,int f){
int res=0;
fill(h.begin(),h.end(),0);
while(f>0){
priority_queue<P,vector<P>,greater<P> > que;
fill(dist.begin(),dist.end(),INF);
dist[s]=0;
que.push(P(0,s));
while(!que.empty()){
P p=que.top();que.pop();
int v=p.second;
if(dist[v]<p.first) continue;
for(int i=0;i<(int)G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
prevv[e.to]=v;
preve[e.to]=i;
que.push(P(dist[e.to],e.to));
}
}
}
if(dist[t]==INF) return -1;
for(int v=0;v<n;v++) h[v]+=dist[v];
int d=f;
for(int v=t;v!=s;v=prevv[v]){
d=min(d,G[prevv[v]][preve[v]].cap);
}
f-=d;
res+=d*h[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e=G[prevv[v]][preve[v]];
e.cap-=d;
G[v][e.rev].cap+=d;
}
}
return res;
}
};
int main(){
int v,e,f;
cin>>v>>e>>f;
PrimalDual pd(v);
for(int i=0;i<e;i++){
int u,v,c,d;
cin>>u>>v>>c>>d;
pd.add_edge(u,v,c,d);
}
cout<<pd.flow(0,v-1,f)<<endl;
return 0;
}
|
#include<iostream>
using namespace std;
//Minimum Cost Flow O(FE log V)
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<limits>
template<typename T>
struct MCF{
vector<vector<pair<pair<int,int>,pair<int,T> > > >G;
vector<T>h,d;
vector<int>pv,pe;
MCF(int n_=0):G(n_),h(n_,0),d(n_),pv(n_),pe(n_){}
void add_edge(int from,int to,int cap,T cost)
{
G[from].push_back(make_pair(
make_pair(to,G[to].size()),make_pair(cap,cost)
));
G[to].push_back(make_pair(
make_pair(from,G[from].size()-1),make_pair(0,-cost)
));
}
T min_cost_flow(int s,int t,int f)//ans or -1
{
T ret=0;
while(f>0)
{
priority_queue<pair<T,int>,vector<pair<T,int> >,greater<pair<T,int> > >P;
fill(d.begin(),d.end(),numeric_limits<T>::max());
d[s]=0;
P.push(make_pair(0,s));
while(!P.empty())
{
pair<T,int>p=P.top();P.pop();
if(d[p.second]<p.first)continue;
for(int i=0;i<G[p.second].size();i++)
{
pair<pair<int,int>,pair<int,T> >&e=G[p.second][i];
if(e.second.first>0&&
d[e.first.first]>d[p.second]+e.second.second+h[p.second]-h[e.first.first])
{
d[e.first.first]=d[p.second]+e.second.second+h[p.second]-h[e.first.first];
pv[e.first.first]=p.second;
pe[e.first.first]=i;
P.push(make_pair(d[e.first.first],e.first.first));
}
}
}
if(d[t]==numeric_limits<T>::max())return -1;
for(int u=0;u<G.size();u++)h[u]+=d[u];
int d=f;
for(int u=t;u!=s;u=pv[u])d=min(d,G[pv[u]][pe[u]].second.first);
f-=d;
ret+=d*h[t];
for(int u=t;u!=s;u=pv[u])
{
G[pv[u]][pe[u]].second.first-=d;
G[u][G[pv[u]][pe[u]].first.second].second.first+=d;
}
}
return ret;
}
};
int main()
{
int N,E,F;
cin>>N>>E>>F;
MCF<int>mf(N);
for(int i=0;i<E;i++)
{
int u,v,c,d;cin>>u>>v>>c>>d;
mf.add_edge(u,v,c,d);
}
cout<<mf.min_cost_flow(0,N-1,F)<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
//O(F*V^2)
class MinimumCostFlow {
private:
struct edge {
int to, cap, cost, rev;
};
const int INF = 1e18;
int V;
vector<vector<edge>> G;
vector<int> h, dist;
vector<int> prevv, preve;
void add_edge(int from, int to, int cap, int cost) {
G[from].push_back({to, cap, cost, (int)G[to].size()});
G[to].push_back({from, 0LL, -cost, (int)G[from].size() - 1LL});
}
int min_cost_flow(int s, int t, int f) {
int res = 0;
fill(h.begin(), h.end(), 0LL);
while (f > 0) {
using P = pair<int, int>;
priority_queue<P, vector<P>, greater<P>> que;
fill(dist.begin(), dist.end(), INF);
dist[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (int i = 0; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P{dist[e.to], e.to});
}
}
}
if (dist[t] == INF) {
return -1;
}
for (int v = 0; v < V; v++) {
h[v] += dist[v];
}
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
public:
MinimumCostFlow(int N) : V(N), G(N, vector<edge>()), h(N), dist(N), prevv(N), preve(N) {}
void addEdge(int from, int to, int cap, int cost) {
return add_edge(from, to, cap, cost);
}
int minCostFlow(int s, int t, int f) {
return min_cost_flow(s, t, f);
}
};
signed main() {
int V, E, F;
cin >> V >> E >> F;
MinimumCostFlow mcf(V);
for (int i = 0; i < E; i++) {
int u, v, c, d;
cin >> u >> v >> c >> d;
mcf.addEdge(u, v, c, d);
}
cout << mcf.minCostFlow(0, V - 1, F) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
// ????°??????¨??? O(F |E| |V|)
struct MinCostFlow
{
typedef pair<int, int> P; // first -> minDist, second -> v
struct Edge
{
int to, cap, cost, rev;
};
const int V;
vector<vector<Edge>> G;
vector<int> dist, prevv, preve;
MinCostFlow(int v) : V(v), G(v), dist(v), prevv(v), preve(v){}
void add_edge(int from, int to, int cap, int cost) {
G[from].push_back({to, cap, cost, (int)G[to].size()});
G[to].push_back({from, 0, -cost, (int)G[from].size()-1});
}
// minCostFlow s -> t
// noexist flow return -1
int min_cost_flow(int s, int t, int f) {
int res = 0;
while(f > 0) {
for(int i = 0; i < V; ++i) dist[i] = INF;
dist[s] = 0;
bool update = true;
while(update) {
update = false;
for(int v = 0; v < V; ++v) {
if(dist[v] == INF) continue;
for(int i = 0; i < G[v].size(); ++i) {
Edge& e = G[v][i];
if(e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if(dist[t] == INF) return -1;
int d = f;
for(int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * dist[t];
for(int v = t; v != s; v = prevv[v]) {
Edge& e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
};
int main(){
int V, E, F;
cin >> V >> E >> F;
MinCostFlow f(V);
rep(i, E) {
int a, b, c, d;
cin >> a >> b >> c >> d;
f.add_edge(a, b, c, d);
}
cout << f.min_cost_flow(0, V-1, F) << endl;
return 0;
}
|
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B&lang=jp
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int>P;
class MCF{
public:
class edge{
public:
int to, cap, cost, rev;
edge(){};
edge(int _to, int _cap, int _cost, int _rev){
to = _to;
cap = _cap;
cost = _cost;
rev = _rev;
}
};
vector<vector<edge> >G;
vector<int>h;
vector<int>dist;
vector<int>prevv;
vector<int>preve;
MCF(int n){
G.resize(n);
preve.resize(n, 0);
prevv.resize(n, 0);
}
void addEdge(int from, int to, int cap, int cost){
G[from].push_back(edge(to, cap, cost, G[to].size()));
G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
}
int flow(int s, int t, int f){
int res = 0;
h.clear();
h.resize(G.size(), 0);
while (f > 0){
priority_queue<P, vector<P>, greater<P> >que;
dist.clear();
dist.resize(G.size(), 1145141919);
dist[s] = 0;
que.push(P(0, s));
while (!que.empty()){
P p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first)continue;
for (int i = 0; i < G[v].size(); i++){
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P(dist[e.to], e.to));
}
}
}
if (dist[t] == 1145141919){
return -1;
}
for (int v = 0; v < G.size(); v++)h[v] += dist[v];
int d = f;
for (int v = t; v != s; v = prevv[v]){
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d*h[t];
for (int v = t; v != s; v = prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
};
int main(){
int V, E, F;
cin >> V >> E >> F;
MCF mcf(V);
for (int i = 0; i < E; i++){
int from, to, cap, cost;
cin >> from >> to >> cap >> cost;
mcf.addEdge(from, to, cap, cost);
}
cout << mcf.flow(0, V - 1, F) << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))
using namespace std;
template<class T>bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0;}
template<class T>bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0;}
using ll = long long;
using R = long double;
const R EPS = 1e-9; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r) {return (r > EPS) - (r < -EPS);}
inline R sq(R x) {return sqrt(max<R>(x, 0.0));}
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// Problem Specific Parameter:
//Appropriately Changed
using W = ll;
using edge = struct {int to, rev; W cap, flow, cost;};
using G = vector<vector<edge>>;
//Appropriately Changed
void add_edge(G &graph, int from, int to, W cap, W cost) {
graph[from].push_back({to, int(graph[to].size()) , cap , 0 , cost});
graph[to].push_back({from, int(graph[from].size()) - 1, 0 , 0, -cost});
}
// Description: ??°??????????????????????°??????¨???
// TimeComplexity: $ \mathcal{O}(FEV) $
// Verifyed: AOJ GRL_6_B
W primal_dual(G &graph, int s, int t, int f) {
const W inf = 1LL << 50;
W res = 0;
while (f) {
int n = graph.size(), update;
vector<W> dist(n, inf);
vector<int> pv(n, 0), pe(n, 0);
dist[s] = 0;
rep(loop, n) {
update = false;
rep(v, n)rep(i, graph[v].size()) {
edge &e = graph[v][i];
if (e.cap > e.flow and chmin(dist[e.to], dist[v] + e.cost)) {
pv[e.to] = v, pe[e.to] = i;
update = true;
}
}
if (!update) break;
}
if (dist[t] == inf) return -1;
W d = f;
for (int v = t; v != s; v = pv[v]) chmin(d, graph[pv[v]][pe[v]].cap - graph[pv[v]][pe[v]].flow);
f -= d, res += d * dist[t];
for (int v = t; v != s; v = pv[v]) {
edge &e = graph[pv[v]][pe[v]];
e.flow += d;
graph[v][e.rev].flow -= d;
}
}
return res;
}
int main(void) {
int v, e, f;
cin >> v >> e >> f;
G graph(v);
rep(i, e) {
int a, b, c, d;
cin >> a >> b >> c >> d;
add_edge(graph, a, b, c, d);
}
cout << primal_dual(graph, 0, v - 1, f) << endl;
return 0;
}
|
#include<bits/stdc++.h>
#define fi first
#define se second
typedef long long ll;
using namespace std;
//BEGIN CUT HERE
//Min_cost_flow
class Min_cost_flow{
public:
explicit Min_cost_flow(int n):vertex(static_cast<unsigned int>(n+10)){
G.resize(vertex);
prev_e.resize(vertex);
prev_v.resize(vertex);
}
void add_edge(int from,int to,ll cap,ll cost){
G[from].push_back((edge){to, cap, cost, static_cast<int>(G[to].size())});
G[to].push_back((edge){from, 0, -cost, static_cast<int>(G[from].size() - 1)});
}
ll flow(int s,int t,ll f){
ll res=0;
h.clear();
h.resize(vertex,0);
while(f>0){
vector<ll> dist;
dist.resize(vertex);
dist=dijkstra(s);
if(dist[t]==INF)return -1;
for(int i=0;i<vertex;i++)h[i]+=dist[i];
ll d=f;
for(int i=t;i!=s;i=prev_v[i]){
d=min(d,G[prev_v[i]][prev_e[i]].cap);
}
f-=d;
res+=d*h[t];
for(int i=t;i!=s;i=prev_v[i]){
edge &e=G[prev_v[i]][prev_e[i]];
e.cap-=d;
G[i][e.rev].cap+=d;
}
}
return res;
}
private:
struct edge{
int to;
ll cap;
ll cost;
int rev;
};
unsigned int vertex;
ll INF=(ll)1e16;
vector<vector<edge> > G;
vector<ll> h;
vector<int> prev_v,prev_e;
vector<ll> dijkstra(int start){
vector<ll> dist(vertex,INF);
dist[start]=0;
priority_queue<pair<ll,int> > q;
q.push({0,start});
while(!q.empty()){
int now=q.top().se;
ll now_cost=-q.top().fi;
q.pop();
if(now_cost>dist[now])continue;
for(int i=0;i<(int)G[now].size();i++){
edge &e=G[now][i];
if(e.cap>0 && dist[e.to]>dist[now]+e.cost+h[now]-h[e.to]){
dist[e.to]=dist[now]+e.cost+h[now]-h[e.to];
prev_v[e.to]=now;
prev_e[e.to]=i;
q.push({-dist[e.to],e.to});
}
}
}
return dist;
}
};
int main(){
int v,e,f;cin>>v>>e>>f;
Min_cost_flow m(v);
for(int i=0;i<e;i++){
int from,to,cap,cost;
cin>>from>>to>>cap>>cost;
m.add_edge(from,to,cap,cost);
}
cout<<m.flow(0,v-1,f)<<endl;
}
|
#include <bits/stdc++.h>
static const int MAX=100;
static const int INFTY=(1<<30);
class edge{
public:
int target, cap, cost, rev;
edge(int target = 0, int cap =0, int cost = 0, int rev =0) : target(target), cap(cap), cost(cost), rev(rev) {}
};
int V;
std::vector<edge> G[MAX];
int h[MAX], dist[MAX], prevv[MAX], preve[MAX];
void add_edge(const int &source, const int &target, const int &cap, const int &cost) {
G[source].push_back(edge(target, cap, cost, G[target].size()));
G[target].push_back(edge(source, 0, -cost, G[source].size()-1));
}
int min_cost_flow(int s, int t, int f) {
int res = 0;
std::fill(h, h+V, 0);
while(f > 0) {
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int> >, std::greater<std::pair<int, int> > > pq;
std::fill(dist, dist + V, INFTY);
dist[s] = 0;
pq.push({0, s});
while (!pq.empty()) {
std::pair<int, int> p = pq.top(); pq.pop();
int current = p.second;
if (dist[current] < p.first) continue;
for (int i = 0; i < G[current].size(); i++) {
edge &e = G[current][i];
if (e.cap > 0 && dist[e.target] > dist[current] + e.cost + h[current] - h[e.target]) {
dist[e.target] = dist[current] + e.cost + h[current] - h[e.target];
prevv[e.target] = current; preve[e.target] = i;
pq.push({dist[e.target], e.target});
}
}
}
if (dist[t] == INFTY) return -1;
for (int v = 0; v < V; v++) h[v] += dist[v];
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = std::min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v!=s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main()
{
int E, f, source, target, cap, cost;
std::scanf("%d %d %d", &V, &E, &f);
for (int i = 0; i < E; i++) {
std::scanf("%d %d %d %d", &source, &target, &cap, &cost);
add_edge(source, target, cap, cost);
}
std::printf("%d\n", min_cost_flow(0, V-1, f));
return 0;
}
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
#include <deque>
#include <stdio.h>
using namespace std;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
typedef long long ll;
template<class T> struct MinCostFlow {
struct Edge {
int to, rev;
T cap, cost;
Edge () {}
Edge (int _to, T _cap, T _cost, int _rev) :
to(_to), cap(_cap), cost(_cost), rev(_rev) {}
};
const T INF = numeric_limits<T>::max() / 2;
int N;
vector< vector<Edge> > G;
vector<T> h;
vector<T> dist;
vector<int> prevv, preve;
MinCostFlow (int n) : N(n), G(n), h(n), dist(n), prevv(n), preve(n) {}
void add_edge(int from, int to, T cap, T cost) {
G[from].push_back(Edge(to,cap,cost,G[to].size()));
G[to].push_back(Edge(from,0,-cost,G[from].size()-1));
}
T get_min(int s, int t, T f) {
T ret = 0;
for (int i=0; i<N; i++) h[i] = 0;
while (f > 0) {
priority_queue< pair<T,int>, vector< pair<T,int> >, greater< pair<T,int> > > que;
for (int i=0; i<N; i++) dist[i] = INF;
dist[s] = 0;
que.push(make_pair(0,s));
while (que.size() != 0) {
pair<int,int> p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (int i=0; i<G[v].size(); i++) {
Edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(make_pair(dist[e.to], e.to));
}
}
}
if (dist[t] == INF) {
return -1;
}
for (int v=0; v<N; v++) h[v] += dist[v];
int d = f;
for (int v=t; v!=s; v=prevv[v]) {
d = min(d,G[prevv[v]][preve[v]].cap);
}
f -= d;
ret += d * h[t];
for (int v=t; v!=s; v=prevv[v]) {
Edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return ret;
}
};
int main() {
int V,E,F;
cin >> V >> E >> F;
MinCostFlow<int> inst(110);
REP(i,E) {
int a,b,c,d;
cin >> a >> b >> c >> d;
inst.add_edge(a,b,c,d);
}
cout << inst.get_min(0,V-1,F) << endl;
return 0;
}
|
#include <cassert>
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <tuple>
#include <limits>
using namespace std;
struct MinCostFlowGraph {
private:
using ll = long long;
struct edge {
int to;
ll cap, cost;
int r_idx;
edge(int t, ll cap, ll cost, int r) :
to(t), cap(cap), cost(cost), r_idx(r) {}
};
vector<vector<edge>> G;
int sz;
public:
MinCostFlowGraph(int n) : G(n), sz(n) {}
void add_edge(int from, int to, ll cap, ll cost){
int i = G[to].size(), i_ = G[from].size();
G[from].emplace_back(to,cap,cost,i);
G[to].emplace_back(from,0,-cost,i_);
}
ll min_cost_flow(int from, int to, ll f){
ll ans = 0;
while(f > 0){
const ll INF = numeric_limits<ll>::max();
vector<ll> dist(sz,INF);
dist[from] = 0;
vector<ll> potential(sz);
vector<int> prev_v(sz,-1);
vector<int> prev_e(sz,-1);
priority_queue<pair<ll,int>> pq;
pq.emplace(0,from);
while(pq.size()){
// auto [d, v] = pq.top();
ll d, v;
tie(d,v) = pq.top();
d *= -1;
pq.pop();
if(dist[v] < d) continue;
for(size_t i = 0; i < G[v].size(); ++i){
const auto& e = G[v][i];
if(e.cap == 0) continue;
int v_ = e.to;
ll d_ = d + e.cost + potential[v] - potential[v_];
if(dist[v_] <= d_)
continue;
dist[v_] = d_;
prev_v[v_] = v;
prev_e[v_] = i;
pq.emplace(-d_,e.to);
}
}
if(dist[to] >= INF) return -1;
for(int i = 0; i < sz; ++i)
potential[i] += dist[i];
int v = to;
ll flow = f;
while(v != from){
int v_ = prev_v[v];
flow = min(flow,G[v_][prev_e[v]].cap);
v = v_;
}
v = to;
while(v != from){
int v_ = prev_v[v];
auto& e = G[v_][prev_e[v]];
e.cap -= flow;
ans += flow*e.cost;
G[v][e.r_idx].cap += flow;
v = v_;
}
f -= flow;
}
return ans;
}
};
int main(){
int n, m, f;
cin >> n >> m >> f;
MinCostFlowGraph G(n);
for(int i = 0; i < m; ++i){
long long u, v, c, d;
cin >> u >> v >> c >> d;
G.add_edge(u,v,c,d);
}
cout << G.min_cost_flow(0,n-1,f) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
template<typename T, T INF>
class MinCostFlow{
public:
//辺を表す構造体(行き先、容量、コスト、逆辺)
struct edge{
int to,cap;
T cost;
int rev;
edge();
edge(int to,int cap,T cost,int rev):to(to),cap(cap),cost(cost),rev(rev){}
};
int V; //頂点数
vector<vector<edge> > G; //グラフの隣接リスト表現
vector<T> dist; //最短距離
vector<int> prevv,preve; //直前の頂点と辺
MinCostFlow():V(-1){}
MinCostFlow(int V):V(V), G(V), dist(V), prevv(V), preve(V){};
// fromからtoへ向かう容量cap、コストcostの辺をグラフに追加する。
void add_edge(int from,int to,int cap ,T cost){
assert(from>=0 && to >= 0);
assert(from<V && to<V);
G[from].push_back((edge){to,cap,cost,(int)G[to].size()});
G[to].push_back((edge){from,0,-cost,(int)G[from].size()-1});
}
//sからtへの流量fの最小費用流を求める
//流せない場合-1を返す
T flow(int s, int t, int f){
T res = 0;
while(f>0){
//ベルマンフォード法により,s-t間最短路を求める
fill(dist.begin(),dist.end(),INF);
dist[s] = 0;
bool update = true;
while(update){
update = false;
for(int v=0; v<V ;v++){
if(dist[v]==INF) continue;
for(int i=0; i<(int)G[v].size(); i++){
edge &e = G[v][i];
if(e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
if(abs(dist[e.to] - dist[v] - e.cost) < 1e-8) continue; //double演算用
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if(dist[t]==INF) return -1; //これ以上流せない。
//s−t間短路に沿って目一杯流す
int d = f;
for(int v=t; v!=s; v=prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap);
f -= d;
res += d * dist[t];
for(int v=t; v!=s; v=prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
};
int main(){
int V, E, F;
cin>>V>>E>>F;
MinCostFlow<long long, 1LL<<55> f(V);
for(int i=0;i<E;i++){
int u, v, c, d;
cin>>u>>v>>c>>d;
f.add_edge(u, v, c, d);
}
int ans = f.flow(0, V-1, F);
cout<<ans<<endl;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.