下了个测试点,答案明明对的 却WA,看了题解明明感觉是一样的,求助!!!
  • 板块P1113 杂务
  • 楼主DXL3935
  • 当前回复3
  • 已保存回复3
  • 发布时间2022/2/15 17:38
  • 上次更新2023/10/28 08:27:54
查看原帖
下了个测试点,答案明明对的 却WA,看了题解明明感觉是一样的,求助!!!
589470
DXL3935楼主2022/2/15 17:38
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int w[maxn], in[maxn], out[maxn], cost[maxn];
vector<int>g[maxn];
queue<int>q;

int main() {
	int n, x, len, v;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> x >> len;
		w[x] = len;
		while (cin >> v) {
			if (!v)
				break;
			g[v].push_back(x);
			in[x]++;
			out[v]++;
		}
	}
	for (int i = 1; i <= n; i++)
		if (!in[i]) {
			q.push(i);
			cost[i] = w[i];
		}

	while (!q.empty()) {
		int p = q.front();
		q.pop();
		for (int i = 0; i < g[p].size(); i++) {
			int t = g[p][i];
			in[t]--;
			cost[t] = max(cost[t], cost[i] + w[t]);
			if (in[t] == 0)
				q.push(t);
		}
	}

	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (out[i] == 0)
			ans = max(ans, cost[i]);
	}
	cout << ans << endl;
	return 0;
}
2022/2/15 17:38
加载中...