Submission #27842541


Source Code Expand

#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#define int long long
using namespace std;
int N, M;
int c[2010][2010];
int f[2010][2010];
int ca[2010][2010];
int inq[2010];
pair<int, int>d[2010];
vector<int>l[2010];
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int K;
	cin >> N >> M>>K;
	int i;
	int sum = 0;
	for (i = 0; i < N; i++)
	{
		ca[0][i + 1] = -1;
		l[0].push_back(i + 1);
		l[i + 1].push_back(0);
	}
	for (i = 0; i < M; i++)
	{
		ca[i + N + 1][M + N + 1] = -1;
		l[M + N + 1].push_back(i + 1 + N);
		l[i + N + 1].push_back(1 + M + N);
	}
	for (i = 0; i < K; i++)
	{
		int a,b,cc;
		cin >> a>> b >> cc;
		a--;
		b--;
		ca[0][a+1]++;
		ca[b + N+1][M+N+1]++;
		ca[a + 1][b + N + 1] = 1;
		c[a + 1][b + N + 1] = -cc;
		c[b + N + 1][a + 1] = cc;
		l[a + 1].push_back(b + N + 1);
		l[b + N + 1].push_back(a + 1);
		sum += cc;
	}
	int ans = 0;
	int ans2 = 0;
	while (1)
	{
		memset(d, 1, sizeof(d));
		memset(inq, 0, sizeof(inq));
		d[0] = { 0,-1 };
		queue<int>q;
		q.push(0);
		while (q.size())
		{
			int j = q.front();
			q.pop();
			inq[j] = 0;
			int k;
			for (k = 0; k < l[j].size(); k++)
			{
				if (f[j][l[j][k]] != ca[j][l[j][k]] && (d[l[j][k]].first > d[j].first + c[j][l[j][k]]))
				{
					d[l[j][k]] = { d[j].first + c[j][l[j][k]],j };
					if (inq[l[j][k]] == 0)
					{
						q.push(l[j][k]);
						inq[l[j][k]] = 1;
					}
				}
			}
		}
		if (d[N + M + 1].first > (1LL << 40))
			break;
		int s = N + M + 1;
		int flow = 1000000000, cost = 0;
		while (s)
		{

			flow = min(flow, ca[d[s].second][s] - f[d[s].second][s]);
			s = d[s].second;
		}

		s = N + M + 1;
		while (s)
		{
			cost += c[d[s].second][s] * flow;
			f[d[s].second][s] += flow;
			f[s][d[s].second] -= flow;
			s = d[s].second;
		}
		if (cost > 0)
			break;
		ans2 += cost;
		ans += flow;
	}
	cout << sum+ans2;
}

Submission Info

Submission Time
Task H - Minimum Coloring
User codingmorning
Language C++ (GCC 9.2.1)
Score 600
Code Size 1977 Byte
Status AC
Exec Time 64 ms
Memory 21344 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:65:18: warning: comparison of integer expressions of different signedness: ‘long long int’ and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   65 |    for (k = 0; k < l[j].size(); k++)
      |                ~~^~~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 41
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 7 ms 3664 KiB
random_01.txt AC 5 ms 4388 KiB
random_02.txt AC 4 ms 4108 KiB
random_03.txt AC 18 ms 4324 KiB
random_04.txt AC 6 ms 4124 KiB
random_05.txt AC 4 ms 4160 KiB
random_06.txt AC 10 ms 4160 KiB
random_07.txt AC 10 ms 4244 KiB
random_08.txt AC 7 ms 4180 KiB
random_09.txt AC 4 ms 4296 KiB
random_10.txt AC 5 ms 4108 KiB
random_11.txt AC 6 ms 4220 KiB
random_12.txt AC 10 ms 4264 KiB
random_13.txt AC 6 ms 4244 KiB
random_14.txt AC 7 ms 4220 KiB
random_15.txt AC 12 ms 4260 KiB
random_16.txt AC 11 ms 4236 KiB
random_17.txt AC 58 ms 15840 KiB
random_18.txt AC 26 ms 4692 KiB
random_19.txt AC 35 ms 9300 KiB
random_20.txt AC 2 ms 3724 KiB
random_21.txt AC 50 ms 15728 KiB
random_22.txt AC 34 ms 8864 KiB
random_23.txt AC 34 ms 9556 KiB
random_24.txt AC 7 ms 4104 KiB
random_25.txt AC 61 ms 21172 KiB
random_26.txt AC 64 ms 21344 KiB
random_27.txt AC 59 ms 20976 KiB
random_28.txt AC 59 ms 21012 KiB
random_29.txt AC 18 ms 19696 KiB
random_30.txt AC 16 ms 19752 KiB
random_31.txt AC 9 ms 4180 KiB
random_32.txt AC 10 ms 9148 KiB
random_33.txt AC 15 ms 8744 KiB
random_34.txt AC 13 ms 9528 KiB
random_35.txt AC 14 ms 8784 KiB
random_36.txt AC 26 ms 11124 KiB
random_37.txt AC 15 ms 11792 KiB
sample_01.txt AC 3 ms 3756 KiB
sample_02.txt AC 3 ms 3672 KiB
sample_03.txt AC 2 ms 3760 KiB