結果

問題 No.298 話の伝達
ユーザー koyumeishikoyumeishi
提出日時 2015-11-07 02:29:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 229 ms / 5,000 ms
コード長 1,898 bytes
コンパイル時間 809 ms
コンパイル使用メモリ 91,124 KB
実行使用メモリ 175,380 KB
最終ジャッジ日時 2023-10-11 14:53:27
合計ジャッジ時間 2,772 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 185 ms
175,356 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 7 ms
7,296 KB
testcase_11 AC 154 ms
175,380 KB
testcase_12 AC 80 ms
85,056 KB
testcase_13 AC 148 ms
175,212 KB
testcase_14 AC 79 ms
85,108 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 229 ms
175,160 KB
testcase_17 AC 6 ms
5,020 KB
testcase_18 AC 67 ms
42,256 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 121 ms
85,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;


void TopologicalSort_dfs(vector<vector<int> > &G, vector<int> &res, int node, vector<bool> &visit){
	if(visit[node] == true) return;
	visit[node] = true;
	for(auto itr = G[node].rbegin(); itr != G[node].rend(); itr++){
		TopologicalSort_dfs(G, res, *itr, visit);
	}
	/*
	for(int i=0; i<G[node].size(); i++){
		TopologicalSort(G, res, G[node][i], visit);
	}
	*/
	res.push_back(node);
}

vector<int> TopologicalSort(vector<vector<int>> &G){
	int n = G.size();
	vector<int> ret;
	vector<bool> visit(n,false);
	for(int i=0; i<n; i++){
		if(visit[i]) continue;
		TopologicalSort_dfs(G, ret, i, visit);		
	}
	reverse(ret.begin(), ret.end());
	return ret;
}


int main(){
	int n,m;
	cin >> n >> m;

	vector<int> a(m),b(m),c(m);
	for(int i=0; i<m; i++){
		cin >> a[i] >> b[i] >> c[i];
	}


	vector<vector<int>> G(n);
	for(int i=0; i<m; i++){
		G[a[i]].push_back(b[i]);
	}

	auto topo = TopologicalSort(G);
	vector<int> topo_inv(n);
	for(int i=0; i<n; i++){
		topo_inv[topo[i]] = i;
	}

	vector<vector<pair<int,double>>> rev(n);
	for(int i=0; i<m; i++){
		rev[b[i]].push_back({a[i],c[i]/100.0});
	}

	vector<vector<double>> dp(n, vector<double>(1<<n, 0));
	dp[topo_inv[0]][1<<topo_inv[0]] = 1.0;
	
	for(int i=topo_inv[0]+1; i<n; i++){
		int pos = topo[i];
		for(int s=0; s<(1<<i); s++){
			double x = dp[i-1][s];
			double y = 1.0;
			for(auto z: rev[pos]){
				int k = topo_inv[z.first];
				if((s>>k)&1){
					y *= 1.0 - z.second;
				}
			}
			y = 1.0-y;

			dp[i][s|(1<<i)] += x*y;
			dp[i][s|(0<<i)] += x*(1-y);
		}
	}

	vector<double> p(n, 0);
	for(int i=0; i<(1<<n); i++){
		for(int j=0; j<n; j++){
			if((i>>j)&1){
				p[topo[j]] += dp[n-1][i];
			}
		}
	}

	printf("%.12f\n", p[n-1]);
	return 0;
}
0