HDU - 6126 Give out candies
生活随笔
收集整理的這篇文章主要介紹了
HDU - 6126 Give out candies
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Give out candies
題解:
第一次遇見這樣處理的網絡流模型。
將問題轉換成最小割問題。
具體的題解參考自:傳送門
先將每個人的拆成m個人。
然后s向第1人連邊流量為inf。第i個人向第i+1個人連邊,流量為 3000 - w。 將t視為每組的第m+1個人。
接來下是約束關系的建邊, x, y ,z。
如果x小朋友拿了j個糖果,則y小朋友拿的糖果至少為y-z。
則在x的拆點和y對應的拆點之間建立一條流量為inf的邊。
最后跑完最大流的時候。
如果ans >= inf 則說明沒有最小割, 無解。
否則 ans = n * 3000 - ans.
?
代碼:
#include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); #define LL long long #define ULL unsigned LL #define fi first #define se second #define pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lch(x) tr[x].son[0] #define rch(x) tr[x].son[1] #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) typedef pair<int,int> pll; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const LL INF = 0x3f3f3f3f3f3f3f3f; const LL _INF = 0xc0c0c0c0c0c0c0c0; const LL mod = (int)1e9+7; const int N = 3000; const int M = 2e5; int head[N], deep[N], cur[N]; int w[M], to[M], nx[M]; int tot; void add(int u, int v, int val){w[tot] = val; to[tot] = v;nx[tot] = head[u]; head[u] = tot++;w[tot] = 0; to[tot] = u;nx[tot] = head[v]; head[v] = tot++; } int bfs(int s, int t){queue<int> q;memset(deep, 0, sizeof(deep)); // for(int i = s; i <= t; ++i) deep[i]=0; q.push(s);deep[s] = 1;while(!q.empty()){int u = q.front();q.pop();for(int i = head[u]; ~i; i = nx[i]){if(w[i] > 0 && deep[to[i]] == 0){deep[to[i]] = deep[u] + 1;q.push(to[i]);}}}return deep[t] > 0; } LL Dfs(int u, int t, LL flow){if(u == t) return flow;for(int &i = cur[u]; ~i; i = nx[i]){if(deep[u]+1 == deep[to[i]] && w[i] > 0){LL di = Dfs(to[i], t, min(1ll*w[i], flow));if(di > 0){w[i] -= di, w[i^1] += di;return di;}}}return 0; }LL Dinic(int s, int t){LL ans = 0, tmp;while(bfs(s, t)){for(int i = 0; i <= t; i++) cur[i] = head[i];while(tmp = Dfs(s, t, inf)) ans += tmp;}return ans; } void init(){memset(head, -1, sizeof(head));tot = 0; } int main(){int T;scanf("%d", &T);for(int _cas = 1; _cas <= T; ++_cas){init();int n, m, k, s, t;scanf("%d%d%d", &n, &m, &k);s = 0, t = n * m + 1; // assert(t > 300);for(int i = 0, v; i < n; ++i){add(s, i*m+1, inf);for(int j = 1; j <= m; ++j){scanf("%d", &v);if(j == m) add(i*m+j, t, 3000-v);else add(i*m+j, i*m+j+1, 3000-v);}}for(int i = 1, x,y,z; i <= k; ++i){scanf("%d%d%d", &x, &y, &z);for(int j = 1; j <= m; ++j){if(j-z < 1) add(x*m-m+j, s, inf);else if(j-z > m) add(x*m-m+j, t, inf);else add((x-1)*m+j, (y-1)*m+j-z, inf);}}LL ans = Dinic(s, t);if(ans >= inf) puts("-1");else printf("%lld\n", 3000*n-ans);}return 0; } View Code?
轉載于:https://www.cnblogs.com/MingSD/p/11130696.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的HDU - 6126 Give out candies的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图解AODV
- 下一篇: 台湾电脑品牌(香港人眼中的台湾)