UVA 1486 - Transportation(网络流+拆边)
2014-09-25 15:07
148 查看
UVA 1486 - Transportation
题目链接题意:一个有向图上运输k货物,有一些边,每个边一个系数a,经过该边如果有x货物,就要缴纳a
x x的钱,问运输从1到n最小的代价
思路:费用流,这题边的容量c最大只有5,所以可以拆边,一条边拆成c条边,每条容量1,对应相应的代价为a * (i^2 - (i - 1)^2),然后跑一下费用流即可
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXNODE = 105;
const int MAXEDGE = 200005;
typedef int Type;
const Type INF = 0x3f3f3f3f;
struct Edge {
int u, v;
Type cap, flow, cost;
Edge() {}
Edge(int u, int v, Type cap, Type flow, Type cost) {
this->u = u;
this->v = v;
this->cap = cap;
this->flow = flow;
this->cost = cost;
}
};
struct MCFC {
int n, m, s, t;
Edge edges[MAXEDGE];
int first[MAXNODE];
int next[MAXEDGE];
int inq[MAXNODE];
Type d[MAXNODE];
int p[MAXNODE];
Type a[MAXNODE];
void init(int n) {
this->n = n;
memset(first, -1, sizeof(first));
m = 0;
}
void add_Edge(int u, int v, Type cap, Type cost) {
edges[m] = Edge(u, v, cap, 0, cost);
next[m] = first[u];
first[u] = m++;
edges[m] = Edge(v, u, 0, 0, -cost);
next[m] = first[v];
first[v] = m++;
}
bool bellmanford(int s, int t, Type &flow, Type &cost) {
for (int i = 0; i < n; i++) d[i] = INF;
memset(inq, false, sizeof(inq));
d[s] = 0; inq[s] = true; p[s] = s; a[s] = INF;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = false;
for (int i = first[u]; i != -1; i = next[i]) {
Edge& e = edges[i];
if (e.cap > e.flow && d[e.v] > d[u] + e.cost) {
d[e.v] = d[u] + e.cost;
p[e.v] = i;
a[e.v] = min(a[u], e.cap - e.flow);
if (!inq[e.v]) {Q.push(e.v); inq[e.v] = true;}
}
}
}
if (d[t] == INF) 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]].u;
}
return true;
}
Type Mincost(int s, int t, Type D) {
Type flow = 0, cost = 0;
while (bellmanford(s, t, flow, cost));
if (D != flow) return -1;
return cost;
}
} gao;
int n, m, k;
int main() {
while (~scanf("%d%d%d", &n, &m, &k)) {
gao.init(n + 1);
gao.add_Edge(0, 1, k, 0);
int u, v, a, c;
while (m--) {
scanf("%d%d%d%d", &u, &v, &a, &c);
for (int i = 1; i <= c; i++) {
gao.add_Edge(u, v, 1, a * (i * i - (i - 1) * (i - 1)));
}
}
printf("%d\n", gao.Mincost(0, n, k));
}
return 0;
}
相关文章推荐
- (beginer) 网络流(最小费用流+费用跟流量有关) UVA 1486 - Transportation
- Uva-1486-Transportation
- UVa1486 Transportation(最小费用最大流)
- uva 1486 Transportation (费用流+拆边)
- uva1486 Transportation
- uva 1486 - Transportation(最小费用流)
- UVA-1486 - Transportation(最小费用最大流)
- UVA 11419 SAM I AM 网络流
- Uva 301 - Transportation
- 【网络流#4】UVA 753 最大流
- 【费用流模型】BZOJ2668 UVA1317 UVA1486 UVA1104
- uva 10594 网络流(邻接表)
- UVA 12534 - Binary Matrix 2 (网络流‘最小费用最大流’ZKW)
- UVA 11082 Matrix Decompressing(网络流)
- UVa Problem 10249 The Grand Dinner - 网络流解题
- UVA10480 Sabotage (网络流,最小割,ISAP算法,最大流最小割定理)
- Monkeys in the Emei Mountain -- UVA - 11167 (网络流)
- UVA-10779 Collectors Problem (网络流建模)
- uva 301 Transportation
- UVA 10779 - Collectors Problem(网络流)