HDU 3035 War(对偶图求最小割)

HDU 3035 War

题目链接

题意:根据图那样,给定一个网络,要求阻断s到t,需要炸边的最小代价

思路:显然的最小割,但是也显然的直接建图强行网络流会超时,这题要利用平面图求最小割的方法,把每一块当成一个点,共有边连边,然后每一个路径就是一个割,然后最短路就是最小割了

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
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<HeapNode> 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值