QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#642121#5114. Cells ColoringPonyHexTL 925ms22052kbC++205.1kb2024-10-15 10:34:162024-10-15 10:34:16

Judging History

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

  • [2024-10-15 10:34:16]
  • 评测
  • 测评结果:TL
  • 用时:925ms
  • 内存:22052kb
  • [2024-10-15 10:34:16]
  • 提交

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 = 4e5 + 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);
    }
    idx = 1;
    for (int i = 1; i <= n; i++) {
        add(st, i, 0); add(i, st, 0);
    }
    for (int j = 1; j <= m; j++) {
        add(n + j, ed, 0); add(ed, n + j, 0);
    }
    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);
            }
        }
    }
    ll ans = maxm; ll val = 0;
    for (int c = 0; c <= mx; c++) {//枚举c建图,然后找最大流
        
        val += dinic(); //cout << val << endl;
        ans = min(ans, cc * c + dd * (sum - val));
        
        for (int i = 2; i <= m*2+n*2; i+=2)e[i].c=e[i].c+1;
    }
    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: 2ms
memory: 15912kb

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 18156kb

input:

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

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 281ms
memory: 18024kb

input:

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

output:

109972100048

result:

ok 1 number(s): "109972100048"

Test #4:

score: 0
Accepted
time: 365ms
memory: 18048kb

input:

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

output:

8437726048

result:

ok 1 number(s): "8437726048"

Test #5:

score: 0
Accepted
time: 847ms
memory: 22052kb

input:

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

output:

18268031127

result:

ok 1 number(s): "18268031127"

Test #6:

score: 0
Accepted
time: 787ms
memory: 20064kb

input:

250 250 768323813 489146
...*................*...........*.................*..***..*.......*..*......*.................*...*.........*.*.*.*...*.*.*.*.*.......*........*.............*...............*..*.............*.*...*.....................**.....**.....*.*........*......
...................*.......

output:

25999088192

result:

ok 1 number(s): "25999088192"

Test #7:

score: 0
Accepted
time: 289ms
memory: 20260kb

input:

250 250 865365220 7248935
.....**.*.***...**.**...*.**.*****..****.**.**.*...*..**....*.**.*..**..*..*.****....***.***.*...*.*.*.**..****.***.*.**..*****.**..*.*.***..***.*..*.*..*......*.*******.*******.*..*.******.....**.***...*****...*...**....**.**.*...*...**.*.*****...*.
*..*.**.*...****.*.**.*...

output:

97440874100

result:

ok 1 number(s): "97440874100"

Test #8:

score: 0
Accepted
time: 98ms
memory: 18000kb

input:

153 225 485767021 308782855
.*.**.***..***..***..****.*****.***.....*.***.****.*.*......**......****.****.**.******...**...*.***.*..**.*****.****....*.*.*...**..****.**.******....*....****....**.*.*****.**.**.**.***...*.**.*.**.****.*.*....*.*****...***
*.*....*...*.*..*.*****.***.***.***.***..*.***...

output:

54405906352

result:

ok 1 number(s): "54405906352"

Test #9:

score: 0
Accepted
time: 12ms
memory: 17872kb

input:

17 20 823772868 410753944
.....*......**......
.......*............
...............*....
......*.............
...................*
*........*.*..*.....
.....*.............*
..*..........*.*....
.......*............
...**...........**.*
....................
**......**.......*..
.*.............*....
....

output:

16062438436

result:

ok 1 number(s): "16062438436"

Test #10:

score: 0
Accepted
time: 116ms
memory: 20056kb

input:

227 129 426009970 614728889
*..****..*..*.********.*.*..***.*.*..**..*.***.**.**.***..*.***..*..*.***.*.*.**..*****.**..***.*.****.**.***.****..**.**.*.**.**
*...*****.**....****..*....*.*.**.*****.*..*****...*...**...******..****.*..**...***.*.*.*..*.*.*..*.******.*****..*.**********.*
.*.*..**..**...

output:

49843166490

result:

ok 1 number(s): "49843166490"

Test #11:

score: 0
Accepted
time: 171ms
memory: 17928kb

input:

170 219 28214303 818736602
*..*....*..*..*....**.*...*..*.**....**.*..*........*.**.....*....*.*..*..*.**.....*..***......*.....*...*........**.*.*.***...*........**..**....***...**....*.*..........**....*....**.***....*.*.*..*..***.....*.*..***.
.*.*.....**...*..*....*.*....*.*.**.........*...*..*....

output:

4514288480

result:

ok 1 number(s): "4514288480"

Test #12:

score: 0
Accepted
time: 64ms
memory: 18188kb

input:

221 2 186883458 764869506
*.
.*
**
.*
**
**
*.
.*
.*
.*
*.
.*
..
**
**
*.
..
.*
.*
**
**
*.
.*
*.
..
*.
**
.*
**
..
**
..
**
..
.*
*.
**
.*
.*
**
..
.*
**
**
**
**
.*
..
.*
.*
..
**
.*
**
.*
**
..
**
*.
**
..
.*
*.
.*
**
.*
**
..
.*
**
.*
**
**
.*
**
**
.*
**
**
..
..
.*
**
..
**
.*
*.
*.
*.
*.
*.
*...

output:

17753928510

result:

ok 1 number(s): "17753928510"

Test #13:

score: 0
Accepted
time: 8ms
memory: 18180kb

input:

28 2 127093639 70848307
.*
.*
**
..
..
.*
**
*.
**
..
**
*.
*.
**
*.
..
.*
.*
*.
**
**
.*
**
..
*.
.*
**
**

output:

1468878336

result:

ok 1 number(s): "1468878336"

Test #14:

score: 0
Accepted
time: 39ms
memory: 15888kb

input:

142 1 465099485 99077573
*
.
.
*
*
*
*
*
.
*
.
*
.
*
.
.
.
.
*
*
*
*
*
.
*
.
.
*
*
*
.
*
.
.
.
.
.
*
*
*
*
*
*
*
*
.
*
.
*
.
.
*
*
.
*
*
*
*
.
.
*
*
.
*
*
*
*
*
*
*
.
.
.
*
*
*
*
*
*
.
*
.
*
.
*
*
*
*
.
.
.
*
*
.
*
.
.
*
.
*
.
.
.
*
.
.
*
.
.
.
.
*
.
*
.
*
*
*
*
*
*
*
.
*
*
*
*
.
*
.
.
.
.
*
.
*
*
....

output:

5845576807

result:

ok 1 number(s): "5845576807"

Test #15:

score: 0
Accepted
time: 359ms
memory: 18024kb

input:

250 250 420456041 0
...*****.....****......*.**..*..*.**.**...*.***..**.*......*.*.....**..*..**.*..***.*.****.*.**.*.....**..*.*.**....**..***......*...***..*...****.*****.*.***.**.*.*...****.***..*...*.*.**.***..*.***.*.****.*.*...**....***....*.*.**....***...*.***...
*.**...**.**...*..*..*.*.*.**...

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 925ms
memory: 19956kb

input:

250 250 0 577876919
.............**.*.....*.....*.*..*.......*...*.................**........*........*....*...*.......*...*........................*.......*.....*.*.*.......*........................*...............*..*.*....*.*.*..................*.....................
...*...*...................*....

output:

0

result:

ok 1 number(s): "0"

Test #17:

score: 0
Accepted
time: 0ms
memory: 13856kb

input:

250 250 708109405 398540228
**********************************************************************************************************************************************************************************************************************************************************
*********************...

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: -100
Time Limit Exceeded

input:

250 250 1000000000 1000000
..........................................................................................................................................................................................................................................................
.........................

output:

62500000000

result: