给定一张有向图,每条边都有一个容量C和一个扩容费用W。这里扩容费用是指将容量扩大1所需的费用。求:
第一问最大流。
第二问,在原来最大流参量网络基础上,考虑如何加边求出结果。因为要求在原来的基础上增加流量K,新建源点S,S连1,容量为K,费用为0,这样保证了新增的流量为K。在原来的边的基础上,增加边,容量为INF,费用为W。这样跑从S到N的最小费用最大流,费用即为结果。
//
// Created by pengwill on 2018/9/26.
//
#include
using namespace std;
typedef int valtype;
const int nmax = 50007;
const int INF = 0x3f3f3f3f;
struct ee {
int u, v, c, w;
}add[nmax];
vector<ee> nv;
struct MCMF{
valtype final_flow,final_cost;
int tot,S,T;
bool inque[nmax];
int head[nmax], pre_edge[nmax],pre_index[nmax];
valtype add_flow[nmax], dis[nmax];
struct edge{int to,nxt;valtype cap,flow,cost;}e[nmax<<1];
void init(int S, int T){
// memset(head,-1,sizeof head);
// tot = 0;
// final_cost = final_flow = 0;
this->S = S, this->T = T;
}
void add_edge(int u, int v, valtype cap, valtype cost){
e[tot].to = v, e[tot].nxt = head[u], e[tot].flow = 0, e[tot].cap = cap, e[tot].cost = cost, head[u] = tot++;
e[tot].to = u, e[tot].nxt = head[v], e[tot].flow = 0, e[tot].cap = 0, e[tot].cost = -cost, head[v] = tot++;
}
bool spfa(){
for(int i = S;i<=T;++i) inque[i] = false, dis[i] = i == S?0:INF;
queue<int> q; q.push(S), inque[S] = true, add_flow[S] = INF;
while(!q.empty()){
int u = q.front(); q.pop(); inque[u] = false;
for(int i = head[u];i!=-1;i=e[i].nxt){
int v = e[i].to;
if(e[i].cap > e[i].flow && dis[u] + e[i].cost < dis[v]){
dis[v] = dis[u] + e[i].cost, pre_edge[v] = i, pre_index[v] = u;
add_flow[v] = min(add_flow[u],e[i].cap - e[i].flow);
if(!inque[v]) q.push(v),inque[v] = true;
}
}
}
return dis[T] != INF;
}
void mincost_mxflow() {
while(spfa()){
final_flow += add_flow[T];
final_cost += add_flow[T] * dis[T];
int now = T;
while(now != S){
e[pre_edge[now]].flow += add_flow[T];
e[pre_edge[now]^1].flow -= add_flow[T];
now = pre_index[now];
}
}
}
}mcmf;
int n, m, k;
int main() {
memset(mcmf.head, -1, sizeof(mcmf.head));
int s = 0, t = n;
scanf("%d %d %d", &n, &m, &k);
int u, v, c, w;
mcmf.S = 1, mcmf.T = n;
for(int i = 1; i <= m; ++i) {
scanf("%d %d %d %d", &u, &v, &c, &w);
nv.push_back(ee{u, v, c, w});
mcmf.add_edge(u, v, c, 0);
}
mcmf.mincost_mxflow();
printf("%d ", mcmf.final_flow);
mcmf.final_flow = mcmf.final_cost = 0;
for(int i = 0; i < nv.size(); ++i) {
mcmf.add_edge(nv[i].u, nv[i].v, INF, nv[i].w);
}
mcmf.S = 0, mcmf.T = n;
mcmf.add_edge(0, 1, k, 0);
mcmf.mincost_mxflow();
printf("%d\n", mcmf.final_cost);
}