1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| #include<iostream> #include<cmath> #include<cstdio> #include<cstring> #include<queue> #include<stack> #include<vector> #include<map> #include<set> #include<algorithm>
#define I_copy_this_answer return 0;
using namespace std;
int n,m,head[1100],size=1; int mmx=1000,mincost,maxwater; int flow[1100]; int need[1100],cost[310][310]; int pre[1100],las[1100],dis[1100],vis[1100],hw[1100];
struct edge{ int next,to,dis,flow; }e[100860];
void addedge(int next,int to,int dis,int flow) { e[++size].to=to; e[size].dis=dis; e[size].flow=flow; e[size].next=head[next]; head[next]=size; }
int spfa(int s) { memset(flow,0x3f,sizeof(flow)); memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis)); queue <int> q; q.push(s); dis[s]=0; vis[s]=1; pre[mmx]=-1; while(!q.empty()) { int t=q.front(); q.pop(); vis[t]=0; int i,j,k,l; for(i=head[t];i;i=e[i].next) { j=e[i].to; k=e[i].dis; l=e[i].flow; if(dis[t]+k<dis[j]&&l>0) { dis[j]=dis[t]+k; las[j]=i; pre[j]=t; flow[j]=min(flow[t],l); if(!vis[j]) { q.push(j); vis[j]=1; } } } } return pre[mmx]!=-1; }
void mcmf() { while(spfa(0)) { mincost+=dis[mmx]*flow[mmx]; int t=mmx; while(t!=0) { e[las[t]].flow-=flow[mmx]; e[las[t]^1].flow+=flow[mmx]; t=pre[t]; } } }
void build_edge(int t) { int i,j; for(i=1;i<=m;i++) { addedge(0,i,0,hw[i]); addedge(i,0,0,0); } for(i=1;i<=m;i++) for(j=1;j<=n;j++) { addedge(i,j+m,cost[i][j]*t,need[j]); addedge(j+m,i,-cost[i][j]*t,0); } for(i=1;i<=n;i++) { addedge(i+m,mmx,0,need[i]); addedge(mmx,i+m,0,0); } }
int main() { int i,j; scanf("%d %d",&m,&n); for(i=1;i<=m;i++) { int t1; scanf("%d",&hw[i]); } for(i=1;i<=n;i++) scanf("%d",&need[i]); for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf("%d",&cost[i][j]); build_edge(1); mcmf(); printf("%d",mincost); maxwater=0; mincost=0; size=1; memset(head,0,sizeof(head)); build_edge(-1); mcmf(); printf("\n%d",-mincost); I_copy_this_answer }
|