POJ - 2516 Minimum Cost 每次要跑K次费用流
题意:
有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的。问为了满足不同买家的订单的最小的花费。
思路:
设立一个源点S和汇点T,从源点S到每个仓库(1~m)连上容量为商品A的库存、费用为0的边,每个仓库再向每个不同的买家连上容量inf,费用为路费的边、每个顾客向汇点连一条容量为自己对商品A的需求个数、费用为0的边。跑一边费用流即可。这只有运送一个商品的费用,对,那我们就对不同商品建不同的图,一共跑K边,累计答案即可。
自己一直在想建图,怎么一次就跑出费用。感觉上面的思路和多开一维的观点差不多。
//#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error "-std=c++11" // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 200; int n,m,k,x; int req[maxn][maxn],hav[maxn][maxn],fy[maxn][maxn][maxn]; int res[maxn]; const int maxl = 1e6+9; struct Edge{ int to,val,cost,nxt; }gEdge[maxl]; int h[maxn],gPre[maxn]; int gPath[maxl],gDist[maxn]; bool in[maxn]; int gcount = 0; bool spfa(int s,int t){ memset(gPre,-1,sizeof(gPre)); memset(gDist, inf,sizeof(gDist)); memset(in, false, sizeof(in)); gDist[s] = 0; in[s] = true; queue<int>q; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); in[u] = false; for(int e = h[u]; e!=-1;e = gEdge[e].nxt){ int v = gEdge[e].to, w = gEdge[e].cost; if(gEdge[e].val > 0 && gDist[v] > gDist[u] + w){ gDist[v] = gDist[u] + gEdge[e].cost; gPre[v] = u; gPath[v] = e; if(!in[v]){ q.push(v); in[v] = true; } } } } return gPre[t] != -1; } int MinCostFlow(int s,int t,int nd){ int cost = 0,flow = 0; while(spfa(s,t)){ int f = inf; for(int u=t; u!=s; u = gPre[u]){ if(gEdge[gPath[u]].val < f){ f = gEdge[gPath[u]].val; } } flow +=f; cost += gDist[t]*f; for(int u=t; u!=s; u=gPre[u]){ gEdge[gPath[u]].val -= f; gEdge[gPath[u]^1].val += f; } } if(flow != nd){ return -1; } return cost; } void addedge(int u,int v,int val,int cost){ gEdge[gcount].to = v; gEdge[gcount].val = val; gEdge[gcount].cost = cost; gEdge[gcount].nxt = h[u]; h[u] = gcount++; gEdge[gcount].to = u; gEdge[gcount].val = 0; gEdge[gcount].cost = -cost; gEdge[gcount].nxt = h[v]; h[v] = gcount++; } int main(){ while(~scanf("%d%d%d", &n, &m, &k) && n+m+k){ memset(req,0,sizeof(req)); memset(hav,0,sizeof(hav)); memset(res, 0 ,sizeof(res)); for(int i=1; i<=n; i++) for(int j=1; j<=k; j++) scanf("%d", &req[i][j]),res[j] += req[i][j]; for(int i=1; i<=m; i++) for(int j=1; j<=k; j++) { scanf("%d", &hav[i][j]); } for(int i=1; i<=k; i++) for(int t = 1; t<=n; t++) for(int j=1; j<=m; j++) { scanf("%d",&fy[i][j][t]); } int ans = 0,kk = k,flag = 1; int s = 0,t = n+m+1; for(int kind = 1; kind <= k; kind ++){ memset(h,-1,sizeof(h)); gcount = 0; for(int i=1; i<=m; i++) addedge(s,i,hav[i][kind],0); for(int i=1; i<=m; i++) for(int j=1; j<=n; j++){ addedge(i,m+j,inf,fy[kind][i][j]); } for(int i=1; i<=n; i++){ addedge(m+i,t, req[i][kind],0); } int tmp = MinCostFlow(s,t,res[kind]); if(tmp == -1) flag = 0; else ans += tmp; } if(flag == 0)puts("-1"); else printf("%d\n", ans); } return 0; }
skr