BZOJ1449: [JSOI2009]球队收益
BZOJ1449: [JSOI2009]球队收益
https://lydsy.com/JudgeOnline/problem.php?id=1280
分析:
- 和剪刀石头布那题建图很像,但这题输也有贡献,于是我们令他一开始全输就好了。
- 建图基本都一样就不说了,因为\(D_i\le C_i\)所以费用递增。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
#define inf 0x3f3f3f3f
#define N 7500
#define M 2000050
typedef double f2;
namespace EK {
const int S=N-1,T=N-2;
int head[N],to[M],nxt[M],flow[M],val[M],cnt=1;
int dis[N],Q[N],path[N],vis[N];
inline void add(int u,int v,int f,int c) {
to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; flow[cnt]=f; val[cnt]=c;
to[++cnt]=u; nxt[cnt]=head[v]; head[v]=cnt; flow[cnt]=0; val[cnt]=-c;
}
bool spfa() {
memset(path,0,sizeof(path));
memset(dis,0x3f,sizeof(dis));
int l=0,r=0;
Q[r++]=S; dis[S]=0;
while(l!=r) {
int x=Q[l++]; if(l==S) l=0;
int i; vis[x]=0;
for(i=head[x];i;i=nxt[i]) if(dis[to[i]]>dis[x]+val[i]&&flow[i]) {
dis[to[i]]=dis[x]+val[i]; path[to[i]]=i^1;
if(!vis[to[i]]) {
vis[to[i]]=1; Q[r++]=to[i]; if(r==S) r=0;
}
}
}
return path[T]!=0;
}
pair<int,int> ek() {
int minc=0,maxf=0;
while(spfa()) {
int nf=inf;
int i;
for(i=T;i!=S;i=to[path[i]]) {
nf=min(nf,flow[path[i]^1]);
}maxf+=nf;
for(i=T;i!=S;i=to[path[i]]) {
flow[path[i]^1]-=nf;
flow[path[i]]+=nf;
minc+=nf*val[path[i]^1];
}
}
return make_pair(minc,maxf);
}
}
int n,m,wi[N],lo[N],tot[N],C[N],D[N],K;
int Abs(int x) {return x>0?x:-x;}
int pf(int x) {return x*x;}
int calc(int x,int y) {
return pf(wi[x]+y)*C[x]+pf(lo[x]+tot[x]-y)*D[x];
}
int main() {
using namespace EK;
scanf("%d%d",&n,&m);
int i,j,x,y;
K=n;
for(i=1;i<=n;i++) scanf("%d%d%d%d",&wi[i],&lo[i],&C[i],&D[i]);
for(i=1;i<=m;i++) {
scanf("%d%d",&x,&y); tot[x]++; tot[y]++;
K++; add(K,x,1,0); add(K,y,1,0); add(S,K,1,0);
}
for(i=1;i<=n;i++) {
for(j=1;j<=tot[i];j++) {
add(i,T,1,calc(i,j)-calc(i,j-1));
}
}
int sum=0;
for(i=1;i<=n;i++) sum+=pf(tot[i]+lo[i])*D[i]+pf(wi[i])*C[i];
printf("%d\n",ek().first+sum);
}