https://www.luogu.org/problemnew/show/P4553
垃圾BZOJ,又是权限题
从 s s 向 s′ s ′ 连流量上界为 m m 、下界为 0 0 、费用为 0 0 的边。
每个点拆成两个点,其中一个向另一个连边,上界下界都为 Vi V i ,费用为 0 0 。
从 s′ s ′ 向每个入点连边,每个出点向 t t 连边。
点与点之间如果通航,则从一个连向另一个点。
然后跑有源汇有上下界最小费用最大流即可。
调了好久,发现是dis没有全部赋为 +∞ + ∞ QAQ
/**************************************
* Au: Hany01
* Prob: [BZOJ2055] 80人环游世界
* Date: Jul 19th, 2018
* Email: [email protected]
**************************************/
#include
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define y1 wozenmezhemecaia
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
register char c_; register int _, __;
for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 207, maxm = maxn * maxn * 6;
int n, m, s, t, S, T, beg[maxn], v[maxm], nex[maxm], f[maxm], w[maxm], e = 1, dis[maxn], vis[maxn], Flow, Cost, s1;
inline void add(int uu, int vv, int ff, int ww, int fl = 1) {
v[++ e] = vv, w[e] = ww, f[e] = ff, nex[e] = beg[uu], beg[uu] = e;
if (fl) add(vv, uu, 0, -ww, 0);
}
/*inline void Add(int u, int v, int f1, int f2, int w) {
add(S, v, f1, w), add(u, T, f1, 0);
if (f2 > f1) add(u, v, f2 - f1, w);
}*/
inline bool BFS()
{
static queue<int> q;
Set(dis, 127);
Set(vis, 0), dis[S] = 0, q.push(S);
while (!q.empty()) {
int u = q.front(); q.pop(), vis[u] = 0;
for (register int i = beg[u]; i; i = nex[i])
if (f[i] && chkmin(dis[v[i]], dis[u] + w[i]))
if (!vis[v[i]]) vis[v[i]] = 1, q.push(v[i]);
}
return dis[T] != 0x7f7f7f7f;
}
int DFS(int u, int flow)
{
if (u == T) return flow;
int t, res = flow; vis[u] = 1;
for (register int i = beg[u]; i; i = nex[i])
if (f[i] && dis[v[i]] == dis[u] + w[i] && !vis[v[i]]) {
f[i] -= (t = DFS(v[i], min(res, f[i]))), f[i ^ 1] += t, Cost += t * w[i];
if (!(res -= t)) return flow;
}
return flow - res;
}
int main()
{
#ifdef hany01
File("bzoj2055");
#endif
static int tmp;
n = read(), m = read(), s1 = (T = (S = (t = (n << 1) + 1) + 1) + 1) + 1;
add(s, s1, m, 0);
For(i, 1, n) tmp = read(), add(S, i + n, tmp, 0), add(i, T, tmp, 0), add(s1, i, m, 0), add(i + n, t, m, 0);
For(i, 1, n - 1) For(j, i + 1, n)
if ((tmp = read()) != -1) add(i + n, j, m, tmp);
add(t, s, INF, 0);
while (BFS()) Flow += DFS(S, INF);
printf("%d\n", Cost);
return 0;
}