HDU 6094 Rikka with K-Match
HDU 6094 Rikka with K-Match
网络流大多都是上凸的!
wqs二分(实数二分)
轮廓线dp(需要记录最优解和选择的条数)。
其中二分的值是整数就行:
proof:
\(f(x)\)是整数。
/*
{
######################
# Author #
# Gary #
# 2021 #
######################
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
//inline int read(){
// int x=0;
// char ch=getchar();
// while(ch<'0'||ch>'9'){
// ch=getchar();
// }
// while(ch>='0'&&ch<='9'){
// x=(x<<1)+(x<<3)+(ch^48);
// ch=getchar();
// }
// return x;
//}
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n,m,k;
int a[40000+2][5];
int b[40000+2][5];
pair<LL,int> dp[2][5][1<<4];
pair<LL,int> solve(LL x){
rb(j,1,m) rep(mask,1<<m) dp[1][j][mask]=II(4e18+2,4e18+2);
dp[1][1][0]=II(0,0);
rb(i,1,n){
rb(j,1,m) rep(mask,1<<m) dp[(i&1)^1][j][mask]=II(4e18+2,4e18+2);
rb(j,1,m){
rep(mask,1<<m){
if(dp[i&1][j][mask].FIR==4e18+2) continue;
int nexi,nexj;
nexi=i&1,nexj=j+1;
if(nexj==m+1){nexi^=1,nexj=1;}
bool pre,up;
if(j!=1){
pre=(mask>>(j-2))&1;
if(!pre){
LL cost=dp[i&1][j][mask].FIR+b[i][j-1]-x;
int cnt=dp[i&1][j][mask].SEC-1;
check_min(dp[nexi][nexj][mask|(1<<(j-2))|(1<<(j-1))],II(cost,cnt));
}
}
if(i!=1){
up=(mask>>(j-1))&1;
if(!up){
LL cost=dp[i&1][j][mask].FIR+a[i-1][j]-x;
int cnt=dp[i&1][j][mask].SEC-1;
check_min(dp[nexi][nexj][mask|(1<<(j-1))],II(cost,cnt));
}
}
int nmask=mask;
if((mask>>(j-1))&1) nmask^=1<<(j-1);
check_min(dp[nexi][nexj][nmask],dp[i&1][j][mask]);
}
}
}
pair<LL,int> ret={4e18+2,0};
rep(mask,1<<m){
check_min(ret,dp[(n&1)^1][1][mask]);
}
ret.SEC=-ret.SEC;
return ret;
}
void solve(){
scanf("%d%d%d",&n,&m,&k);
rb(i,1,n-1) rb(j,1,m) scanf("%d",&a[i][j]);
rb(i,1,n) rb(j,1,m-1) scanf("%d",&b[i][j]);
LL l=0,r=1e14+1;
while(l<r-1){
LL mid=(l+r)>>1;
if(solve(mid).SEC<k) l=mid;
else r=mid;
}
++l;
printf("%lld\n",llround(solve(l).FIR+l*k));
}
int main(){
// system("gen.exe");
// freopen("test.in","r",stdin);
// freopen("my.out","w",stdout);
int t;
scanf("%d",&t);
while(t--) solve();
return 0;
}