标签:style blog color os io for ar 2014 div
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<queue> #include<set> #include<cmath> #include<bitset> #define mem(a,b) memset(a,b,sizeof(a)) #define lson i<<1,l,mid #define rson i<<1|1,mid+1,r #define llson j<<1,l,mid #define rrson j<<1|1,mid+1,r #define INF 0x7fffffff typedef long long ll; typedef unsigned long long ull; using namespace std; #define maxn 20005 struct { int v,w,c,next,re; //re记录逆边的下标,c是费用,w是流量 } e[maxn]; int n,cnt; int head[maxn],que[maxn*8],pre[maxn],dis[maxn]; bool vis[maxn]; void add(int u, int v, int w, int c) { e[cnt].v=v,e[cnt].w=w,e[cnt].c=c; e[cnt].next=head[u]; e[cnt].re=cnt+1,head[u]=cnt++; e[cnt].v=u,e[cnt].w=0,e[cnt].c=-c; e[cnt].next=head[v]; e[cnt].re=cnt-1,head[v]=cnt++; } bool spfa() { int i, l = 0, r = 1; for(i = 0; i <= n; i ++) dis[i] = INF,vis[i] = false; dis[0]=0,que[0]=0,vis[0]=true; while(l<r) { int u=que[l++]; for(i=head[u];i!=-1;i=e[i].next) { int v = e[i].v; if(e[i].w&&dis[v]>dis[u]+e[i].c) { dis[v] = dis[u] + e[i].c; pre[v] = i; if(!vis[v]) { vis[v] = true; que[r++] = v; } } } vis[u] = false; } return dis[n]!=INF; } int change() { int i,p,sum=INF,ans=0; for(i=n;i!=0;i=e[e[p].re].v) {//e[e[p].re].v为前向结点,不理解看add和spfa p=pre[i];//p为前向结点编号 sum=min(sum,e[p].w); } for(i=n;i!=0;i=e[e[p].re].v) { p=pre[i]; e[p].w-=sum; e[e[p].re].w+=sum; ans+=sum*e[p].c;//c记录的为单位流量费用,必须得乘以流量。 } return ans; } int EK() { int sum=0; while(spfa()) sum+=change(); return sum; } void init() { mem(head,-1),mem(pre,0),cnt=0; } int a[51][51],b[51][51],cost[51][101],aa[51],bb[51]; int main() { //freopen("1.txt","r",stdin); int N,m,K; while(~scanf("%d%d%d",&N,&m,&K)) { if(!N&&!m&&!K) break; int i,j,k,flag=0,sum=0,s=0,t=m+N+1; mem(aa,0),mem(bb,0); for(i=1;i<=N;i++) for(j=1;j<=K;j++) { scanf("%d",&a[i][j]); aa[j]+=a[i][j]; } for(i=1;i<=m;i++) for(j=1;j<=K;j++) { scanf("%d",&b[i][j]); bb[j]+=b[i][j]; } for(i=1;i<=K;i++) if(bb[i]<aa[i]) {flag=1;break;} for(k=1;k<=K;k++) { for(i=m+1;i<=N+m;i++) for(j=1;j<=m;j++) scanf("%d",&cost[j][i]); if(flag) continue; init(); for(i=1;i<=m;i++) add(s,i,b[i][k],0); for(i=1;i<=m;i++) for(j=m+1;j<=N+m;j++) add(i,j,b[i][k],cost[i][j]); for(i=m+1;i<=m+N;i++) add(i,t,a[i-m][k],0); n=t; sum+=EK();//每种k物品都求一次 } if(flag) puts("-1"); else printf("%d\n",sum); } return 0; }
标签:style blog color os io for ar 2014 div
原文地址:http://blog.csdn.net/u011466175/article/details/38826485