最大费用最大流
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
#define CLR(arr,v) memset(arr,v,sizeof(arr))
const int INF = 1<<29 ;
template<int MaxV,int MaxE>
class MinCostMaxFlow{
public:
void Clear(){
pos = 0;
CLR(h,-1); CLR(Flow,0);
}
void add(int u,int v,int f,int c){
num[pos] = v;
cap[pos] = f;
cost[pos] = c;
next[pos] = h[u];
h[u] = pos++;
num[pos] = u;
cap[pos] = 0;
cost[pos] = -c;
next[pos] = h[v];
h[v] = pos++;
}
int GetMinCostMaxFlow(int start,int end){
int cur,maxflow = 0,mincost = 0,minflow;
while(SPFA(start,end)){
cur = end;
minflow = INF;
while(cur != start){
if(minflow > cap[ pre_e[cur] ] - Flow[ pre_e[cur] ])
minflow = cap[ pre_e[cur] ] - Flow[ pre_e[cur] ];
cur = pre_v[cur];
}
cur = end;
maxflow += minflow;
while(cur != start){
Flow[ pre_e[cur] ] += minflow;
Flow[ pre_e[cur]^1] -= minflow;
mincost += minflow * cost[ pre_e[cur] ];
cur = pre_v[cur];
}
}
return mincost;
}
private:
int h[MaxV],dis[MaxV],num[MaxE],cap[MaxE],cost[MaxE],Flow[MaxE],next[MaxE],pre_e[MaxV],pre_v[MaxV],Q[MaxV],InQ[MaxV],pos;
bool SPFA(int s,int t){
CLR(dis,0);
CLR(InQ,false);
dis[s] = 1;
int head = 0,total = 0,cur;
Q[total++] = s;
while(head != total){
cur = Q[head++];
if(head >= MaxV) head = 0;
for(int i = h[cur];i != -1;i = next[i]){
if(cap[i] > Flow[i] && dis[cur] + cost[i] > dis[ num[i] ]){
dis[ num[i] ] = dis[cur] + cost[i];
pre_v[ num[i] ] = cur;
pre_e[ num[i] ] = i;
if(!InQ[ num[i] ])
{
if(total >= MaxV) total = 0;
InQ[ num[i] ] = true;
Q[total++] = num[i];
}
}
}
InQ[cur] = false;
}
return dis[t] > 1;
}
};
MinCostMaxFlow<2005,10000> g;
struct Friend{
int x,y;
};
Friend f[1005];
struct Gift{
int a,b,c;
};
Gift gift[5];
int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
g.Clear();
for(int i = 0;i < n;++i)
scanf("%d%d",&f[i].x,&f[i].y);
for(int i = 0;i < 2;++i)
{
scanf("%d%d%d",&gift[i].c,&gift[i].a,&gift[i].b);
g.add(0,i+1,gift[i].c,0);
}
int val;
for(int i = 0;i < 2;++i)
{
for(int j = 0;j < n;++j)
{
val = gift[i].a*f[j].x+gift[i].b*f[j].y;
g.add(i+1,j+3,1,val);
}
}
for(int i = 0;i < n;++i)
g.add(i+3,n+3,1,0);
printf("%d\n",g.GetMinCostMaxFlow(0,n+3));
}
return 0;
}