BZOJ 1877: [SDOI2009]晨跑

要求把图分割成若干条除了起点和终点之外都不相交的路径
拆点最小费用最大流解决即可

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<ll, int>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) >> 1)
#define lowbit(i) ((i) & (-i))
#define ll long long
#define ull unsigned long long
#define db double
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define Edg int ccnt=1,head[N],to[E],ne[E];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[E],ne[E],c[E];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1e9 + 7;
void M(int &x) {if (x >= MOD)x -= MOD; if (x < 0)x += MOD;}
int qp(int a, int b = MOD - 2) {int ans = 1; for (; b; a = 1LL * a * a % MOD, b >>= 1)if (b & 1)ans = 1LL * ans * a % MOD; return ans % MOD;}
template<class T>T gcd(T a, T b) { while (b) { a %= b; std::swap(a, b); } return a; }
template<class T>bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T>bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _() {
	int x = 0, f = 1; char ch = getc();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
	while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
	return x * f;
}

const int N = 555, E = 666666, INF = 0x3f3f3f3f;
Edgc
bool inq[N];
int n, m, cnt, in[N], out[N], f[E], path[N], dis[N];
int que[E];
inline void add(int u, int v, int ff, int cc) {
	addd(u, v, cc); f[ccnt] = ff;
	addd(v, u, -cc); f[ccnt] = 0;
}

bool spfa(int s, int t) {
	rep (i, 0, t + 1) dis[i] = INF, path[i] = inq[i] = 0;
	int l = 0, r = 0;
	que[r++] = s;
	dis[s] = 0;
	inq[s] = 1;
	while (l ^ r) {
		int u = que[l++];
		inq[u] = 0;
		es (u, i, v) {
			if (f[i] && chkmin(dis[v], dis[u] + c[i])) {
				path[v] = i;
				if (!inq[v]) inq[v] = 1, que[r++] = v;
			}
		}
	}
	return dis[t] != INF;
}

std::pii solve(int s, int t) {
	std::pii ans(0, 0);
	while (spfa(s, t)) {
		int x = INF;
		for (int i = path[t]; i; i = path[to[i ^ 1]]) x = std::min(x, f[i]);
		for (int i = path[t]; i; i = path[to[i ^ 1]]) f[i] -= x, f[i ^ 1] += x;
		ans.fi++; ans.se += dis[t];
	}
	return ans;
}

int main() {
#ifdef LOCAL
	freopen("ans.out", "w", stdout);
#endif
	n = _(), m = _();
	rep (i, 1, n + 1) {
		if (i == 1 || i == n) in[i] = out[i] = ++cnt;
		else in[i] = ++cnt, out[i] = ++cnt;
	}
	rep (i, 0, m) {
		int a = _(), b = _(), cc = _();
		add(out[a], in[b], INF, cc);
	}
	rep (i, 2, n) add(in[i], out[i], 1, 0);
	std::pii ans = solve(in[1], out[n]);
	printf("%d %d\n", ans.fi, ans.se);
	return 0;
}
posted @ 2020-03-03 17:35  Mrzdtz220  阅读(71)  评论(0编辑  收藏  举报