https://www.luogu.org/problemnew/show/P1251
有一家酒店,酒店每天需要ri张桌布,桌布可以现买,p元。可以通过快洗店,等m天,f元。可以通过慢洗店,等n天,s元。问满足每天用布需求的最小费用
这道题拆点是要的,把一天拆成早上和晚上。比较精彩的是,把每天需要用ri张桌布分开来看,“早上需要有ri张脏布”,“晚上有ri张脏布”。翻译过来就是,早上向终点连ri容量的边,源点向晚上连ri容量的边。
然后又是三种情况的讨论,1)现买,源点向早上连费用为p的边。2)快洗店,晚上向+m天连费用为f的边。3)慢洗店,晚上向+n天连费用为s的边。
最后还要注意,由于可以留下晚上的脏布,所以每个晚上向下一个晚上连边。
#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> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \(‘?‘) > ⌒ヽ / へ\ / / \\ ? ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / ‘ノ ) L? */ 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 boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; 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; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 2009; struct E{ int v,w,cost; int nxt; }edge[10*maxn*maxn]; int n,gtot = 0; int head[10*maxn]; void addedge(int u,int v,int w,int cost){ edge[gtot].v = v; edge[gtot].w = w; edge[gtot].cost = cost; edge[gtot].nxt = head[u]; head[u] = gtot ++; edge[gtot].v = u; edge[gtot].w = 0; edge[gtot].cost = -1*cost; edge[gtot].nxt = head[v]; head[v] = gtot++; } int vis[maxn*10],dis[maxn*10],pre[maxn*10],path[maxn*10]; bool spfa(int s,int t){ memset(vis, 0, sizeof(vis)); memset(dis, inf, sizeof(dis)); memset(pre, -1, sizeof(pre)); queue<int>que; que.push(s); vis[s] = 1; dis[s] = 0; while(!que.empty()){ int u = que.front(); que.pop(); vis[u] = 0; for(int i=head[u]; ~i; i = edge[i].nxt){ int v = edge[i].v, w = edge[i].w, cost = edge[i].cost; if(w > 0 && dis[v] > dis[u] + cost){ dis[v] = dis[u] + cost; pre[v] = u; path[v] = i; if(vis[v] == 0){ que.push(v); vis[v] = 1; } } } } return pre[t] != -1; } ll solve(int s,int t){ ll flow = 0, cost = 0; while(spfa(s,t)){ int f = inf; for(int i=t; i!=s; i = pre[i]){ f = min(f, edge[path[i]].w); } flow += f; cost += 1ll*f * dis[t]; // cout<<f<<" "<<dis[t]<<endl; for(int i=t; i!=s; i = pre[i]){ edge[path[i]].w -= f; edge[path[i] ^ 1].w += f; } } return cost; } int main(){ memset(head, -1, sizeof(head)); scanf("%d", &n); int s = 0, t = n+n+1; for(int i=1; i<=n; i++) { int x; scanf("%d", &x); addedge(s, i+n, x, 0); addedge(i, t, x, 0); } int p,m,ff,nn,ss; scanf("%d%d%d%d%d", &p, &m, &ff, &nn, &ss); for(int i=1; i<=n; i++) addedge(s, i, inf, p); for(int i=1; i + m <=n; i++) addedge(i+n, i+m, inf, ff); for(int i=1; i + nn<=n; i++) addedge(i+n, i+nn, inf, ss); for(int i=1; i<n; i++) addedge(i+n, i+n+1, inf, 0); printf("%lld\n", solve(s, t)); return 0; }
原文:https://www.cnblogs.com/ckxkexing/p/10372291.html