题目
百度
分析
很容易想到二分答案
然后考虑判定
条件很多,奇奇怪怪
那就上网络流吧
边权 (leq mid) 两个城市连边 (inf)
源点与所有城市连边,边权为本城市有金矿量
城市与自己的仓库连边,边权为本城市仓库容量
仓库与汇点两边 (inf)
(Code)
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
const int N = 205 , M = 40005 , INF = 2e9;
int w[N] , c[N] , tot , n , m , W , S = 0 , T = 2 * N - 9;
int h[2 * N] , cur[2 * N] , dep[2 * N];
struct node{int x , y , z;}b[M];
struct edge{int to , nxt , w;}e[2 * M];
inline void add(int x , int y , int z){e[++tot] = edge{y , h[x] , z} , h[x] = tot;}
void build(int up)
{
memset(h , 0 , sizeof h);
tot = 1;
for(register int i = 1; i <= m; i++)
if (b[i].z <= up) add(b[i].x , b[i].y , INF) , add(b[i].x , b[i].y , 0),
add(b[i].y , b[i].x , INF) , add(b[i].y , b[i].x , 0);
for(register int i = 1; i <= n; i++) add(i , i + n , c[i]) , add(i + n , i , 0);
for(register int i = 1; i <= n; i++) add(S , i , w[i]) , add(i , S , 0);
for(register int i = 1; i <= n; i++) add(i + n , T , INF) , add(T , i + n , 0);
}
queue<int> q;
int bfs()
{
while (!q.empty()) q.pop();
for(register int i = S; i <= T; i++) cur[i] = h[i] , dep[i] = 0;
dep[S] = 1 , q.push(S);
while (!q.empty())
{
int now = q.front(); q.pop();
for(register int i = h[now]; i; i = e[i].nxt)
{
int v = e[i].to;
if (dep[v] || e[i].w == 0) continue;
dep[v] = dep[now] + 1 , q.push(v);
}
}
return dep[T];
}
int dfs(int x , int fa , int mi)
{
if (x == T || mi <= 0) return mi;
int flow = 0;
for(register int i = cur[x]; i; i = e[i].nxt)
{
cur[x] = i;
int v = e[i].to;
if (e[i].w == 0 || v == fa || dep[x] + 1 != dep[v]) continue;
int f = dfs(v , x , min(mi , e[i].w));
if (f <= 0) continue;
flow += f , mi -= f , e[i].w -= f , e[i ^ 1].w += f;
if (mi <= 0) break;
}
return flow;
}
int dinic()
{
int flow = 0;
while (bfs()) flow += dfs(S , -1 , INF);
return flow;
}
int check(int up)
{
build(up);
if (dinic() >= W) return 1;
return 0;
}
int main()
{
scanf("%d" , &n);
while (n != 0)
{
W = 0;
for(register int i = 1; i <= n; i++) scanf("%d" , &w[i]) , W += w[i];
for(register int i = 1; i <= n; i++) scanf("%d" , &c[i]);
scanf("%d" , &m);
int l = INF , r = -INF , mid , ans = INF;
for(register int i = 1; i <= m; i++)
scanf("%d%d%d" , &b[i].x , &b[i].y , &b[i].z),
l = min(l , b[i].z) , r = max(r , b[i].z);
while (l <= r)
{
mid = (l + r) >> 1;
if (check(mid)) r = mid - 1 , ans = mid;
else l = mid + 1;
}
if (ans == INF) printf("No Solution
");
else printf("%d
" , ans);
scanf("%d" , &n);
}
}