日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 色www国产亚洲阿娇 自拍一区在线 | 中国黄色录像一级片 | 国产永久视频 | 国模吧无码一区二区三区 | 成人区人妻精品一区二区不卡视频 | 中文字幕亚洲一区二区三区 | 久久黄色一级片 | 老熟妇毛茸茸 | 成人性色生活片 | 黄色大片在线播放 | 三级小视频在线观看 | 蜜桃在线一区二区三区 | 午夜国产视频 | www.九色| 成品短视频泡芙 | 国产专区自拍 | 在线黄色网 | 久久亚洲精少妇毛片午夜无码 | 456亚洲影视 | 国产日韩欧美精品一区二区 | 日韩一级影院 | 精品久久久免费 | 国产乱子伦视频一区二区三区 | 日韩毛片在线看 | www.成年人 | 一区二区三区在线免费观看视频 | 三级黄色小视频 | 午夜爽爽影院 | 超碰cc| 欧美午夜精品久久久久久浪潮 | 超碰婷婷 | 亚洲欧美日韩另类在线 | 国产最新自拍视频 | 麻豆传媒在线免费 | 永久免费精品 | 亚洲欧美国产日韩精品 | 欧美黄页 | 美女大bxxxxn内射 | 欧美视频自拍偷拍 | 久久天天干 | 黑鬼巨鞭白妞冒白浆 | 国产精品一区二区在线播放 | 亚洲一区免费 | 少妇人妻偷人精品无码视频新浪 | 天天看天天做 | 亚洲jizzjizz日本少妇 | 神马影院午夜伦理片 | ass亚洲熟妇毛耸耸pics | 亚洲美女啪啪 | 亚洲一区二区偷拍 | 日韩欧美a级片 | av观看网址 | 亚洲欧美另类自拍 | 久久久无码一区二区三区 | 日韩三级视频在线播放 | aa级黄色片 | 97国产高清 | 国产综合色视频 | 日韩欧美国产电影 | 熟女国产精品一区二区三 | 中文精品在线观看 | 亚洲黄色一区二区三区 | 国产精品资源网站 | 婷婷tv| 无人码人妻一区二区三区免费 | 欧美午夜精品久久久久久浪潮 | 久久精品五月天 | 日韩一级中文字幕 | 国产乱码一区二区三区在线观看 | 日韩脚交footjobhd | 久久偷看各类女兵18女厕嘘嘘 | 国产小视频在线看 | 天堂av免费看 | 少妇人妻一区二区三区 | 日韩中文字幕视频在线观看 | 久久看毛片 | 国产a三级| 超碰麻豆 | 亚洲国产精品无码久久 | 欧美日韩一区二区电影 | 99在线无码精品入口 | 韩国成人在线 | 亚洲精品久久夜色撩人男男小说 | 成人午夜淫片100集 伊人久久国产 | 亚洲性猛交富婆 | www.av免费 | 亚洲精品污 | 国产美女www爽爽爽 www.国产毛片 | 日日爱夜夜操 | 久久精品国产亚洲AV无码男同 | 天堂最新| 国产精品亚洲综合 | 亚洲欧洲中文字幕 | 老司机午夜精品视频 | 超碰伊人久久 | 五月激情视频 | 亚洲激情二区 | 在线播放视频高清在线观看 | 亚洲婷婷网 |