网络流/费用流/二分图最小权匹配
题解:http://blog.csdn.net/huzecong/article/details/9119741
太神了!由于一赢一输不好建图,就先假设全部都输,再将赢的收益修改!就变成普通的二分图了!!
费用与流量的平方相关时拆边……这个稍微处理一下即可
/**************************************************************
Problem: 1449
User: Tunix
Language: C++
Result: Accepted
Time:676 ms
Memory:3940 kb
****************************************************************/ //BZOJ 1449
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define pb push_back
#define CC(a,b) memset(a,b,sizeof(a))
using namespace std;
int getint(){
int v=,sign=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') sign=-; ch=getchar();}
while(isdigit(ch)) {v=v*+ch-''; ch=getchar();}
return v*sign;
}
const int N=,M=,INF=~0u>>;
typedef long long LL;
const double eps=1e-;
/*******************template********************/
int n,m,w[N],l[N],c[N],d[N],du[N];
LL ans;
struct edge{int from,to,v,c;};
struct Net{
edge E[M];
int head[N],next[M],cnt;
void ins(int x,int y,int z,int c){
E[++cnt]=(edge){x,y,z,c};
next[cnt]=head[x]; head[x]=cnt;
}
void add(int x,int y,int z,int c){
ins(x,y,z,c); ins(y,x,,-c);
}
int S,T,d[N],from[N],Q[M];
bool inq[N];
bool spfa(){
int l=,r=-;
F(i,,T)d[i]=INF;
d[S]=; Q[++r]=S; inq[S]=;
while(l<=r){
int x=Q[l++]; inq[x]=;
for(int i=head[x];i;i=next[i])
if(E[i].v && d[x]+E[i].c<d[E[i].to]){
d[E[i].to]=d[x]+E[i].c;
from[E[i].to]=i;
if(!inq[E[i].to]){
Q[++r]=E[i].to;
inq[E[i].to]=;
}
}
}
return d[T]!=INF;
}
void mcf(){
int x=INF;
for(int i=from[T];i;i=from[E[i].from])
x=min(x,E[i].v);
for(int i=from[T];i;i=from[E[i].from]){
E[i].v-=x;
E[i^].v+=x;
}
ans+=x*d[T];
}
void init(){
n=getint(); m=getint(); cnt=; ans=;
S=; T=n+m+;
F(i,,n){
w[i]=getint();l[i]=getint();
c[i]=getint();d[i]=getint();
// ans+=w[i]*w[i]*c[i]+l[i]*l[i]*d[i];
}
int x,y;
F(i,,m){
x=getint(); y=getint();
du[x]++; du[y]++;
add(x,i+n,,); add(y,i+n,,);
add(i+n,T,,);
l[x]++; l[y]++;
}
F(i,,n) ans+=w[i]*w[i]*c[i]+l[i]*l[i]*d[i]; F(i,,n) F(j,,du[i])
add(S,i,,((j+w[i])*-)*c[i] - ((l[i]-j+)*-)*d[i] );
while(spfa()) mcf();
printf("%lld\n",ans);
}
}G1;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
G1.init();
return ;
}
1449: [JSOI2009]球队收益
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 516 Solved: 283
[Submit][Status][Discuss]
Description
Input
Output
一个整数表示联盟里所有球队收益之和的最小值。
Sample Input
3 3
1 0 2 1
1 1 10 1
0 1 3 3
1 2
2 3
3 1
1 0 2 1
1 1 10 1
0 1 3 3
1 2
2 3
3 1
Sample Output
43