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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ-1459 Power Network 网络流

發布時間:2025/3/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ-1459 Power Network 网络流 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:給定一些散列的源點會匯點,求解網絡流。

代碼如下:

#include <cstdlib> #include <cstring> #include <cstdio> #include <iostream> #include <algorithm> using namespace std;const int INF = 0x3fffffff; const int SS = 105, TT = 106; int n, np, nc, m;struct Edge {int v, c, next; }; Edge e[100000]; int idx, head[110]; int front, tail, que[110]; int lv[110];void insert(int a, int b, int c) {e[idx].v = b, e[idx].c = c;e[idx].next = head[a];head[a] = idx++; }void read_3(int &a, int &b, int &c) {char ch;while (ch = getchar(), ch != '(') ;scanf("%d,%d)%d", &a, &b, &c); // printf("%d %d %d\n", a, b, c); }void read_2(int &a, int &c) {char ch;while (ch = getchar(), ch != '(') ;scanf("%d)%d", &a, &c); // printf("%d %d\n", a, c); }bool bfs() {memset(lv, 0xff, sizeof (lv));front = tail = 0;lv[SS] = 0;que[tail++] = SS;while (front < tail) {int u = que[front++];for (int i = head[u]; i != -1; i = e[i].next) {if (!(~lv[e[i].v]) && e[i].c) {lv[e[i].v] = lv[u] + 1;if (e[i].v == TT) return true;que[tail++] = e[i].v;}}}return ~lv[TT]; }int dfs(int u, int sup) {if (u == TT) return sup;int tf = 0, f;for (int i = head[u]; i != -1; i = e[i].next) {if (lv[u]+1==lv[e[i].v] && e[i].c && (f=dfs(e[i].v, min(e[i].c, sup-tf)))) {tf += f;e[i].c -= f, e[i^1].c += f;if (tf == sup) return sup; }}if (!tf) lv[u] = -1;return tf; }int dinic() {int ret = 0;while (bfs()) {ret += dfs(SS, INF);// printf("ret = %d\n", ret); }return ret; }int main() {int a, b, c;while (scanf("%d %d %d %d", &n, &np, &nc, &m) != EOF) {idx = 0;memset(head, 0xff, sizeof (head));for (int i = 0; i < m; ++i) { // m條邊 read_3(a, b, c);insert(a, b, c);insert(b, a, 0);}for (int i = 0; i < np; ++i) {read_2(a, c);insert(SS, a, c);insert(a, SS, 0);}for (int i = 0; i < nc; ++i) {read_2(a, c);insert(a, TT, c);insert(TT, a, 0);}printf("%d\n", dinic());}return 0; }

?

轉載于:https://www.cnblogs.com/Lyush/archive/2013/04/30/3052382.html

總結

以上是生活随笔為你收集整理的POJ-1459 Power Network 网络流的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。