日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU - 6126 Give out candies

發(fā)布時(shí)間:2023/12/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU - 6126 Give out candies 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Give out candies

題解:

第一次遇見這樣處理的網(wǎng)絡(luò)流模型。

將問題轉(zhuǎn)換成最小割問題。

具體的題解參考自:傳送門

先將每個(gè)人的拆成m個(gè)人。

然后s向第1人連邊流量為inf。第i個(gè)人向第i+1個(gè)人連邊,流量為 3000 - w。 將t視為每組的第m+1個(gè)人。

接來下是約束關(guān)系的建邊, x, y ,z。

如果x小朋友拿了j個(gè)糖果,則y小朋友拿的糖果至少為y-z。

則在x的拆點(diǎn)和y對(duì)應(yīng)的拆點(diǎn)之間建立一條流量為inf的邊。

最后跑完最大流的時(shí)候。

如果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

?

轉(zhuǎn)載于:https://www.cnblogs.com/MingSD/p/11130696.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的HDU - 6126 Give out candies的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。