結果

問題 No.298 話の伝達
ユーザー koyumeishikoyumeishi
提出日時 2015-11-07 01:04:23
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,481 bytes
コンパイル時間 738 ms
コンパイル使用メモリ 87,812 KB
実行使用メモリ 4,476 KB
最終ジャッジ日時 2023-10-11 14:47:45
合計ジャッジ時間 1,896 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
/*
bool dfs(vector<vector<int>> &G, vector<bool> &visit, int pos){
	visit[pos] = true;
	if(pos == G.size()-1) return true;
	for(int to : G[pos]){
		if(visit[to]) continue;
		if(dfs(G,visit,to)) return true;
	}
	return false;
}

#include <cassert>

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

	assert(m<16);

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

	double ans = 0.0;
	for(int i=0; i<(1<<m); i++){
		double tmp = 1.0;
		vector<vector<int>> G(n);
		vector<bool> visit(n,false);
		for(int j=0; j<m; j++){
			if((i>>j)&1){
				tmp *= c[j]/100.0;
				G[a[j]].push_back(b[j]);
			}else{
				tmp *= 1.0 - c[j]/100.0;
			}
		}
		if(dfs(G,visit, 0)){
			ans += tmp;
		}
	}

	printf("%.12f\n", ans);

	return 0;
}
*/

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,int>>> rev(n);
	for(int i=0; i<m; i++){
		rev[b[i]].push_back({a[i],c[i]});
	}

	vector<double> p(n, 0);
	p[0] = 1.0;

	for(int z=1; z<n; z++){
		int pos = topo[z];
		int sz = rev[pos].size();
		for(int i=0; i<(1<<sz); i++){
			
			double x = 1.0;
			double y = 1.0;
			for(int j=0; j<sz; j++){
				if((i>>j)&1){
					x *= p[rev[pos][j].first];
					y *= (100-rev[pos][j].second)/100.0;
				}else{
					x *= (1-p[rev[pos][j].first]);
				}
			}
			y = 1.0 - y;

			p[pos] += x*y;
		}
	}

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


	return 0;
}
0