Live2D

Solution -「多校联训」数学考试

Description

  Link.

  给定 n 个函数,第 i 个有 fi(x)=aix3+bix2+cxi+d (x[li,ri]Z),还有 m 条形如 xuxv+d 的限制,请最大化 i=1nfi(xi) 或声明无解。

  n,|li|,|ri|100

Solution

  很久没遇到了,压根儿没往网络流方面想 qwq。

  对于每个 fi,拉一条代表 fi(li..ri) 的链,边权就是某个 f 的值的相反数;限制条件方便转化为最小割,之后直接跑最小割即可。

  O(Dinic((rl),m(rl)))

Code

/*~Rainybunny~*/

#include <queue>
#include <cstdio>

#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )

#define int long long

inline int imin( const int a, const int b ) { return a < b ? a : b; }

const int MAXN = 100, IINF = 1ll << 50, BASE = 7e6;
int n, m, lid[MAXN + 5];

struct Function {
    int a, b, c, d, l, r;
    inline void read() {
        scanf( "%lld %lld %lld %lld %lld %lld" , &a, &b, &c, &d, &l, &r );
    }
    inline int operator () ( const int x ) const {
        return d + x * ( c + x * ( b + x * a ) );
    } 
} fc[MAXN + 5];

struct FlowGraph {
    static const int MAXND = 2e4 + 10, MAXEG = 2e5;
    int ecnt, bound, S, T, head[MAXND];
    struct Edge { int to, flw, nxt; } graph[MAXEG * 2];
    int ds[MAXND], curh[MAXND];

    FlowGraph(): ecnt( 1 ) {}

    inline void clear() {
        ecnt = 1;
        rep ( i, 0, bound ) head[i] = 0;
    }

    inline void operator () ( const int s, const int t, const int f ) {
        graph[++ecnt] = { t, f, head[s] }, head[s] = ecnt;
        graph[++ecnt] = { s, 0, head[t] }, head[t] = ecnt;
    }

    inline bool bfs() {
        static std::queue<int> que;
        rep ( i, 0, bound ) ds[i] = IINF;
        que.push( S ), ds[S] = 0;
        while ( !que.empty() ) {
            int u = que.front(); que.pop();
            for ( int i = head[u], v; i; i = graph[i].nxt ) {
                if ( graph[i].flw && ds[u] + 1 < ds[v = graph[i].to] ) {
                    ds[v] = ds[u] + 1, que.push( v );
                }
            }
        }
        return ds[T] != IINF;
    }

    inline int dfs( const int u, int iflw ) {
        if ( u == T ) return iflw;
        int oflw = 0;
        for ( int& i = curh[u], v; i; i = graph[i].nxt ) {
            if ( graph[i].flw && ds[u] + 1 == ds[v = graph[i].to] ) {
                int tmp = dfs( v, imin( iflw - oflw, graph[i].flw ) );
                oflw += tmp, graph[i].flw -= tmp, graph[i ^ 1].flw += tmp;
                if ( iflw == oflw ) break;
            }
        }
        if ( !oflw ) ds[u] = IINF;
        return oflw;
    }

    inline int calc( const int s, const int t ) {
        int ret = 0; S = s, T = t;
        while ( bfs() ) {
            rep ( i, 0, bound ) curh[i] = head[i];
            ret += dfs( S, IINF );
        }
        return ret;
    }
} G;

signed main() {
    freopen( "sleep.in", "r", stdin );
    freopen( "sleep.out", "w", stdout );

    int Q; scanf( "%lld", &Q );
    while ( Q-- ) {
        scanf( "%lld %lld", &n, &m ), G.clear();
        int S = 0, T = 1, node = 1;
        rep ( i, 1, n ) {
            fc[i].read();
            G( S, lid[i] = ++node, IINF );
            rep ( j, fc[i].l, fc[i].r ) {
                G( node, node + 1, BASE - fc[i]( j ) ), ++node;
            }
            G( node, T, IINF );
        }
        rep ( i, 1, m ) {
            int u, v, d; scanf( "%lld %lld %lld", &u, &v, &d );
            rep ( x, fc[u].l, fc[u].r ) {
                if ( fc[v].l <= x - d && x - d <= fc[v].r ) {
                    G( lid[u] + x - fc[u].l, lid[v] + x - d - fc[v].l, IINF );
                } else if ( x - d > fc[v].r ) {
                    G( lid[u] + x - fc[u].l, T, IINF );
                }
            }
        }
        G.bound = node;
        int ans = -G.calc( S, T );
        if ( ans <= -IINF ) puts( "mei ji ge" );
        else printf( "%lld\n", ans + n * BASE );
    }
    return 0;
}

posted @   Rainybunny  阅读(60)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2020-07-11 Solution -「洛谷 P5827」边双连通图计数
2020-07-11 Solution -「洛谷 P5827」点双连通图计数
点击右上角即可分享
微信分享提示