There are N cities, and M directed roads connecting them. Now you want to transport K units of goods from city 1 to city N. There are many robbers on the road, so you must be very careful. The more goods you carry, the more dangerous it is. To be more specific, for each roadi, there is a coefficient ai. If you want to carryx units of goods along this road, you should payai* x2 dollars to hire guards to protect your goods. And what's worse, for each roadi, there is an upper boundCi, which means that you cannot transport more thanCi units of goods along this road. Please note you can only carry integral unit of goods along each road.
You should find out the minimum cost to transport all the goods safely.
Input
There are several test cases. The first line of each case contains three integers,N,M and
K. (1N
100,
1
M
5000,
0
K
100).
Then M lines followed, each contains four integers(ui,vi,
ai,Ci), indicating there is a directed road from cityui to
vi, whose coefficient isai and upper bound isCi.
(1
ui,vi
N,
0 <ai
100,Ci
5)
Output
Output one line for each test case, indicating the minimum cost. If it is impossible to transport all theK units of goods, output `-1'.
Sample Input
2 1 2 1 2 1 2 2 1 2 1 2 1 1 2 2 2 1 2 1 2 1 2 2 2
Sample Output
4 -1 3
首先,构图上要用到拆边法,把ax2这样的边,拆为a,3a,5a,7a这样的容量为1的边。然后此题是固定流量的,所以在求最小费用,增广时要检查一下,在flow+a>=k的时候只增广k - flow单位的流量,然后终止程序。
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 100+10;
const int INF = 1000000000;
struct Edge{
int from,to,cap,flow,cost;
};
struct MCMF {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn]; // 是否在队列中
int d[maxn]; // Bellman-Ford
int p[maxn]; // 上一条弧
int a[maxn]; // 可改进量
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 k,int s, int t, int &flow,int &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;//s-t不连通,失败退出
if(flow + a[t] >= k){//判断流量是否达到了k
cost += d[t] * (k-flow);
flow = k;
return false;
}
flow += a[t];
cost += d[t] * a[t];
int u = t;
while(u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
// 需要保证初始网络中没有负权圈
int Mincost(int &can,int s, int t) {
int flow = 0,cost = 0;
while(BellmanFord(can,s, t,flow, cost));
can = flow;
return cost;
}
};
MCMF g;
int main(){
int n,m,k;
while(scanf("%d%d%d",&n,&m,&k) != EOF){
g.init(n);
int sorce = 0,sink = n-1;
for(int i = 0;i < m;i++){
int from,to,a,cap;
scanf("%d%d%d%d",&from,&to,&a,&cap);
for(int i = 1;i <= cap;i++){
g.AddEdge(from-1,to-1,1,a*(2*i-1));
}
}
int can = k;
int ans = g.Mincost(can,sorce,sink);
if(can == k) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}