记录编号 61117 评测结果 AAAAAAAAAA
题目名称 [NOI 2012]美食节 最终得分 100
用户昵称 Gravatarcstdio 是否通过 通过
代码语言 C++ 运行时间 3.437 s
提交时间 2013-06-04 13:13:41 内存使用 5.65 MiB
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<vector>
#include<queue>
using namespace std;
const int INF=0x7fffffff;
const int SIZEN=100000;
int N,M,P=0;
int t[1000][1000];
int S,T;
class EDGE{
public:
	int from,to,cap,flow,cost;
};
vector<EDGE> edges;
vector<int> c[SIZEN];//邻接表
int ansc=0;
void addedge(int from,int to,int cap,int cost){
	edges.push_back((EDGE){from,to,cap,0,cost});
	edges.push_back((EDGE){to,from,0,0,-cost});
	int tot=edges.size()-2;
	c[from].push_back(tot);
	c[to].push_back(tot+1);
}
void addchef(int x,int k){//第x个厨师倒数第k个做菜
	int now=N+P*(x-1)+k;
	int i;
	addedge(now,T,1,0);
	for(i=1;i<=N;i++) addedge(i,now,1,k*t[i][x]);
}
unsigned long long fc=0;
int ansf=0;
bool SPFA(int s,int t,int n){//0~n
	queue<int> Q;
	int f[SIZEN]={0};//距离
	bool inq[SIZEN]={0};//是否在队列中
	int cf[SIZEN]={0};//每个点的最大可改进量
	int father[SIZEN]={0};
	int i;
	for(i=0;i<=T;i++) f[i]=INF,inq[i]=false,cf[i]=INF,father[i]=-1;
	f[s]=0,inq[s]=true,Q.push(s);
	int x;
	#define now edges[c[x][i]]
	while(!Q.empty()){
		x=Q.front();Q.pop();
		inq[x]=false;
		if(x==T) continue;
		for(i=0;i<c[x].size();i++){
			fc++;
			if(now.to==S) continue;
			if(now.cap>now.flow&&f[now.to]>f[x]+now.cost){
				f[now.to]=f[x]+now.cost;
				father[now.to]=c[x][i];
				cf[now.to]=min(cf[now.to],now.cap-now.flow);
				if(!inq[now.to]){
					inq[now.to]=true;
					Q.push(now.to);
				}
			}
		}
	}
	if(f[t]==INF) return false;
	ansc+=cf[t]*f[t];
	ansf+=cf[t];
	x=t;
	int nowc,nowk;//当前的厨师,当前厨师做倒数第几道菜
	nowc=(edges[father[x]].from-N)/P+1;
	nowk=(edges[father[x]].from-N)%P;
	if(nowk==0) nowk+=P;
	if(nowk+1<=P) addchef(nowc,nowk+1);
	while(x!=s){
		edges[father[x]].flow+=cf[t];
		edges[father[x]^1].flow-=cf[t];
		x=edges[father[x]].from;
	}
	return true;
}
void MCMF(void){
	for(int i=1;i<=P;i++) SPFA(S,T,T);
	cout<<ansc<<endl;
}
int p[SIZEN]={0};
void init(void){
	scanf("%d%d",&N,&M);//N种菜M个厨师
	int i,j;
	for(i=1;i<=N;i++){
		scanf("%d",&p[i]);
		P+=p[i];
	}
	for(i=1;i<=N;i++){
		for(j=1;j<=M;j++) scanf("%d",&t[i][j]);//第j个厨师做第i种菜
	}
	S=0,T=N+P*M+1;
	for(i=1;i<=N;i++) addedge(S,i,p[i],0);
	for(i=1;i<=M;i++) addchef(i,1);
}
int main(){
	freopen("noi12_delicacy.in","r",stdin);
	freopen("noi12_delicacy.out","w",stdout);
	init();
	MCMF();
	return 0;
}