程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 3157 Crazy Circuits(有源匯上下界最小流)

HDU 3157 Crazy Circuits(有源匯上下界最小流)

編輯:C++入門知識

HDU 3157 Crazy Circuits(有源匯上下界最小流)


HDU 3157 Crazy Circuits

題目鏈接

題意:一個電路板,上面有N個接線柱(標號1~N),還有兩個電源接線柱 + -,給出一些線路,每個線路有一個下限值求一個可以讓所有部件正常工作的總電流 沒有則輸出impossible

思路:
有源匯有上下界求最小流,建模方法為:
按無源匯先建圖,跑超級源匯ss->tt一次,然後加入t->s,容量INF的邊,在跑一次ss->tt,如果是滿流,就有解,解為t->s邊的當前流量

順帶寫個最大流的,最大流就先把t->s加入直接跑一下,t->s的流量就是了

代碼:

#include 
#include 
#include 
#include 
using namespace std;

const int MAXNODE = 65;
const int MAXEDGE = 10005;

typedef int Type;
const Type INF = 0x3f3f3f3f;

struct Edge {
	int u, v;
	Type cap, flow;
	Edge() {}
	Edge(int u, int v, Type cap, Type flow) {
		this->u = u;
		this->v = v;
		this->cap = cap;
		this->flow = flow;
	}
};

struct Dinic {
	int n, m, s, t;
	Edge edges[MAXEDGE];
	int first[MAXNODE];
	int next[MAXEDGE];
	bool vis[MAXNODE];
	Type d[MAXNODE];
	Type flow;
	int cur[MAXNODE];

	void init(int n) {
		this->n = n;
		memset(first, -1, sizeof(first));
		m = 0;
		flow = 0;
	}
	void add_Edge(int u, int v, Type cap) {
		edges[m] = Edge(u, v, cap, 0);
		next[m] = first[u];
		first[u] = m++;
		edges[m] = Edge(v, u, 0, 0);
		next[m] = first[v];
		first[v] = m++;
	}

	bool bfs() {
		memset(vis, false, sizeof(vis));
		queue Q;
		Q.push(s);
		d[s] = 0;
		vis[s] = true;
		while (!Q.empty()) {
			int u = Q.front(); Q.pop();
			for (int i = first[u]; i != -1; i = next[i]) {
				Edge& e = edges[i];
				if (!vis[e.v] && e.cap > e.flow) {
					vis[e.v] = true;
					d[e.v] = d[u] + 1;
					Q.push(e.v);
				}
			}
		}
		return vis[t];
	}

	Type dfs(int u, Type a) {
		if (u == t || a == 0) return a;
		Type flow = 0, f;
		for (int &i = cur[u]; i != -1; i = next[i]) {
			Edge& e = edges[i];
			if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[i^1].flow -= f;
				flow += f;
				a -= f;
				if (a == 0) break;
			}
		}
		return flow;
	}

	Type Maxflow(int s, int t) {
		this->s = s; this->t = t;
		while (bfs()) {
			for (int i = 0; i < n; i++)
				cur[i] = first[i];
			flow += dfs(s, INF);
		}
		return flow;
	}

	bool judge(int s) {
		for (int i = first[s]; i + 1; i = next[i])
			if (edges[i].flow != edges[i].cap) return false;
		return true;
	}
} gao;

const int N = 65;

int n, m, s, t, ss, tt, du[N];

char c[15];

int main() {
	while (~scanf("%d%d", &n, &m) && n || m) {
		gao.init(n + 4);
		s = 0; t = n + 1; ss = n + 2; tt = n + 3;
		int u, v, w;
		memset(du, 0, sizeof(du));
		while (m--) {
			scanf("%s", c);
			if (c[0] == '+') u = s;
			else sscanf(c, "%d", &u);
			scanf("%s", c);
			if (c[0] == '-') v = t;
			else sscanf(c, "%d", &v);
			scanf("%d", &w);
			gao.add_Edge(u, v, INF);
			du[u] -= w;
			du[v] += w;
		}
		for (int i = s; i <= t; i++) {
			if (du[i] > 0) gao.add_Edge(ss, i, du[i]);
			if (du[i] < 0) gao.add_Edge(i, tt, -du[i]);
		}
		gao.Maxflow(ss, tt);
		gao.add_Edge(t, s, INF);
		gao.Maxflow(ss, tt);
		if (!gao.judge(ss)) printf("impossible\n");
		else printf("%d\n", gao.edges[gao.m - 2].flow);
	}
	return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved