結果

問題 No.30 たこやき工場
ユーザー femtofemto
提出日時 2016-09-26 02:20:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,258 bytes
コンパイル時間 1,023 ms
コンパイル使用メモリ 96,048 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 05:18:18
合計ジャッジ時間 1,872 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <queue>
using namespace std;

typedef pair<int, int> P;

vector<vector<P> > G1;
vector<vector<int> > G2;
int S[100];

vector<int> tsort(const vector<vector<int> > &g) {
	int n = g.size(), k = 0;
	vector<int> ord(n), in(n);
	for(auto &es : g) for(auto &e : es) in[e]++;
	queue<int> q;
	for(int i = 0; i < n; ++i) if(in[i] == 0) q.push(i);
	while(q.size()) {
		int v = q.front(); q.pop(); ord[k++] = v;
		for(auto &e : g[v]) if(--in[e] == 0) q.push(e);
	}
	return *max_element(in.begin(), in.end()) == 0 ? ord : vector<int>();
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N, M;
	cin >> N >> M;

	G1 = vector<vector<P> >(N);
	G2 = vector<vector<int> >(N);
	for(int i = 0; i < M; i++) {
		int P, Q, R;
		cin >> P >> Q >> R;
		P--, R--;
		G1[R].emplace_back(P, Q);
		G2[R].emplace_back(P);
	}

	vector<int> top = tsort(G2);
	S[N - 1] = 1;
	for(int i = 0; i < N; i++) {
		int u = top[i];
		if(S[u] && G1[u].size()) {
			for(P p : G1[u]) {
				int v = p.first, q = p.second;
				S[v] += q * S[u];
			}
			S[u] = 0;
		}
	}

	for(int i = 0; i < N - 1; i++) {
		cout << S[i] << endl;
	}
}
0