QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#487215#5340. It Can Be ArrangedPetroTarnavskyi#AC ✓20ms3780kbC++203.0kb2024-07-22 18:24:012024-07-22 18:24:01

Judging History

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

  • [2024-07-22 18:24:01]
  • 评测
  • 测评结果:AC
  • 用时:20ms
  • 内存:3780kb
  • [2024-07-22 18:24:01]
  • 提交

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;

LL LINF = 1e18;

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;
	}
};

const int N = 147;
const int INF = 1e9;
bool used[N];
int a[N], b[N], s[N];
int c[N][N];
VI g[N];

void dfs(int v)
{
	used[v] = true;
	for (auto to : g[v])
		if (!used[to])
			dfs(to);
}

void solve()
{
	int n, m;
	cin >> n >> m;
	Graph G;
	G.init(2 * n + 2);
	int ans = 0;
	FOR (i, 0, n)
	{
		cin >> a[i] >> b[i] >> s[i];
		ans += (s[i] + m - 1) / m;
	}
	FOR (i, 0, n)
	{
		FOR (j, 0, n)
		{
			cin >> c[i][j];
			if (b[i] + c[i][j] < a[j])
				g[i].PB(j);
		}
	}
	int S = 2 * n;
	int T = S + 1;
	FOR (i, 0, n)
	{
		int x = (s[i] + m - 1) / m;
		G.addEdge(S, i, x);
		G.addEdge(n + i, T, x);
		//for(int j : g[i])
		//	G.addEdge(i, n + j, INF);
		
		fill(used, used + n, false);
		dfs(i);
		FOR (j, 0, n)
		{
			if (used[j] && i != j)
				G.addEdge(i, n + j, INF);
		}
		g[i].clear();
	}
	cout << ans - G.flow(S, T) << '\n';
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int t;
	cin >> t;
	FOR (tc, 0, t)
	{
		cout << "Case " << tc + 1 << ": ";
		solve();
	}
	
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3496kb

input:

3
1 5
1 60 12
0
4 1
1 100 10
50 130 3
150 200 15
80 170 7
0 2 3 4
5 0 7 8
9 10 0 12
13 14 15 0
2 1
1 10 1
12 20 1
0 2
5 0

output:

Case 1: 3
Case 2: 22
Case 3: 2

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 20ms
memory: 3780kb

input:

93
73 2421
5 5997 5769
8953 13857 572
32065 39982 2558
29906 54006 3321
12912 18798 7139
28055 40575 8920
30260 41509 4672
18593 45948 8750
17795 40178 6221
16098 43279 5229
14957 39451 2945
32472 42065 6492
3588 14115 8299
12991 37234 3922
17784 41003 1252
21981 25241 4935
21382 44165 8795
16387 26...

output:

Case 1: 117
Case 2: 107
Case 3: 50
Case 4: 135
Case 5: 113
Case 6: 46
Case 7: 83
Case 8: 9
Case 9: 88
Case 10: 116
Case 11: 42
Case 12: 77
Case 13: 55
Case 14: 12
Case 15: 93
Case 16: 10
Case 17: 28
Case 18: 36
Case 19: 106
Case 20: 19
Case 21: 1
Case 22: 76
Case 23: 70
Case 24: 24
Case 25: 21
Case ...

result:

ok 93 lines