[BZOJ1877][SDOI2009]晨跑[最大流+费用流]
天数最多 长度最小 天数是流量,长度是费用
每个点拆成两个点限流1,就能保证只走一次 然后跑费用流
#include <bits/stdc++.h>
using namespace std;
#define MP make_pair
#define pb push_back
#define read2(a, b) (read(a), read(b))
#define read3(a, b, c) (read(a), read(b), read(c))
#define lop(i,a,b) for(register int i = (a); i <= (b); ++i)
#define dlop(i,a,b) for(register int i = (a); i >= (b); --i)
#define eps (1e-7)
#define fir first
#define sec second
const int inf = 0x3f3f3f3f-1;
const int MAXN = 4e2+7;
const int MAXM = 8e4+7;
typedef long long LL;
typedef long double LF;
typedef pair<int,int> pii;
typedef unsigned long long ULL;
typedef unsigned int uint;
template<class T> void read(T & x) {
register int c = getchar(), f = 1;x = 0;
while(!isdigit(c)) {if (c == '-') f = -f;c = getchar();}
while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
x *= f;
}
int n, m, k, maxflow, dep[MAXN], s, t, cur[MAXN], head[MAXN], ans, dis[MAXN];
bool inq[MAXN];
struct Edge {
int u, v, w, c, next;
} G[MAXM]; int tot = 1;
inline void add(int u, int v, int w, int c) {
// cout << u << ' ' << v << ' ' << w << endl;
G[++tot] = (Edge){u, v, w, c, head[u]}; head[u] = tot;
G[++tot] = (Edge){v, u, 0, -c, head[v]}; head[v] = tot;
}
bool spfa(int s, int t) {
memset(dis, 0x3f, sizeof dis);
memset(inq, 0, sizeof inq);
queue<int>q; q.push(s); dis[s] = 0, inq[s] = 1;
while(!q.empty()) {
int u = q.front();
inq[u] = 0;
q.pop();
for(int i = head[u]; i; i = G[i].next) {
int v = G[i].v, w = G[i].w, c = G[i].c;
if (dis[v] > dis[u] + c && w) {
dis[v] = dis[u] + c;
cur[v] = i;
if (!inq[v]) q.push(v), inq[v] = 1;
}
}
}
return dis[t] < inf;
}
void update(int s, int t) {
int i, x = inf;
for(i = cur[t]; i; x = min(x, G[i].w), i = cur[G[i].u]);
for(i = cur[t]; i; G[i].w -= x, G[i^1].w += x, ans += x * G[i].c, i = cur[G[i].u]);
maxflow += x;
}
void EK(int s, int t) {
while(spfa(s, t)) update(s, t);
}
int main(void) {
// freopen("data.in", "r", stdin);
read2(n, m);
for(int i = 2; i < n; ++i) add(i, i+n, 1, 0);
for(int u, v, w, i = 1; i <= m; ++i) {
read3(u, v, w); add(u+n, v, 1, w);
}
EK(1+n, n);
cout << maxflow << ' ' << ans;
return 0;
}