篇首语:本文由编程笔记#小编为大家整理,主要介绍了bzoj1449/bzoj2895[JSOI2009]球队收益/球队预算 费用流相关的知识,希望对你有一定的参考价值。
第i支队伍向T连fi条边,容量均为1,第j条边表示赢j场比赢j-1场多出来的收益,所以费用应为ci*(wi+j)^2+di*(wi+fi-j)^2-ci*(li+j-1)^2-di*(li+j+1)^2。
#include
#include
#include
#define N 10010
#define M 3500000
using namespace std;
queue q;
int w[N] , l[N] , c[N] , d[N] , x[N] , y[N] , f[N];
int head[N] , to[M] , val[M] , cost[M] , next[M] , cnt = 1 , s , t , dis[N] , from[N] , pre[N];
void add(int x , int y , int v , int c)
{
to[++cnt] = y , val[cnt] = v , cost[cnt] = c , next[cnt] = head[x] , head[x] = cnt;
to[++cnt] = x , val[cnt] = 0 , cost[cnt] = -c , next[cnt] = head[y] , head[y] = cnt;
}
bool spfa()
{
int x , i;
memset(from , -1 , sizeof(from));
memset(dis , 0x3f , sizeof(dis));
dis[s] = 0 , q.push(s);
while(!q.empty())
{
x = q.front() , q.pop();
for(i = head[x] ; i ; i = next[i])
if(val[i] && dis[to[i]] > dis[x] + cost[i])
dis[to[i]] = dis[x] + cost[i] , from[to[i]] = x , pre[to[i]] = i , q.push(to[i]);
}
return ~from[t];
}
int mincost()
{
int i , k , ans = 0;
while(spfa())
{
k = 0x7fffffff;
for(i = t ; i != s ; i = from[i]) k = min(k , val[pre[i]]);
ans += k * dis[t];
for(i = t ; i != s ; i = from[i]) val[pre[i]] -= k , val[pre[i] ^ 1] += k;
}
return ans;
}
int main()
{
int n , m , i , j , ans = 0;
scanf("%d%d" , &n , &m) , s = 0 , t = m + n + 1;
for(i = 1 ; i <= n ; i ++ ) scanf("%d%d%d%d" , &w[i] , &l[i] , &c[i] , &d[i]);
for(i = 1 ; i <= m ; i ++ ) scanf("%d%d" , &x[i] , &y[i]) , f[x[i]] ++ , f[y[i]] ++ , l[x[i]] ++ , l[y[i]] ++ ;
for(i = 1 ; i <= n ; i ++ ) ans += c[i] * w[i] * w[i] + d[i] * l[i] * l[i];
for(i = 1 ; i <= m ; i ++ ) add(s , i , 1 , 0) , add(i , x[i] + m , 1 , 0) , add(i , y[i] + m , 1 , 0);
for(i = 1 ; i <= n ; i ++ )
for(j = 1 ; j <= f[i] ; j ++ )
add(i + m , t , 1 , c[i] * (2 * w[i] + 2 * j - 1) - d[i] * (2 * l[i] - 2 * j + 1));
printf("%d\n" , ans + mincost());
return 0;
}