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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

POJ 3436 -- ACM Computer Factory(最大流,建图)

發(fā)布時間:2024/4/18 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3436 -- ACM Computer Factory(最大流,建图) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接

Description

As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory.Every ACM computer consists of P parts. When all these parts are present, the computer is ready and can be shipped to one of the numerous ACM contests.Computer manufacturing is fully automated by using N various machines. Each machine removes some parts from a half-finished computer and adds some new parts (removing of parts is sometimes necessary as the parts cannot be added to a computer in arbitrary order). Each machine is described by its performance (measured in computers per hour), input and output specification.Input specification describes which parts must be present in a half-finished computer for the machine to be able to operate on it. The specification is a set of P numbers 0, 1 or 2 (one number for each part), where 0 means that corresponding part must not be present, 1 — the part is required, 2 — presence of the part doesn’t matter.Output specification describes the result of the operation, and is a set of P numbers 0 or 1, where 0 means that the part is absent, 1 — the part is present.The machines are connected by very fast production lines so that delivery time is negligibly small compared to production time.After many years of operation the overall performance of the ACM Computer Factory became insufficient for satisfying the growing contest needs. That is why ACM directorate decided to upgrade the factory.As different machines were installed in different time periods, they were often not optimally connected to the existing factory machines. It was noted that the easiest way to upgrade the factory is to rearrange production lines. ACM directorate decided to entrust you with solving this problem.

Input

Input file contains integers P N, then N descriptions of the machines. The description of ith machine is represented as by 2 P + 1 integers Qi Si,1 Si,2…Si,P Di,1 Di,2…Di,P, where Qi specifies performance, Si,j — input specification for part j, Di,k — output specification for part k.Constraints1 ≤ P ≤ 10, 1 ≤ N ≤ 50, 1 ≤ Qi ≤ 10000

Output

Output the maximum possible overall performance, then M — number of connections that must be made, then M descriptions of the connections. Each connection between machines A and B must be described by three positive numbers A B W, where W is the number of computers delivered from A to B per hour.If several solutions exist, output any of them.

Sample Input

Sample input 1 3 4 15 0 0 0 0 1 0 10 0 0 0 0 1 1 30 0 1 2 1 1 1 3 0 2 1 1 1 1 Sample input 2 3 5 5 0 0 0 0 1 0 100 0 1 0 1 0 1 3 0 1 0 1 1 0 1 1 0 1 1 1 0 300 1 1 2 1 1 1 Sample input 3 2 2 100 0 0 1 0 200 0 1 1 1

Sample Output

Sample output 1 25 2 1 3 15 2 3 10 Sample output 2 4 5 1 3 3 3 5 3 1 2 1 2 4 1 4 5 1 Sample output 3 0 0

AC

  • 兩個版本拆點和不拆點
  • 網(wǎng)上很多都是拆點,我這里是不拆點的(如果WA,就多交幾次。Special Judge的鍋)
  • 建圖:
  • 如果一個機(jī)器的輸入端沒有1,那么這個機(jī)器就是開始的機(jī)器,讓這個機(jī)器和源點相連
  • 如果一個機(jī)器的輸出端全是1,那么這個機(jī)器就是結(jié)束的機(jī)器,讓這個機(jī)器和匯點相連
  • 然后遍歷每個機(jī)器,如果一個機(jī)器的輸出端符合另一臺的輸入端,那么就可以建邊,權(quán)值為輸出端的最大產(chǎn)量
  • 最后從源點到匯點跑一個最大流
// 不拆點 #include <iostream> #include <stdio.h> #include <map> #include <vector> #include <queue> #include <algorithm> #include <cmath> #define N 300005 #include <cstring> #define ll long long #define P pair<int, int> #define mk make_pair using namespace std;int a[105][105], a1[105][105], g[105][55], pre[105]; bool vis[105], viss[105]; int inf = 0x3f3f3f3f; int n, m; struct ac{int x, y, c; }; // EK最大流 bool bfs(int s, int e) {memset(pre, -1, sizeof(pre));memset(vis, false, sizeof(vis));vis[s] = true;pre[s] = s;queue<int> que;que.push(s);while (!que.empty()) {int t = que.front();que.pop();for (int i = 0; i <= m + 1; ++i) {if (vis[i] || a[t][i] == 0) continue;vis[i] = true;pre[i] = t;if (i == e) return true;que.push(i);}}return false; } ll solve(int s, int e) {ll sum = 0;while (bfs(s, e)) {int MIN = inf;for (int i = e; i != s; i = pre[i]) {MIN = min(MIN, a[pre[i]][i]);}for (int i = e; i != s; i = pre[i]) {a[pre[i]][i] -= MIN;a[i][pre[i]] += MIN;}sum += MIN;}return sum; }int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifwhile (scanf("%d%d", &n, &m) != EOF) {memset(g, 0, sizeof(g));memset(a, 0, sizeof(a));memset(viss, false, sizeof(viss));for (int i = 1; i <= m; ++i) {for (int j = 0; j <= 2 * n; ++j) {scanf("%d", &g[i][j]);}}for (int i = 1; i <= m; ++i) {// 判斷輸入端是否全是0 int flag1 = 0;for (int j = 1; j <= n; ++j) {if (g[i][j] == 1) flag1 = 1;}if (!flag1) a[0][i] = g[i][0], viss[i] = true;// 判斷輸出端是否全唯1 int flag0 = 0;for (int j = n + 1; j <= 2 * n; ++j) {if (g[i][j] == 0) flag0 = 1;}if (!flag0) {a[i][m + 1] = g[i][0];continue;} // 將滿足輸出與輸入關(guān)系對應(yīng)的點建邊 for (int j = 1; j <= m; ++j) {// 如果要匹配的點是起點,直接跳過(加快速度) if (i == j || viss[j]) continue;int flag = 1;for (int k = 1; k <= n; ++k) {if (g[i][k + n] + g[j][k] == 1)flag = 0;}if (flag) a[i][j] = g[i][0];} }// 將圖備份,在最后選擇出最大流經(jīng)過的邊 for (int i = 0; i <= m + 1; ++i) {for (int j = 0; j <= m + 1; ++j) {a1[i][j] = a[i][j];}}// 最大流 ll ans = solve(0, m + 1);// 存最大流 每條邊的情況 vector<ac> v;for (int i = 1; i <= m; ++i) {for (int j = 1; j <= m; ++j) {// a1是備份圖,如果跑完最大流之后,邊的權(quán)值小于原來的權(quán)值,那么這個邊就是最大流的一部分 if (a1[i][j] > a[i][j]) {v.push_back((ac){i, j, a1[i][j] - a[i][j]});}}} int len = v.size();printf("%lld %d\n", ans, len);for (int i = 0; i < len; ++i) {printf("%d %d", v[i].x, v[i].y);printf(" %d\n", v[i].c);}}return 0; }
  • 將點與自己連一個產(chǎn)量
  • 源點和點的全為零的輸入端,匯點和全為1的輸出端設(shè)為正無窮
// 拆點 #include <iostream> #include <stdio.h> #include <map> #include <vector> #include <queue> #include <algorithm> #include <cmath> #define N 300005 #include <cstring> #define ll long long #define P pair<int, int> #define mk make_pair using namespace std;int a[205][205], a1[205][205], g[205][105], pre[205]; bool vis[205], viss[205]; int inf = 0x3f3f3f3f; int n, m; struct ac{int x, y, c; }; // EK最大流 bool bfs(int s, int e) {memset(pre, -1, sizeof(pre));memset(vis, false, sizeof(vis));vis[s] = true;pre[s] = s;queue<int> que;que.push(s);while (!que.empty()) {int t = que.front();que.pop();for (int i = 0; i <= 2 * m + 1; ++i) {if (vis[i] || a[t][i] == 0) continue;vis[i] = true;pre[i] = t;if (i == e) return true;que.push(i);}}return false; } ll solve(int s, int e) {ll sum = 0;while (bfs(s, e)) {int MIN = inf;for (int i = e; i != s; i = pre[i]) {MIN = min(MIN, a[pre[i]][i]);}for (int i = e; i != s; i = pre[i]) {a[pre[i]][i] -= MIN;a[i][pre[i]] += MIN;}sum += MIN;}return sum; }int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifwhile (scanf("%d%d", &n, &m) != EOF) {memset(g, 0, sizeof(g));memset(a, 0, sizeof(a));memset(viss, false, sizeof(viss));for (int i = 1; i <= m; ++i) {for (int j = 0; j <= 2 * n; ++j) {scanf("%d", &g[i][j]);}}// 拆點建邊 for (int i = 1; i <= m; ++i) {a[2 * i - 1][2 * i] = g[i][0];} for (int i = 1; i <= m; ++i) {// 判斷輸入端是否全是0 int flag1 = 0;for (int j = 1; j <= n; ++j) {if (g[i][j] == 1) flag1 = 1;}if (!flag1) a[0][i * 2 - 1] = inf, viss[i] = true;// 判斷輸出端是否全唯1 int flag0 = 0;for (int j = n + 1; j <= 2 * n; ++j) {if (g[i][j] == 0) flag0 = 1;}if (!flag0) {a[i * 2][2 * m + 1] = inf;continue;} // 將滿足輸出與輸入關(guān)系對應(yīng)的點建邊 for (int j = 1; j <= m; ++j) {// 如果要匹配的點是起點,直接跳過(加快速度) if (i == j || viss[j]) continue;int flag = 1;for (int k = 1; k <= n; ++k) {if (g[i][k + n] + g[j][k] == 1)flag = 0;}if (flag) a[i * 2][j * 2 - 1] = min(g[i][0], g[j][0]);} }// 將圖備份,在最后選擇出最大流經(jīng)過的邊 for (int i = 0; i <= 2 * m + 1; ++i) {for (int j = 0; j <= 2 * m + 1; ++j) {a1[i][j] = a[i][j];}}// 最大流 ll ans = solve(0, 2 * m + 1);// 存最大流 每條邊的情況 vector<ac> v;for (int i = 1; i <= 2 * m; ++i) {for (int j = 1; j <= 2 * m; ++j) {if ((i + 1) / 2 == (j + 1) / 2) continue;// a1是備份圖,如果跑完最大流之后,邊的權(quán)值小于原來的權(quán)值,那么這個邊就是最大流的一部分 if (a1[i][j] > a[i][j]) {v.push_back((ac){(i + 1) / 2, (j + 1) / 2, a1[i][j] - a[i][j]});}}} int len = v.size();printf("%lld %d\n", ans, len);for (int i = 0; i < len; ++i) {printf("%d %d", v[i].x, v[i].y);printf(" %d\n", v[i].c);}}return 0; }

總結(jié)

以上是生活随笔為你收集整理的POJ 3436 -- ACM Computer Factory(最大流,建图)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: av资源在线看 | 九九热视频这里只有精品 | 九色论坛 | 老司机精品福利导航 | 粉嫩av一区二区白浆 | 在线黄网| 欧美色交 | 亚洲网站在线免费观看 | 欧美日韩在线免费观看视频 | www.天天射 | 欧美亚洲激情视频 | 婷婷色婷婷开心五月四房播播 | 日本二区在线观看 | 久国产| 亚洲精品理论 | 无码人妻精品一区二区三区99日韩 | 北条麻妃av在线播放 | 亚洲人女屁股眼交6 | 成人在线观看免费 | 秋霞成人午夜伦在线观看 | 国产欧美日韩专区发布 | 91综合视频| 国产女主播在线播放 | 欧美激情中文字幕 | 91综合久久 | 中文欧美日韩 | 日本欧美韩国国产精品 | 亚洲熟女乱综合一区二区三区 | av免费在线观 | 日本做爰高潮又黄又爽 | 日韩视频免费观看高清 | 青青插 | 欧美人体视频一区二区三区 | 少妇一边呻吟一边说使劲视频 | 色吧av色av| 久久九九精品视频 | 青草成人免费视频 | 日韩 欧美 亚洲 国产 | 成年人视频网 | av丝袜天堂| 狠狠操婷婷 | 欧美亚洲少妇 | 国产清纯白嫩初高中在线观看性色 | 亚洲人妻电影一区 | 朴麦妮原版视频高清资源 | 中文字幕一区二区三区手机版 | 69精品一区二区 | 亚洲成人一区二区 | 日韩性欧美 | 日本大尺度做爰呻吟舌吻 | 波多野结衣精品在线 | 日韩视频网 | 日本福利一区二区三区 | 国产美女一区二区三区 | 性――交――性――乱睡觉 | 福利久久久 | 岛国免费av | 91视频黄色 | 日韩在线网 | 超碰人人爱人人 | 日韩精品极品视频在线观看免费 | 免费极品av一视觉盛宴 | 伊人久久久久久久久久久久 | 欧日韩在线视频 | 91调教打屁股xxxx网站 | 久久无码视频一区 | 精品人妻一区二区三区免费 | 日韩av综合网 | 国产精品午夜影院 | 蜜桃色av| 男生操女生逼逼 | 一区二区三区日 | 91在线观看免费视频 | 99久久99久久精品免费看蜜桃 | 国产性猛交╳xxx乱大交一区 | eeuss一区二区三区 | 天天躁日日躁狠狠躁免费麻豆 | 亚洲av无码一区二区三区人妖 | 成人综合在线视频 | 丰满人妻一区二区三区无码av | 国产成人麻豆免费观看 | 日韩av在线看免费观看 | 白丝动漫美女 | 日韩精品一区二区视频 | 国产不卡一区二区视频 | 无码人妻精品一区二区三 | 久热久色 | 一级特黄欧美 | 中文字幕人成人乱码亚洲电影 | 五月天视频 | 人妻巨大乳hd免费看 | 午夜影视剧场 | 中文字幕在线观看视频免费 | 亚洲AV无码成人精品区麻豆 | 激情文学综合网 | 女同一区二区三区 | 喷潮在线观看 | 又黄又刺激的视频 | 永久精品网站 |