QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#642037#5114. Cells ColoringPonyHexTL 888ms34436kbC++205.1kb2024-10-15 09:05:542024-10-15 09:05:54

Judging History

你现在查看的是最新测评结果

  • [2024-10-15 09:05:54]
  • 评测
  • 测评结果:TL
  • 用时:888ms
  • 内存:34436kb
  • [2024-10-15 09:05:54]
  • 提交

answer

#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define double long double
//#define int long long
#define lc u<<1
#define rc u<<1|1
#define endl "\n"
#define X first
#define Y second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> PII;
const ll N = 1e6 + 50;
const ll maxm = 2e18;
const ll mod = 1e9 + 7;

ll exgcd(ll a, ll b, ll& x, ll& y);

ll ksm(ll a, ll b) {
    ll base = a;
    ll ans = 1;
    while (b) {
        if (b & 1)ans *= base;
        ans %= mod;
        base *= base; base %= mod;
        b >>= 1;
    }
    return ans % mod;
}

char mp[300][300];
ll n, m, cc, dd;
//ll mf[N * 2], prv[N * 2];
ll cur[N * 2], d[N * 2];
ll st, ed;
struct eage {
    ll v, next, c;
}e[N];
ll h[N], idx = 1;
void add(ll u, ll v, ll c) {
    e[++idx] = { v,h[u],c }; h[u] = idx;
    e[++idx] = { u,h[v],c }; h[v] = idx;
}

bool bfs() {//在残量网络中构建分层图
    memset(d, 0, sizeof(d));
    queue<ll>q;
    q.push(st);
    d[st] = 1;
    while (q.size()) {//我们对此时能达到的节点构造分层图
        ll u = q.front(); q.pop();
        for (int i = h[u]; i; i = e[i].next) {
            ll v = e[i].v;
            if (d[v] == 0 && e[i].c) {//变化的残量网络中,一部分容量已经无了,所以我们直接把他从分层图中踢出去
                d[v] = d[u] + 1;
                q.push(v);
                if (v == ed)return true;
            }
        }
    }
    //不存在prv数组不需要记录入边编号,没有mf数组,不需要在bfs的时候顺路记录mflow
    return false;
}

ll dfs(ll u, ll mf) {//判断我们在分层图中所处的位置,记录当前路径的最大流
    if (u == ed)return mf;
    //走到汇点了,我们能得到流
    ll sum = 0;//记录当前节点后能获取多大的流,然后返回
    for (int i = cur[u]; i; i = e[i].next) {//遍历能到达的所有节点
        cur[u] = i;//当前弧优化,就是说走到过的最远的弧,我们下次要是再走,就从这个弧开始
        ll v = e[i].v;
        if (e[i].c && d[v] == d[u] + 1) {
            ll mid = dfs(v, min(mf, e[i].c));
            sum += mid;
            e[i].c -= mid;
            e[i ^ 1].c += mid;

            mf -= mid;//我就说忘了什么
            if (mf == 0)break;

            /*
            if (e[i].c == 0)d[i] = 0;
            */
        }
    }
    //很多细节都没记住,
    //包括这个
    if (sum == 0)d[u] = 0;//从图中踢出去
    //在上面我写了一个,残量为0的时候从图中踢出去
    //但是显然sum为0更合理因为残量不为0如果后续残量为0我们依然无法获取新的路径
    //所以我们根据sum为0来判断是否移除出图
    return sum;
}

ll dinic() {
    ll flow = 0;
    while (bfs()) {//判断是否能够开拓出增广路径,并且构建出分层图
        memcpy(cur, h, sizeof h);
        flow += dfs(st, maxm);
    }
    return flow;
}

void solve()
{
    //现在尝试网络流解法,听xixu说起,其实是要对行和列建边,
    //其实有思路,但是不知道是不是正解
    cin >> n >> m >> cc >> dd; idx = 0;
    st = n + m + 1; ed = n + m + 2;
    ll mx = 0, sum = 0;
    for (int i = 1; i <= n; i++) {
        ll mid = 0;
        for (int j = 1; j <= m; j++) {
            cin >> mp[i][j];
            if (mp[i][j] == '.')mid++, sum++;
        }
        mx = max(mx, mid);
    }
    for (int j = 1; j <= m; j++) {
        ll mid = 0;
        for (int i = 1; i <= n; i++) {
            if (mp[i][j] == '.')mid++;
        }
        mx = max(mx, mid);
    }
    ll ans = maxm;
    for (int c = 0; c <= mx; c++) {//枚举c建图,然后找最大流
        for (int i = 1; i <= n + m + 2; i++)h[i] = 0;
        idx = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                //对每行每列建边
                if (mp[i][j] == '.') {
                    add(i, n + j, 1); add(n + j, i, 0);
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            add(st, i, c); add(i, st, 0);
        }
        for (int j = 1; j <= m; j++) {
            add(n + j, ed, c); add(ed, n + j, 0);
        }
        ll val = dinic(); //cout << val << endl;
        ans = min(ans, cc * c + dd * (sum - val));
    }
    cout << ans << endl;
    return;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    ll t = 1; //cin >> t;
    while (t--)solve();
    return 0;
}
/*PonyHex*/


ll exgcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1; y = 0;
        return a;
    }
    ll g = exgcd(b, a % b, x, y);
    ll temp = x;
    x = y;
    y = temp - (a / b) * y;
    return g;
}

/*
ll ksm(ll a, ll b) {
    ll base = a;
    ll ans = 1;
    while (b) {
        if (b & 1)ans *= base;
        base *= base;
        b >>= 1;
    }
    return ans;
}*/
/*
ll gcd(ll a, ll b) {
    return b ? gcd(b, a % b) : a;
}*/

詳細信息

Test #1:

score: 100
Accepted
time: 9ms
memory: 32300kb

input:

3 4 2 1
.***
*..*
**..

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 3ms
memory: 32276kb

input:

3 4 1 2
.***
*..*
**..

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 697ms
memory: 34436kb

input:

250 250 965680874 9042302
..**.*****..**..**.*****..***..***.**......*.***.*...***.*....*.**.*.**.*.*.****...*.******.***.************....**.*..*..***.*******.*.***.*..**..****.**.*.*..***.****..**.....***.....*.****...*...*.***..****..**.*.*******..*.*******.*.*.*.****.*.***
....**.*******.*.******...

output:

109972100048

result:

ok 1 number(s): "109972100048"

Test #4:

score: 0
Accepted
time: 888ms
memory: 32296kb

input:

250 250 62722280 506434
*.**.***.*.*....*....*...**.*..**..****.*.*..*.*.*..*.....**..*.*.*.*****.*.**..*.**....***..*..*.*.*.**.*..*..*.**..*...**....**..*.*.***.*****.*****.***..**.***.****....*.*.**.**.*...****....*..*.**.**********.......********...***.**..*.....**.*..*
.*..********..*...*..****...

output:

8437726048

result:

ok 1 number(s): "8437726048"

Test #5:

score: -100
Time Limit Exceeded

input:

250 250 85956327 344333
..*.............*...*.....*...*..........*.........*...*.......*..***......*.*........*.*........*........*..*..*.............*.*........*....*..*................***...................*..*.............*..*.....*..**..............*..*......*.....*..**
.........*......*.*.........

output:


result: