hdu4280 Island Transport 最大流 ISAP模板題

Island Transport

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 8005    Accepted Submission(s): 2525


Problem Description
  In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships.
  You have a transportation company there. Some routes are opened for passengers. Each route is a straight line connecting two different islands, and it is bidirectional. Within an hour, a route can transport a certain number of passengers in one direction. For safety, no two routes are cross or overlap and no routes will pass an island except the departing island and the arriving island. Each island can be treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from south to north.
  The transport capacity is important to you. Suppose many passengers depart from the westernmost island and would like to arrive at the easternmost island, the maximum number of passengers arrive at the latter within every hour is the transport capacity. Please calculate it.
 

Input
  The first line contains one integer T (1<=T<=20), the number of test cases.
  Then T test cases follow. The first line of each test case contains two integers N and M (2<=N,M<=100000), the number of islands and the number of routes. Islands are number from 1 to N.
  Then N lines follow. Each line contain two integers, the X and Y coordinate of an island. The K-th line in the N lines describes the island K. The absolute values of all the coordinates are no more than 100000.
  Then M lines follow. Each line contains three integers I1, I2 (1<=I1,I2<=N) and C (1<=C<=10000) . It means there is a route connecting island I1 and island I2, and it can transport C passengers in one direction within an hour.
  It is guaranteed that the routes obey the rules described above. There is only one island is westernmost and only one island is easternmost. No two islands would have the same coordinates. Each island can go to any other island by the routes.
 

Output
  For each test case, output an integer in one line, the transport capacity.
 

Sample Input
2 5 7 3 3 3 0 3 1 0 0 4 5 1 3 3 2 3 4 2 4 3 1 5 6 4 5 3 1 4 4 3 4 2 6 7 -1 -1 0 1 0 2 1 0 1 1 2 3 1 2 1 2 3 6 4 5 5 5 6 3 1 4 6 2 5 5 3 6 4
 

Sample Output
9 6


#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <iostream>
#include <cstdlib>

using namespace std;

const int maxn = 100005, INF = 0x7fffffff;

int s;         // 源點
int t;         // 匯點
int p[maxn];   // 可增廣路上的上一條弧的編號
int num[maxn]; // 和 t 的最短距離等於 i 的節點數量
int cur[maxn]; // 當前弧下標
int d[maxn];   // 殘量網絡中節點 i 到匯點 t 的最短距離
bool vis[maxn];
int T, n, m;

struct Edge {
	int from, to, cap, flow;
	Edge() {}
	Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow) {}
};
vector<int> G[maxn];
vector<Edge> edges;

void AddEdge(int from, int to, int cap) {
	edges.push_back(Edge(from, to, cap, 0));
	edges.push_back(Edge(to, from, 0, 0));
	int len = edges.size();
	G[from].push_back(len - 2);
	G[to].push_back(len - 1);
}

// 預處理, 反向 BFS 構造 d 數組
bool BFS() {
	memset(vis, 0, sizeof(vis));
	queue<int> Q;
	Q.push(t);
	vis[t] = 1;
	d[t] = 0;
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		for (int i = 0; i < G[u].size(); i++) {
			Edge &e = edges[G[u][i] ^ 1];
			if (!vis[e.from] && e.cap > e.flow) {
				vis[e.from] = true;
				d[e.from] = d[u] + 1;
				Q.push(e.from);
			}
		}
	}
	return vis[s];
}

// 增廣
int Augment()
{
	int x = t, a = INF;
	// 從匯點到源點通過 p 追蹤增廣路徑, a 爲一路上最小的殘量
	while (x != s) {
		Edge &e = edges[p[x]];
		a = min(a, e.cap - e.flow);
		x = edges[p[x]].from;
	}
	x = t;
	// 從匯點到源點更新流量
	while (x != s) {
		edges[p[x]].flow += a;
		edges[p[x] ^ 1].flow -= a;
		x = edges[p[x]].from;
	}
	return a;
}

int Maxflow()
{
	int flow = 0;
	BFS();
	memset(num, 0, sizeof(num));
	for (int i = 0; i < n; i++) num[d[i]]++;
	int x = s;
	memset(cur, 0, sizeof(cur));
	while (d[s] < n) {
		if (x == t) {
			flow += Augment();
			x = s;
		}
		bool advanced = false;
		for (int& i = cur[x]; i < G[x].size(); i++) {  //int& i,當前弧優化
			Edge& e = edges[G[x][i]];
			if (e.cap > e.flow && d[x] == d[e.to] + 1) {
				advanced = true;
				p[e.to] = G[x][i];
				cur[x] = i;
				x = e.to;
				break;
			}
		}
		if (!advanced) { // retreat
			int m = n - 1;
			for (int i = 0; i < G[x].size(); i++) {
				if (edges[G[x][i]].cap > edges[G[x][i]].flow) {
					m = min(m, d[edges[G[x][i]].to]);
				}
			}
			if (--num[d[x]] == 0) break; // gap優化
			num[d[x] = m + 1]++;
			cur[x] = 0;
			if (x != s) x = edges[p[x]].from;
		}
	}
	return flow;
}

int main()
{
	cin >> T;
	while (T--) {
		scanf("%d%d", &n, &m);
		edges.clear();
		memset(vis, 0, sizeof(vis));
		for (int i = 1; i <= n; i++) {
			G[i].clear();
		}
		int wm = INF, em = -INF, wi, ei;
		for (int i = 1; i <= n; i++) {
			int x, y;
			scanf("%d%d", &x, &y);
			if (x < wm) {
				wi = i;
				wm = x;
			}
			if (x > em) {
				ei = i;
				em = x;
			}
		}
		for (int i = 1; i <= m; i++) {
			int u, v, c;
			scanf("%d%d%d", &u, &v, &c);
			AddEdge(u, v, c);
			AddEdge(v, u, c);
		}
		s = wi; t = ei;
		printf("%d\n", Maxflow());
	}
	return 0;
}




Dinic超時代碼:

#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <iostream>
#include <cstdlib>
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const int maxn = 100005, INF = 0x7fffffff;
int T, n, m, wm, em, wi, ei;

struct Edge {
	int from, to, cap, flow;
	Edge() {}
	Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow) {}
};

vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn], cur[maxn];

struct Dinic {
	int len, s, t;
	void Init() {
		edges.clear();
		for (int i = 1; i <= n; i++) {
			G[i].clear();
		}
		memset(d, 0, sizeof(d));
	}

	void AddEdge(int from, int to, int cap) {
		edges.push_back(Edge(from, to, cap, 0));
		edges.push_back(Edge(to, from, 0, 0));
		len = edges.size();
		G[from].push_back(len - 2);
		G[to].push_back(len - 1);
	}

	bool BFS() {
		memset(vis, 0, sizeof(vis));
		queue<int> Q;
		Q.push(s);
		d[s] = 0;
		vis[s] = 1;
		while (!Q.empty()) {
			int x = Q.front(); Q.pop();
			for (int i = 0; i < G[x].size(); i++) {
				Edge& e = edges[G[x][i]];
				if (!vis[e.to] && e.cap > e.flow) {
					vis[e.to] = 1;
					d[e.to] = d[x] + 1;
					Q.push(e.to);
				}
			}
		}
		return vis[t];
	}

	int DFS(int x, int a) {
		if (x == t || a == 0) return a;
		int flow = 0, f;
		for (int& i = cur[x]; i < G[x].size(); i++) {
			Edge& e = edges[G[x][i]];
			if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[G[x][i] ^ 1].flow -= f;
				flow += f;
				a -= f;
				if (a == 0) break;
			}
		}
		return flow;
	}

	int Maxflow(int s, int t) {
		this->s = s; this->t = t;
		int flow = 0;
		while (BFS()) {
			memset(cur, 0, sizeof(cur));
			flow += DFS(s, INF);
		}
		return flow;
	}
};

int main()
{
	Dinic d;
	cin >> T;
	while (T--) {
		scanf("%d%d", &n, &m);
		d.Init();
		wm = INF, em = -INF;
		for (int i = 1; i <= n; i++) {
			int x, y;
			scanf("%d%d", &x, &y);
			if (x < wm) {
				wi = i;
				wm = x;
			}
			if (x > em) {
				ei = i;
				em = x;
			}
		}
		for (int i = 1; i <= m; i++) {
			int u, v, c;
			scanf("%d%d%d", &u, &v, &c);
			d.AddEdge(u, v, c);
			d.AddEdge(v, u, c);
		}
		printf("%d\n", d.Maxflow(wi, ei));
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章