題目大意
有
題解
這題很難。首先假設有兩種超級植物
目標函數是
接下來需要考慮一些經典的對偶模型。對於最大循環費用流,其線性規劃模型是
這個東西的對偶,就是
可以發現這個對偶問題貌似能套在原問題上。
比如對於這條式子
是可以直接套的,這相當於在循環流中連上一條
那麼對於類似
最後的問題是,給定圖G,如何求一個最大循環流。
最大循環流
這個問題等價於我們用若干個環,使得其覆蓋的權值和最大。
一個點在環上,等價於其入度=出度。
那麼我們一開始先貪心的把所有權值爲正數的邊都選上,同時修改兩端的度數。對於一條負邊權
代碼
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 1005;
typedef long long ll;
class network
{
public:
struct node
{
ll to,next,flow,cost;
node(void){}
node(int a,int b,ll c,ll d) : to(a),next(b),flow(c),cost(d){}
}e[maxn * 100];
ll deg[maxn];
int final[maxn],ed[maxn][4],n,tot,s,t,cnt;
void set(int siz)
{
n = siz,s = n,t = n + 1;
for(int i = 0;i <= t;i ++) final[i] = deg[i] = 0;
tot = 1;
cnt = 0;
}
void link(int u,int v,ll f,ll c)
{
e[++ tot] = node(v,final[u],f,c),final[u] = tot;
e[++ tot] = node(u,final[v],0,-c),final[v] = tot;
}
void push(int u,int v,int f,int c)
{
ed[++ cnt][0] = u,ed[cnt][1] = v,ed[cnt][2] = f,ed[cnt][3] = c;
}
ll maxflow()
{
static ll dis[maxn];
static int pr[maxn];
static bool in[maxn];
ll ret = 0;
for(;;)
{
for(int i = 0;i <= t;i ++) dis[i] = (1ll << 60),in[i] = 0;
dis[s] = 0;
static int que[maxn * 100];
que[1] = s;
in[s] = 1;
for(int fi = 1,en = 1;fi <= en;fi ++)
{
int u = que[fi];
for(int i = final[u];i;i = e[i].next)
if (e[i].flow > 0 && dis[e[i].to] > dis[u] + e[i].cost)
{
dis[e[i].to] = dis[u] + e[i].cost;
pr[e[i].to] = i;
if (!in[e[i].to])
{
in[e[i].to] = 1,que[++ en] = e[i].to;
}
}
in[u] = 0;
}
if (dis[t] == (1ll << 60)) break;
ll fl = (1ll << 60);
for(int i = t;i != s;i = e[pr[i] ^ 1].to)
fl = min(fl,e[pr[i]].flow);
ret += fl * dis[t];
for(int i = t;i != s;i = e[pr[i] ^ 1].to)
e[pr[i]].flow -= fl,e[pr[i] ^ 1].flow += fl;
}
return ret;
}
ll calc()
{
ll sum = 0;
for(int i = 1;i <= cnt;i ++)
{
ll u = ed[i][0],v = ed[i][1],f = ed[i][2],c = ed[i][3];
if (c < 0)
link(u,v,f,-c); else
{
sum += f * c;
deg[u] += f,deg[v] -= f;
link(v,u,f,c);
}
}
for(int i = 0;i < n;i ++)
if (deg[i] < 0) link(s,i,-deg[i],0); else link(i,t,deg[i],0);
return sum - maxflow();
}
}flow;
class Farmville
{
public:
int minTime(vector<string> s, vector<int> t, vector<int> c,int budget)
{
int l = 0,r = 100000,tmp,n = t.size();
for(int mid;l <= r;)
{
mid = l + r >> 1;
flow.set(n * 2 + 2);
int ys = n * 2,xt = n * 2 + 1;
for(int i = 0;i < n;i ++)
{
flow.push(ys,i << 1,1 << 30,0),flow.push(i << 1 | 1,xt,1 << 30,0);
flow.push(i << 1,i << 1 | 1,1 << 30,0),flow.push(i << 1,i << 1 | 1,c[i],t[i]);
}
for(int i = 0;i < n;i ++)
for(int j = 0;j < n;j ++)
if (s[i][j] == '1') flow.push(j << 1 | 1,i << 1,1 << 30,0);
flow.push(xt,ys,1 << 30,-mid);
if (flow.calc() > budget) l = mid + 1; else
tmp = mid,r = mid - 1;
}
return tmp;
}
};