QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#627213 | #5114. Cells Coloring | MENDAX | TL | 864ms | 11156kb | C++14 | 2.0kb | 2024-10-10 15:13:07 | 2024-10-10 15:13:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
const int N = 2e5 + 10;
//n*m+n+m
struct edge {
int cap,flow;
}e[N];
vector <pii> G[N];
int n,m,s,t,tot = -1,dis[N],cur[N];
bool vis[N];
bool BFS() {
memset(vis,0,sizeof vis);
queue <int> q;
q.push(s);
dis[s] = 0;vis[s] = 1;
while (!q.empty()) {
int x = q.front();q.pop();
for (auto ed : G[x]) {
int y = ed.first,num = ed.second;
if (!vis[y] && e[num].cap > e[num].flow) {
vis[y] = 1;
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
return vis[t];
}
int DFS(int x,int res) {
if (x == t || !res) return res;
int now = 0;
for (int &i = cur[x];i < G[x].size();++i) {
pii ed = G[x][i];
int y = ed.first,num = ed.second;
if (dis[x] + 1 == dis[y]){
int f = DFS(y,min(res,e[num].cap - e[num].flow));
if (f > 0) {
e[num].flow += f;
e[num ^ 1].flow -= f;
now += f;
res -= f;
if (!res) break;
}
}
}
return now;
}
int maxflow() {
int ans = 0;
while (BFS()) {
memset(cur,0,sizeof cur);
ans += DFS(s,0x3f3f3f3f);
}
return ans;
}
void addedge(int x,int y,int cap) {
G[x].push_back(mp(y,++tot));
e[tot].cap = cap;
G[y].push_back(mp(x,++tot));
}
ll c,d;
int main() {
cin >> n >> m >> c >> d;
ll ans = 0,cnt = 0;
for (int i = 1;i <= n;++i) {
for (int j = 1;j <= m;++j) {
char c;cin >> c;
if (c == '.') {
addedge(i,j+n,1);
++cnt;
}
}
}
s = n + m + 1,t = n + m + 2;
for (int i = 1;i <= n;++i) {
addedge(s,i,0);
}
for (int i = 1;i <= m;++i) {
addedge(n + i,t,0);
}
ans = d * cnt;
ll res = 0;
for (int k = 1;k <= cnt;++k) {
for (auto ed : G[s]) {
int num = ed.second;
e[num].cap++;
}
for (auto ed : G[t]) {
int num = ed.second;
e[num^1].cap++;
}
ll now = maxflow();
res += now;
//cout << k << ":" << now << "\n";
ans = min(ans,c * k + d * (cnt - res));
}
cout << ans;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 10732kb
input:
3 4 2 1 .*** *..* **..
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 10040kb
input:
3 4 1 2 .*** *..* **..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 864ms
memory: 11156kb
input:
250 250 965680874 9042302 ..**.*****..**..**.*****..***..***.**......*.***.*...***.*....*.**.*.**.*.*.****...*.******.***.************....**.*..*..***.*******.*.***.*..**..****.**.*.*..***.****..**.....***.....*.****...*...*.***..****..**.*.*******..*.*******.*.*.*.****.*.*** ....**.*******.*.******...
output:
109972100048
result:
ok 1 number(s): "109972100048"
Test #4:
score: -100
Time Limit Exceeded
input:
250 250 62722280 506434 *.**.***.*.*....*....*...**.*..**..****.*.*..*.*.*..*.....**..*.*.*.*****.*.**..*.**....***..*..*.*.*.**.*..*..*.**..*...**....**..*.*.***.*****.*****.***..**.***.****....*.*.**.**.*...****....*..*.**.**********.......********...***.**..*.....**.*..* .*..********..*...*..****...