QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#359627#410. TelegraphAkli20 1ms5560kbC++112.1kb2024-03-20 19:34:242024-03-20 19:34:25

Judging History

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

  • [2024-03-20 19:34:25]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:5560kb
  • [2024-03-20 19:34:24]
  • 提交

answer

#include <iostream>
#include <queue>
#define MAX_N 100005
using namespace std;
long long current_ingoing[MAX_N], cost_to_switch[MAX_N], reduced[MAX_N];
pair<long long, long long> outgoing[MAX_N];
queue<long long> vertices_to_check;
bool was_checked[MAX_N];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long N, t1, t2, forced_switch = 0;
	cin >> N;
	for (size_t i = 1; i <= N; i++)
	{
		cin >> t1 >> t2;
		outgoing[i] = { t1, t2 };
		current_ingoing[t1]++;
	}
	for (size_t i = 1; i <= N; i++)
	{
		if (!current_ingoing[i])
		{
			vertices_to_check.push(i);
		}
	}
	while (!vertices_to_check.empty())
	{
		long long cs = vertices_to_check.front();
		vertices_to_check.pop();
		forced_switch += cost_to_switch[cs] - reduced[cs];
		cost_to_switch[outgoing[cs].first] += outgoing[cs].second;
		reduced[outgoing[cs].first] = max(reduced[outgoing[cs].first], outgoing[cs].second);
		current_ingoing[outgoing[cs].first]--;
		if (current_ingoing[outgoing[cs].first] == 0)
		{
			vertices_to_check.push(outgoing[cs].first);
		}
		was_checked[cs] = true;
	}
	for (size_t i = 1; i <= N; i++)
	{
		if (!was_checked[i] && cost_to_switch[i])
		{
			long long curr = outgoing[i].first, cval = outgoing[i].second, lval = -1;
			was_checked[i] = true;
			do 
			{
				cval = min(cval, outgoing[curr].second);
				was_checked[curr] = true;
				curr = outgoing[curr].first;
				lval = outgoing[outgoing[curr].first].second;
			} while (curr != outgoing[i].first);
			forced_switch += min(cost_to_switch[i] + cval, cost_to_switch[i] - reduced[i] + lval);
		}
	}
	for (size_t i = 1; i <= N; i++)
	{
		long long size_track = 0;
		if (!was_checked[i])
		{
			long long curr = outgoing[i].first, cval = outgoing[i].second;
			was_checked[i] = true;
			do {
				cval = min(cval, outgoing[curr].second);
				was_checked[curr] = true;
				curr = outgoing[curr].first;
				size_track++;
			} while (curr != outgoing[i].first);
			forced_switch += cval;
		}
		if (size_track == N)
		{
			cout << 0;
			return 0;
		}
	}
	cout << forced_switch;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 1ms
memory: 5560kb

input:

10
3 85377744
3 191391530
4 553475509
8 344887257
9 812158120
1 880268352
4 686078706
7 182546394
4 220137367
3 89957933

output:

1081551750

result:

ok single line: '1081551750'

Test #2:

score: -10
Wrong Answer
time: 1ms
memory: 3588kb

input:

10
3 688989554
5 112566798
1 294281317
1 96941112
6 746415334
2 612836992
9 629868215
1 406822326
10 638640051
8 199983696

output:

898497464

result:

wrong answer 1st lines differ - expected: '503789227', found: '898497464'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%