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

HDU 3035 War(對偶圖求最小割)

編輯:C++入門知識

HDU 3035 War(對偶圖求最小割)


HDU 3035 War

題目鏈接

題意:根據圖那樣,給定一個網絡,要求阻斷s到t,需要炸邊的最小代價

思路:顯然的最小割,但是也顯然的直接建圖強行網絡流會超時,這題要利用平面圖求最小割的方法,把每一塊當成一個點,共有邊連邊,然後每一個路徑就是一個割,然後最短路就是最小割了

代碼:

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

const int MAXNODE = 1000005;
const int MAXEDGE = 3 * MAXNODE;

typedef int Type;
const Type INF = 0x3f3f3f3f;

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

struct HeapNode {
	Type d;
	int u;
	HeapNode() {}
	HeapNode(Type d, int u) {
		this->d = d;
		this->u = u;
	}
	bool operator < (const HeapNode& c) const {
		return d > c.d;
	}
};

struct Dijkstra {
	int n, m;
	Edge edges[MAXEDGE];
	int first[MAXNODE];
	int next[MAXEDGE];
	bool done[MAXNODE];
	Type d[MAXNODE];

	void init(int n) {
		this->n = n;
		memset(first, -1, sizeof(first));
		m = 0;
	}

	void add_Edge(int u, int v, Type dist) {
		edges[m] = Edge(u, v, dist);
		next[m] = first[u];
		first[u] = m++;
	}

	Type dijkstra(int s, int t) {
		priority_queue Q;
		for (int i = 0; i < n; i++) d[i] = INF;
		d[s] = 0;
		memset(done, false, sizeof(done));
		Q.push(HeapNode(0, s));
		while (!Q.empty()) {
			HeapNode x = Q.top(); Q.pop();
			int u = x.u;
			if (done[u]) continue;
			done[u] = true;
			for (int i = first[u]; i != -1; i = next[i]) {
				Edge& e = edges[i];
				if (d[e.v] > d[u] + e.dist) {
					d[e.v] = d[u] + e.dist;
					Q.push(HeapNode(d[e.v], e.v));
				}
			}
		}
		return d[t];
	}
} gao;

typedef long long ll;

int n, m;

int main() {
	while (~scanf("%d%d", &n, &m)) {
		int u, v, w;
		gao.init(n * m * 4 + 2);
		int s = n * m * 4, t = n * m * 4 + 1;
		for (int i = 0; i < (n + 1); i++) {
			for (int j = 0; j < m; j++) {
				scanf("%d", &w);
				u = (i - 1) * m + j + n * m;
				v = i * m + j;
				if (i == 0) u = t;
				if (i == n) v = s;
				gao.add_Edge(u, v, w);
				gao.add_Edge(v, u, w);
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < (m + 1); j++) {
				scanf("%d", &w);
				u = n * m * 3 + i * m + j - 1;
				v = n * m * 2 + i * m + j;
				if (j == 0) u = s;
				if (j == m) v = t;
				gao.add_Edge(u, v, w);
				gao.add_Edge(v, u, w);
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				scanf("%d", &w);
				u = i * m + j;
				v = n * m * 2 + i * m + j;
				gao.add_Edge(u, v, w);
				gao.add_Edge(v, u, w);
				scanf("%d", &w);
				v += n * m;
				gao.add_Edge(u, v, w);
				gao.add_Edge(v, u, w);
			}
			for (int j = 0; j < m; j++) {
				scanf("%d", &w);
				u = n * m + i * m + j;
				v = n * m * 2 + i * m + j;
				gao.add_Edge(u, v, w);
				gao.add_Edge(v, u, w);
				scanf("%d", &w);
				v += n * m;
				gao.add_Edge(u, v, w);
				gao.add_Edge(v, u, w);
			}
		}
		printf("%d\n", gao.dijkstra(s, t));
	}
	return 0;
}


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