結果

問題 No.30 たこやき工場
ユーザー A8pfA8pf
提出日時 2018-11-06 14:38:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,503 bytes
コンパイル時間 1,231 ms
コンパイル使用メモリ 147,888 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-23 05:20:54
合計ジャッジ時間 2,025 ms
ジャッジサーバーID
(参考情報)
judge13 / 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 1 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

class Edge
{
public:
  int to, from;
  ll cost;
  Edge(int n, int c)
  {
    to = n;
    cost = c;
  }
};

vector<Edge> g[100];
ll memo[100][100]; //make one i needed how many j
int n, m;

//memo[a][i]を求める
void saiki(int a)
{
	//すでに調べてあるものはもう調べなくても良い
	if(memo[a][0]>=0)
		return;
	//逆辺がないなら直接買うしかない
	if(g[a].empty())	
	{
		fill(memo[a],memo[a]+100,0);
		memo[a][a]=1;
		return;
	}

	//逆辺がある場合は調べる
	for(int i=0;i<g[a].size();i++)
	{
		Edge e=g[a][i];
		//逆辺があるものはそこに(すでに求まっているかどうか)調べに行く
		saiki(e.to);
		//求まったものに対して加算していく
		for(int j=0;j<n;j++)
		{
			//aを作るために必要なjの数 = e.cost * e.toを作るために必要なjの数
			if(memo[a][j]==-1)
				memo[a][j]=0; //初期値のままなら戻しておく
			memo[a][j]+=e.cost*memo[e.to][j];
		}
	}
	return;
}

int main()
{
  cin >> n >> m;
  fill(memo[0], memo[100], -1);
  for (int i = 0; i < m; i++)
  {
    int p, q, r;
    cin >> p >> q >> r;
    //逆辺を張る
    //JP comment, zettai abuse
    p--;
    r--;
    //memo[r][p] = q;
    g[r].push_back(Edge(p, q));
  }

	saiki(n-1);
	/*
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
			cerr<<memo[i][j]<<" ";
		cerr<<endl;
	}*/
  for (int i = 0; i < n - 1; i++)
    cout << memo[n - 1][i] << endl;
  return 0;
}
0