結果

問題 No.298 話の伝達
ユーザー koyumeishikoyumeishi
提出日時 2015-11-07 02:20:48
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 4,498 bytes
コンパイル時間 1,015 ms
コンパイル使用メモリ 101,484 KB
実行使用メモリ 175,236 KB
最終ジャッジ日時 2023-10-11 14:53:05
合計ジャッジ時間 8,234 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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, int goal){
	visit[pos] = true;
	if(pos == goal) return true;
	for(int to : G[pos]){
		if(visit[to]) continue;
		if(dfs(G,visit,to,goal)) return true;
	}
	return false;
}

#include <cassert>

void solver(int n, int m, vector<int> a, vector<int> b, vector<int> c){
	for(int ___=0; ___<n; ___++){
		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 ", ans);
	}
	printf("\n");

}


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;
}


//lexicographically smallest
// O(N logN)
//vector<vector<int> > &G is edge list. G[i][j] := edge goes to node[G[i][j]] from node[i] 
//vector<set<int> > from . from[i] := nodes set which have edge going to node[i]
//set<int> begin := the nodes those indegree is zero

	vector<int> TopologicalSorting_lex_smallest(vector<vector<int> > &G, vector< set<int> > &from, set<int> &begin){
		vector<int> ret;
		while( begin.size() > 0 ){
			int val = *(begin.begin());	// using "begin.rbegin()", lexicographically largest one
			begin.erase( val );
			ret.push_back(val);
			for(auto itr = G[val].begin() ; itr != G[val].end(); itr++){
				int m = *itr;
				if( from[m].size() == 1 ){
					begin.insert(m);
				}
				from[m].erase(val);
			}
		}
		return ret;
	}

	vector<int> TopologicalSorting_lex_smallest_call(vector<vector<int>> &G){
		int n = G.size();
		vector<set<int>> rev(n);
		set<int> begin;
		for(int i=0; i<n; i++){
			for(int e: G[i]){
				rev[e].insert(i);
			}
		}
		for(int i=0; i<n; i++){
			if(rev[i].size() == 0){
				begin.insert(i);
			}
		}

		return TopologicalSorting_lex_smallest(G, rev, begin);
	}

template<class T>
ostream& operator << (ostream& os, vector<T> vec){
	for(int i=0; i<vec.size(); i++){
		os << vec[i] << " ";
	}
	return os;
}

#include <bitset>

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];
	}

	solver(n,m,a,b,c);

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

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

	//cerr << topo << endl;

	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[0][1] = 1.0;
	for(int i=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];
			}
		}
	}

/*

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

	for(int z=1; z<n; z++){
		int pos = topo[z];
		cerr << pos << endl;
		int sz = rev[pos].size();
		for(int i=0; i<(1<<sz); i++){

			cerr << bitset<6>(i) << endl;
			
			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 *= (1-rev[pos][j].second);
				}else{
					x *= (1-p[rev[pos][j].first]);
				}
			}
			y = 1.0 - y;

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

*/
/*
	for(int i=0; i<n; i++){
		
		printf("%.12f ", p[i]);
	}

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