結果

問題 No.158 奇妙なお使い
ユーザー HachimoriHachimori
提出日時 2015-03-04 00:22:13
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,195 bytes
コンパイル時間 923 ms
コンパイル使用メモリ 81,680 KB
実行使用メモリ 31,284 KB
最終ジャッジ日時 2023-09-06 06:39:53
合計ジャッジ時間 9,116 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 607 ms
31,284 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,384 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 185 ms
4,376 KB
testcase_18 AC 59 ms
7,408 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 60 ms
4,380 KB
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;


class Data {
public:
    int n1000, n100, n1;
    Data(){}
    Data(int n1000, int n100, int n1):
        n1000(n1000), n100(n100), n1(n1){}

    bool operator< (const Data &opp) const {
        if (n1000 != opp.n1000) return n1000 < opp.n1000;
        if (n100 != opp.n100) return n100 < opp.n100;
        return n1 < opp.n1;
    }
};


Data init;
int payA, chgN1000A, chgN100A, chgN1A;
int payB, chgN1000B, chgN100B, chgN1B;

void read() {
    cin >> init.n1000 >> init.n100 >> init.n1;
    cin >> payA >> chgN1000A >> chgN100A >> chgN1A;
    cin >> payB >> chgN1000B >> chgN100B >> chgN1B;
}


Data doPay(int toPay, int chgN1000, int chgN100, int chgN1, Data curr) {
    int remain = toPay;
    int n;

    n = min(remain / 1000, curr.n1000);
    remain -= n * 1000;
    curr.n1000 -= n;

    n = min(remain / 100, curr.n100);
    remain -= n * 100;
    curr.n100 -= n;

    n = min(remain, curr.n1);
    remain -= n;
    curr.n1 -= n;
    
    if (remain == 0)
        return Data(curr.n1000 + chgN1000, curr.n100 + chgN100, curr.n1 + chgN1);
    else
        return Data(-1, -1, -1);
}


void work() {
    queue<Data> Q;
    map<Data, int> d2cnt;
    
    Q.push(init);
    d2cnt[init] = 0;
    
    while (!Q.empty()) {
        Data curr = Q.front();
        Q.pop();
        
        Data next;

        // A
        next = doPay(payA, chgN1000A, chgN100A, chgN1A, curr);
        
        if (next.n1000 != -1) {
            if (!d2cnt.count(next) || d2cnt[next] < d2cnt[curr] + 1) {
                d2cnt[next] = d2cnt[curr] + 1;
                Q.push(next);
            }
        }
        
        // B
        next = doPay(payB, chgN1000B, chgN100B, chgN1B, curr);

        if (next.n1000 != -1) {
            if (!d2cnt.count(next) || d2cnt[next] < d2cnt[curr] + 1) {
                d2cnt[next] = d2cnt[curr] + 1;
                Q.push(next);
            }
        }
    }

    int ans = 0;
    for (map<Data, int>::iterator it = d2cnt.begin(); it != d2cnt.end(); ++it)
        ans = max(ans, it->second);
    cout << ans << endl;
}


int main() {
    read();
    work();
    return 0;
}
0