【中文题意】
度度熊的交易计划 Accepts: 460 Submissions: 2329
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题:
喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区。
由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但是最多生产b[i]个。
同样的,由于每个片区的购买能力的区别,第i个片区也能够以c[i]的价格出售最多d[i]个物品。
由于这些因素,度度熊觉得只有合理的调动物品,才能获得最大的利益。
据测算,每一个商品运输1公里,将会花费1元。
那么喵哈哈村最多能够实现多少盈利呢?
Input
本题包含若干组测试数据。 每组测试数据包含: 第一行两个整数n,m表示喵哈哈村由n个片区、m条街道。 接下来n行,每行四个整数a[i],b[i],c[i],d[i]表示的第i个地区,能够以a[i]的价格生产,最多生产b[i]个,以c[i]的价格出售,最多出售d[i]个。 接下来m行,每行三个整数,u[i],v[i],k[i],表示该条公路连接u[i],v[i]两个片区,距离为k[i]
可能存在重边,也可能存在自环。
满足: 1<=n<=500, 1<=m<=1000, 1<=a[i],b[i],c[i],d[i],k[i]<=1000, 1<=u[i],v[i]<=n
Output
输出最多能赚多少钱。
Sample Input
2 1
5 5 6 1
3 5 7 7
1 2 1
Sample Output
23
【思路分析】
要求最大的利益,而且可以进行物品的调动,想到了最小费用最大流,从网上抄了个板子就搞上了,设一个源点和一个汇点就好了。
【AC代码】
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<queue>
using namespace std;
template<typename flow_t, typename cost_t>
struct MCMF
{
static const int N = 2000, M = 10000;
const flow_t inf = 1e9;
struct node
{
int from, to, nxt;
flow_t cap, flow;
cost_t cost;
node() {}
node(int from, int to, int nxt, flow_t cap, cost_t cost):
from(from), to(to), nxt(nxt), cap(cap), flow(0), cost(cost) {}
} E[M];
cost_t dis[N];
int G[N], pre[N], vis[N], n, m;
void init(int n)
{
this->n = n;
this->m = 0;
fill(G, G + n, -1);
}
void add(int u, int v, flow_t f, cost_t c)
{
E[m] = node(u, v, G[u], f, +c);
G[u] = m++;
E[m] = node(v, u, G[v], 0, -c);
G[v] = m++;
}
bool bfs(int S, int T)
{
fill(vis, vis + n, 0);
fill(dis, dis + n, inf);
queue<int> queue;
dis[S] = 0;
queue.push(S);
for (; !queue.empty(); queue.pop())
{
int u = queue.front();
vis[u] = false;
for (int it = G[u]; ~it; it = E[it].nxt)
{
int v = E[it].to;
if (E[it].cap > E[it].flow && dis[v] > dis[u] + E[it].cost)
{
dis[v] = dis[u] + E[it].cost;
pre[v] = it;
if (!vis[v]) queue.push(v);
vis[v] = true;
}
}
}
return dis[T] < 0; // 改成dis[T] <= 0 求可行流
}
int Mincost(int S, int T)
{
flow_t max_flow = 0;
cost_t min_cost = 0;
while (bfs(S, T))
{
flow_t delta = inf;
for (int u = T; u != S; u = E[pre[u]].from)
{
delta = std::min(delta, E[pre[u]].cap - E[pre[u]].flow);
}
min_cost += delta * dis[T];
max_flow += delta;
for (int u = T; u != S; u = E[pre[u]].from)
{
E[pre[u]].flow += delta;
E[pre[u] ^ 1].flow -= delta;
}
}
return min_cost;
}
};
int main()
{
int n, m;
int a, b, c, d;
while(~ scanf("%d %d", &n, &m) )
{
MCMF<int, int> mcmf;
mcmf.init(n + 2);
int S = 0, T = n + 1;
for(int i = 1; i <= n; i ++)
{
scanf("%d %d %d %d", &a, &b, &c, &d);
mcmf.add(S, i, b, a);
mcmf.add(i, T, d, -c);
}
for(int i = 1; i <= m; i ++)
{
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
mcmf.add(u, v, mcmf.inf, w);
mcmf.add(v, u, mcmf.inf, w);
}
int re = mcmf.Mincost(S, T);
printf("%d\n",-re);
}
return 0;
}