hdu4280Island Transport

这题就是卡时间的,换成ISAP或时间更优的算法就好了。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2016
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 1e5 + 10;
int 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){}
};
struct ISAP{
	int p[maxn], num[maxn], cur[maxn], d[maxn];
	int s, t, n, m;
	bool vis[maxn];

	vector<int> G[maxn];
	vector<Edge> edges;

	void init(int n) {
		this->n = n;
		for (int i = 0;i <= n;++i) {
			G[i].clear();
			d[i] = INF;
		}
		edges.clear();
	}

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

	bool bfs() {
		memset(vis, false,sizeof vis);

		queue<int> que;
		d[t] = 0;
		vis[t] = true;
		que.push(t);

		while(!que.empty()) {
			int u = que.front();
			que.pop();

			for (int i = 0;i < G[u].size();++i) {
				Edge& e = edges[G[u][i]^1];
				if (e.cap > e.flow && !vis[e.from]) {
					vis[e.from] = true;
					d[e.from] = d[u] + 1;
					que.push(e.from);
				}
			}
		}
		return vis[s];
	}

	int Augment() {
		int u = t, flow = INF;
		while(u != s) {
			Edge& e = edges[p[u]];
			flow = min(flow, e.cap - e.flow);
			u = edges[p[u]].from;
		}

		u = t;
		while(u != s) {
			edges[p[u]].flow += flow;
			edges[p[u]^1].flow -= flow;
			u = edges[p[u]].from;
		}
		return flow;
	}

	int MaxFlow(int s,int t) {
		this->s = s,this->t = t;
		int ret = 0;
		bfs();
		if (d[s] >= n) return 0;

		memset(num, 0,sizeof num);
		memset(cur, 0,sizeof cur);
		for (int i = 0;i < n;++i) {
			if (d[i] < INF) num[d[i]]++;
		}
		int u = s;

		while(d[s] < n) {

			if (u == t) {
				ret += Augment();
				u = s;
			}

			bool ok = false;
			for (int i = cur[u];i < G[u].size();++i) {
				Edge& e = edges[G[u][i]];
				if (e.cap > e.flow && d[u] == d[e.to] + 1) {
					ok = true;
					p[e.to] = G[u][i];
					cur[u] = i;
					u = e.to;
					break;
				}
			}

			if (!ok) {
				int Min = n - 1;
				for (int i = 0;i < G[u].size();++i) {
					Edge& e = edges[G[u][i]];
					if (e.cap > e.flow) Min = min(Min, d[e.to]);
				}
				if (--num[d[u]] == 0) break;
				num[d[u] = Min + 1]++;
				cur[u] = 0;
				if (u != s) u = edges[p[u]].from;
			}
		}
		return ret;
	}
};
ISAP isap;
void init() {
	scanf("%d%d",&n,&m);
	isap.init(n);

	int x, y;
	int Min = INF, Max = -INF;

	int s, t;
	for (int i = 1;i <= n;++i) {
		scanf("%d%d",&x,&y);
		if (x <= Min) {
			s = i;
			Min = x;
		}
		if (x >= Max) {
			Max = x;
			t = i;
		}
	}
	int u, v, c;
	for (int i = 0;i < m;++i) {
		scanf("%d%d%d",&u,&v,&c);
		isap.addedge(u, v, c);
		isap.addedge(v, u, c);
	}

	int ans = isap.MaxFlow(s, t);
	printf("%d\n", ans);
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int tt;
	scanf("%d",&tt);
	while(tt--) {
		init();
	}
	return 0;
}


你可能感兴趣的:(最大流)