标签:
Description
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 road i, there is a coefficient ai. If you want to carry x units of goods along this road, you should pay ai*x2 dollars to hire guards to protect your goods. And what’s worse, for each road i, there is an upper bound Ci, which means that you cannot transport more than Ci 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. (1
Output
Output one line for each test case, indicating the minimum cost. If it is impossible to transport all the K 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
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 200;
const int M = 150000;
typedef long long ll;
int n, m, k, s, t;
int pre[N], inq[N];
ll a[N], d[N];
struct Edge{
int from, to;
ll cap, flow;
ll cos;
};
vector<Edge> edges;
vector<int> G[M];
void init() {
for (int i = 0; i < M; i++) G[i].clear();
edges.clear();
}
void addEdge(int from, int to, ll cap, ll flow, ll cos) {
edges.push_back((Edge){from, to, cap, 0, cos});
edges.push_back((Edge){to, from, 0, 0, -cos});
int m = edges.size();
G[from].push_back(m - 2); G[to].push_back(m - 1);
}
int BF(int s, int t, ll& flow, ll& cost) {
queue<int> Q;
memset(inq, 0, sizeof(inq));
memset(a, 0, sizeof(a));
memset(pre, 0, sizeof(pre));
for (int i = 0; i < N; i++) d[i] = INF;
d[s] = 0;
a[s] = INF;
inq[s] = 1;
int flag = 1;
pre[s] = 0;
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.cos) {
d[e.to] = d[u] + e.cos;
a[e.to] = min(a[u], e.cap - e.flow);
pre[e.to] = G[u][i];
if (!inq[e.to]) {
inq[e.to] = 1;
Q.push(e.to);
}
}
}
flag = 0;
}
if (d[t] == INF) return 0;
flow += a[t];
cost += (ll)d[t] * (ll)a[t];
for (int u = t; u != s; u = edges[pre[u]].from) {
edges[pre[u]].flow += a[t];
edges[pre[u]^1].flow -= a[t];
}
return 1;
}
int MCMF(int s, int t, ll& cost) {
ll flow = 0;
cost = 0;
while (BF(s, t, flow, cost));
return flow;
}
void input() {
int u, v;
ll a;
ll c;
for (int i = 0; i < m; i++) {
scanf("%d %d %lld %lld", &u, &v, &a, &c);
for (int i = 1; i <= c; i++) {
addEdge(u, v, 1, 0, a * (i * i - (i - 1) * (i - 1)));
}
}
addEdge(0, 1, k, 0, 0);
s = 0, t = n;
}
int main() {
while (scanf("%d %d %d", &n, &m, &k) == 3) {
init();
input();
ll cost;
if (MCMF(s, t, cost) != k) printf("-1\n");
else printf("%lld\n", cost);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许也可以转载,不过要注明出处哦。
uva 1486 Transportation (最大流+拆边)
标签:
原文地址:http://blog.csdn.net/llx523113241/article/details/47377497