【BZOJ1877】【SDOI2009】晨跑

【题目链接】

【思路要点】

  • 补档博客,无题解。

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN	505
#define INF	1e9
#define MAXQ	1000005
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
struct edge {int dest, flow, cost; unsigned home; };
int n, m, s, t, tot, len, flow, cost;
int inp[MAXN], outp[MAXN];
int dist[MAXN], path[MAXN];
unsigned home[MAXN];
vector <edge> a[MAXN];
void flow_path() {
	int p = t, ans = INF;
	while (p != s) {
		ans = min(ans, a[path[p]][home[p]].flow);
		p = path[p];
	}
	flow += ans, cost += ans * len;
	p = t;
	while (p != s) {
		a[path[p]][home[p]].flow -= ans;
		a[p][a[path[p]][home[p]].home].flow += ans;
		p = path[p];
	}
}
bool spfa() {
	static int q[MAXQ];
	static bool inq[MAXN];
	int l = 0, r = 0;
	q[0] = s, inq[s] = true, dist[s] = 0;
	while (l <= r) {
		int tmp = q[l++];
		inq[tmp] = false;
		for (unsigned i = 0; i < a[tmp].size(); i++)
			if (a[tmp][i].flow != 0 && dist[a[tmp][i].dest] > dist[tmp] + a[tmp][i].cost) {
				dist[a[tmp][i].dest] = dist[tmp] + a[tmp][i].cost;
				path[a[tmp][i].dest] = tmp;
				home[a[tmp][i].dest] = i;
				if (!inq[a[tmp][i].dest]) {
					inq[a[tmp][i].dest] = true;
					q[++r] = a[tmp][i].dest;
				}
			}
	}
	len = dist[t];
	for (int i = 0; i <= r; i++)
		dist[q[i]] = INF;
	return len != INF;
}
void addedge(int s, int t, int flow, int cost) {
	a[s].push_back((edge) {t, flow, cost, a[t].size()});
	a[t].push_back((edge) {s, 0, -cost, a[s].size() - 1});
}
int main() {
	read(n), read(m);
	tot = 0;
	for (int i = 1; i <= n; i++) {
		inp[i] = ++tot;
		outp[i] = ++tot;
	}
	s = inp[1], t = outp[n];
	for (int i = 1; i <= n; i++) {
		if (i == 1 || i == n) addedge(inp[i], outp[i], INF, 0);
		else addedge(inp[i], outp[i], 1, 0);
	}
	for (int i = 1; i <= m; i++) {
		int x, y, z;
		read(x), read(y), read(z);
		addedge(outp[x], inp[y], 1, z);
	}
	for (int i = 1; i <= tot; i++)
		dist[i] = INF;
	while (spfa())
		flow_path();
	cout << flow << ' ' << cost << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39972971/article/details/80746569