結果
問題 | No.298 話の伝達 |
ユーザー | koyumeishi |
提出日時 | 2015-11-07 02:21:41 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 249 ms / 5,000 ms |
コード長 | 4,500 bytes |
コンパイル時間 | 1,117 ms |
コンパイル使用メモリ | 107,188 KB |
実行使用メモリ | 175,396 KB |
最終ジャッジ日時 | 2024-09-13 13:45:40 |
合計ジャッジ時間 | 3,100 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,948 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,944 KB |
testcase_05 | AC | 202 ms
175,176 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 3 ms
6,940 KB |
testcase_10 | AC | 7 ms
7,304 KB |
testcase_11 | AC | 175 ms
175,380 KB |
testcase_12 | AC | 91 ms
85,088 KB |
testcase_13 | AC | 171 ms
175,396 KB |
testcase_14 | AC | 91 ms
85,048 KB |
testcase_15 | AC | 2 ms
6,944 KB |
testcase_16 | AC | 249 ms
175,248 KB |
testcase_17 | AC | 6 ms
6,944 KB |
testcase_18 | AC | 72 ms
42,240 KB |
testcase_19 | AC | 2 ms
6,940 KB |
testcase_20 | AC | 127 ms
85,108 KB |
ソースコード
#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; }