結果

問題 No.19 ステージの選択
ユーザー keikei
提出日時 2018-03-31 13:00:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 5,477 bytes
コンパイル時間 1,928 ms
コンパイル使用メモリ 180,736 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 08:01:11
合計ジャッジ時間 3,142 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://yukicoder.me/problems/no/19>
 問題文============================================================
 Naomiは、とあるアクションゲームをしている。
 
 そのゲームでは、N個(1から番号がふられている)のステージがありそれぞれ難易度が設定されている。
 さらに、それぞれのステージは、先に指定されたステージをクリアしていると難易度が半分になるという仕組みになっている。
 
 選んだステージは必ずクリアできるとし、すべてのステージをクリアすると考える。
 この時、任意の順番でステージを選べるとして、各ステージの難易度の合計が最小になるように、
 ステージを選ぶとしたとき、その難易度の合計を求めてください。
 
 答えは小数になることもあるが、小数第一位まで求めるとして、丸め誤差などの誤差はないように求めてください。
 =================================================================
 解説=============================================================
 
 条件より、強連結部分については、その中で一番小さい値のステージを最初に選択することによって、
 その中でのコストを最小にできる
 
 また、選択の順番については、強連結のグループで区分けされたグラフを作り、親が存在しないグループが
 一回はそのままの難易度で挑まなければならない
 
 よって、まず、強連結成分を求め、強連結成分のグループごとの遷移のグラフを新たに作成
 
 その根(親が存在しないグループとする)のグループにおいてコスト最小となる選択を行い、
 残りのグループについて全て難易度半分としたものが答え
 
 既存ライブラリの SCCが少し使いずらかったのでもう少し機能を追加しようと思う
 
 ================================================================
 */

class SCC {
public:
    ll V;
    ll group_num;
    vector<vector<ll>> G;
    vector<vector<ll>> rG;
    vector<ll> group;
    vector<ll> List;
    vector<int> visited;
    SCC(ll V) :V(V) {
        G.resize(V); rG.resize(V);
        group.assign(V, -1);
    }
    
    void add_edge(ll u, ll v) {
        G[u].emplace_back(v);
        rG[v].emplace_back(u);
    }
    
    void dfs1(ll u) {
        visited[u] = 1;
        for (auto next : G[u]) {
            if (visited[next] == 1)continue;
            dfs1(next);
        }
        List.emplace_back(u);
    }
    
    void dfs2(ll u, ll group_num) {
        visited[u] = 1;
        group[u] = group_num;
        for (auto next : rG[u]) {
            if (visited[next] == 1)continue;
            dfs2(next, group_num);
        }
    }
    
    void solve() {
        visited.assign(V, 0);
        for (int i = 0; i < V;i++) {
            if (visited[i] == 1)continue;
            dfs1(i);
        }
        
        reverse(List.begin(), List.end());
        
        visited.assign(V, 0);
        group_num = 0;
        for (auto v : List) {
            if (visited[v] == 1)continue;
            dfs2(v, group_num);
            group_num++;
        }
    }
    
    bool same(ll u, ll v) {
        return group[u] == group[v];
    }
};

double solve(){
    double res = 0;
    int N; cin >> N;
    vector<int> L(N),S(N);
    for(int i = 0; i < N;i++){ cin >> L[i] >> S[i]; S[i]--; }
    
    SCC scc(N);
    for(int i = 0; i < N;i++){
        scc.add_edge(S[i], i);
    }
    scc.solve();
    
    vector<vector<ll>> G(scc.group_num);
    for(int i = 0; i < N;i++){
        for(auto next:scc.G[i]){
            if(scc.same(i,next)) continue;
            G[scc.group[next]].push_back(scc.group[i]);
        }
    }
    
    vector<int> roots;
    for(int i = 0; i < scc.group_num;i++){
        if(G[i].size()) continue;
        roots.push_back(i);
    }

    vector<int> used(N,0);
    for(auto root:roots){
        int Sum = 0;
        int Minv = INF;
        for(int i = 0; i < N;i++){
            if(root == scc.group[i]){
                used[i] = 1;
                Sum += L[i];
                Minv = min(Minv,L[i]);
            }
        }
        res += (Sum+Minv)/2.0;
    }
    for(int i = 0; i < N;i++){
        if(used[i] == 1) continue;
        res += L[i]/2.0;
    }
    
    return res;
}
int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    cout << fixed << setprecision(1) << solve() << endl;
    return 0;
}
0