QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#346606#4775. Pool constructionPetroTarnavskyi#AC ✓17ms5132kbC++203.0kb2024-03-08 18:48:042024-03-08 18:48:04

Judging History

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

  • [2024-03-08 18:48:04]
  • 评测
  • 测评结果:AC
  • 用时:17ms
  • 内存:5132kb
  • [2024-03-08 18:48:04]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const LL LINF = 1e9;

struct Graph
{
	struct Edge
	{
		int from, to;
		LL cap, flow;
	};
	
	int n;
	vector<Edge> edges;
	vector<VI> g;
	VI d, p;
	
	void init(int _n)
	{
		n = _n;
		edges.clear();
		g.clear();
		g.resize(n);
		d.resize(n);
		p.resize(n);
	}
	void addEdge(int from, int to, LL cap)
	{
		assert(0 <= from && from < n);
		assert(0 <= to && to < n);
		assert(0 <= cap);
		
		g[from].PB(SZ(edges));
		edges.PB({from, to, cap, 0});
		g[to].PB(SZ(edges));
		edges.PB({to, from, 0, 0});
	}
	int bfs(int s, int t)
	{
		fill(ALL(d), -1);
		d[s] = 0;
		queue<int> q;
		q.push(s);
		while(!q.empty())
		{
			int v = q.front();
			q.pop();
			for(int e : g[v])
			{
				int to = edges[e].to;
				if(edges[e].flow < edges[e].cap && d[to] == -1)
				{
					d[to] = d[v] + 1;
					q.push(to);
				}
			}
		}
		return d[t];
	}
	LL dfs(int v, int t, LL flow)
	{
		if(v == t || flow == 0)
			return flow;
		for(; p[v] < SZ(g[v]); p[v]++)
		{
			int e = g[v][p[v]], to = edges[e].to;
			LL c = edges[e].cap, f = edges[e].flow;
			if(f < c && (to == t || d[to] == d[v] + 1))
			{
				LL push = dfs(to, t, min(flow, c - f));
				if(push > 0)
				{
					edges[e].flow += push;
					edges[e ^ 1].flow -= push;
					return push;
				}
			}
		}
		return 0;
	}
	LL flow(int s, int t)
	{
		assert(0 <= s && s < n);
		assert(0 <= t && t < n);
		assert(s != t);
		LL flow = 0;
		while(bfs(s, t) != -1)
		{
			fill(ALL(p), 0);
			while(true)
			{
				LL f = dfs(s, t, LINF);
				if(f == 0)
					break;
				flow += f;
			}
		}
		return flow;
	}
};


void solve()
{
	int n, m;
	cin >> m >> n;
	
	int d, f, b;
	cin >> d >> f >> b;
	
	Graph F;
	F.init(n * m + 2);
	
	int S = n * m;
	int T = n * m + 1;
	
	int ans = 0;
	FOR(i, 0, n)
	{
		string s;
		cin >> s;
		FOR(j, 0, m)
		{
			int valT = 0;
			if(s[j] == '.')
				valT = f;
			
			
			int valF = 0;
			if(s[j] == '#')
				valF = d;
			
			if(i % (n - 1) == 0 || j % (m - 1) == 0)
				valF = LINF;
			
			
			if(valT <= valF)
			{
				ans += valT;
				F.addEdge(S, i * m + j, valF - valT);
			}
			else
			{
				ans += valF;
				F.addEdge(i * m + j, T, valT - valF);
			}
		
		
			
			if(i != n - 1)
			{
				F.addEdge(i * m + j, (i + 1) * m + j, b);
				F.addEdge((i + 1) * m + j, i * m + j, b);
			}
			if(j != m - 1)
			{
				F.addEdge(i * m + j, i * m + j + 1, b);
				F.addEdge(i * m + j + 1, i * m + j, b);
			}
		}
	}
	cout << ans + F.flow(S, T) << "\n";
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int t;
	cin >> t;
	while(t--)
		solve();
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3628kb

input:

3
3 3
5 5 1
#.#
#.#
###
5 4
1 8 1
#..##
##.##
#.#.#
#####
2 2
27 11 11
#.
.#

output:

9
27
22

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 17ms
memory: 5132kb

input:

56
3 3
5 5 1
#.#
#.#
###
5 4
1 8 1
#..##
##.##
#.#.#
#####
2 2
1 1 1
##
##
2 2
1 10000 1
..
..
5 4
20 41 9
#####
##.##
#.#.#
#####
5 4
20 41 10
#####
##.##
#.#.#
#####
5 4
20 41 11
#####
##.##
#.#.#
#####
5 4
20 39 10
#####
##.##
#.#.#
#####
3 3
9760 9015 711
.#.
#.#
###
5 5
7415 7931 2080
.....
#.....

output:

9
27
0
40000
108
120
123
117
20874
100110
112364
203900
271440
462119
490330
1746528
1067774
1055196
2609818
2094932
5199902
13978
73960
99018
262976
224632
78984
167795
392774
649054
1232290
135876
318982
413042
1479538
1680354
349557
540100
2101110
335884
2245998
170698
780013
1804351
2998519
3661...

result:

ok 56 lines