Description
The degree bear participates in the business convention of the Meow village, but this business meeting has encountered a difficult problem:
Meow Ha Village and the surrounding village can be seen as a total of n pieces of area, M road composed of areas.
Due to the difference in production capacity, the section I can spend a[i] yuan to produce 1 products, but the maximum production b[i].
Similarly, because of the difference in the purchasing power of each slice, the section I is able to sell the most d[i items at the price of c[i].
Because of these factors, the degree bears feel that only reasonable transfer of goods in order to obtain the greatest benefits.
It is estimated that each commodity transport 1 kilometers, will cost 1 yuan.
So the cat haha village can realize how much profit.
Input
The subject contains several sets of test data.
Each set of test data contains:
The first line of two integers n,m that the village consists of n slices, M Street.
The next n lines, four integers per line a[i],b[i],c[i],d[i] the "I" region, can be produced at the price of A[i], most of the production of B[i], to c[i the price of sale, up to the sale of d[i].
Next m line, three integers per line, u[i],v[i],k[i], indicating that the road is connected U[i],v[i] Two slices, distance is k[i]
There may be heavy edges, and there may be self loops.
Meet:
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
The output can make much money.
Sample Input
2 1 5 5 6 1 3 5 7 7 1 2-
1
Sample Output
23
train of Thought
Minimum cost maximum flow, first set up super source point S, with super Sink point T.
Because the production of a product costs a[i] yuan, and the upper limit of b[i], so we from S to these points between a volume of b[i], the cost of-a[i] side.
In the same way, the sale of a commodity can earn c[i] yuan, up to the sale of d[i], so we from these points to t even a volume of d[i], the cost of c[i] side.
Finally all highways are also spent, from u to v connecting a two-way edge, capacity for INF, cost-K, and then run side template can be.
Note: There is a self loop in the diagram, and when we get the two-point path length less than 0, the calculation should be terminated.
AC Code
#include <bits/stdc++.h> using namespace std;
#define INF 0x3f3f3f3f typedef long LL;
const int m=2010;
const int n=510;
struct Edge {int to;
int next;
int cap;
int cost;
} e[11000];
int Head[n],tot;
int d[n], pre[n], path[n];
BOOL Vis[n];
void Init () {memset (head,-1,sizeof head);
tot = 0;
} void Addedge (int s, int t, int cap, int cost) {e[tot].to=t;
E[tot].cap=cap;
E[tot].cost=cost;
E[tot].next=head[s];
Head[s] = tot++;
E[tot].to=s;
E[tot].cap=0;
E[tot].cost=-cost;
E[TOT].NEXT=HEAD[T];
Head[t] = tot++;
int SPFA (int s, int t) {memset (d,-inf,sizeof (d));
memset (pre,-1,sizeof (pre));
memset (path,-1,sizeof (path));
memset (Vis) (vis,false,sizeof);
int res = d[0];
D[s] = 0;
Vis[s] = true;
queue<int>q;
Q.push (s);
while (!q.empty ()) {int u = q.front ();
Q.pop ();
Vis[u] = false;
for (int i = head[u]; ~i i = e[i].next) { int v = e[i].to;
if (D[v] < D[u] + e[i].cost && e[i].cap > 0) {d[v] = D[u] + e[i].cost;
PRE[V] = u;
PATH[V] = i;
if (!vis[v]) {Vis[v] = true;
Q.push (v);
}}} return d[t]!= res;
int Mincostmaxflow (int s, int t,int &cost) {int flow;
flow=cost=0;
while (SPFA (s, t)) {int minn = INF;
for (int i = t; I!= s && ~i i = pre[i]) minn = MIN (minn, e[path[i]].cap);
for (int i = t; I!= s && ~i i = pre[i]) {e[path[i]].cap-= Minn;
E[path[i] ^ 1].cap + + minn;
} if (D[t] < 0) break;
Flow + + Minn;
Cost + = Minn * D[t];
} return flow;
int main (void) {int n,m;
while (cin>>n>>m) {init (); int s=0,t=n+1,cost;
for (int i=1; i<=n; i++) {int a,b,c,d;
cin>>a>>b>>c>>d;
Addedge (S,I,B,-A);
Addedge (I,T,D,C);
while (m--) {int u,v,k;
cin>>u>>v>>k;
Addedge (U,V,INF,-K);
Addedge (V,U,INF,-K);
} mincostmaxflow (S,t,cost);
cout<<cost<<endl;
return 0; }