結果

問題 No.1288 yuki collection
ユーザー milanis48663220milanis48663220
提出日時 2020-11-13 23:52:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,293 bytes
コンパイル時間 1,089 ms
コンパイル使用メモリ 95,524 KB
実行使用メモリ 75,468 KB
最終ジャッジ日時 2023-09-30 04:22:27
合計ジャッジ時間 8,060 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: メンバ関数 ‘void MinCostFlow::add_edge(int, int, ll, ll)’ 内:
main.cpp:39:63: 警告: narrowing conversion of ‘(&((MinCostFlow*)this)->MinCostFlow::G.std::vector<std::vector<edge> >::operator[](((std::vector<std::vector<edge> >::size_type)to)))->std::vector<edge>::size()’ from ‘std::vector<edge>::size_type’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
   39 |             G[from].push_back((edge){to, cap, cost, G[to].size()});
      |                                                     ~~~~~~~~~~^~
main.cpp:40:66: 警告: narrowing conversion of ‘((&((MinCostFlow*)this)->MinCostFlow::G.std::vector<std::vector<edge> >::operator[](((std::vector<std::vector<edge> >::size_type)from)))->std::vector<edge>::size() - 1)’ from ‘std::vector<edge>::size_type’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
   40 |             G[to].push_back((edge){from, 0, -cost, G[from].size()-1});
      |                                                    ~~~~~~~~~~~~~~^~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

const ll INF = 1e18;

struct edge{
    int to;
    ll cap, cost;
    int rev;
};

class MinCostFlow{
    public:
        int V;  // 頂点数
        vector<vector<edge>> G;
        MinCostFlow(int v){
            V = v;
            G = vector<vector<edge>>(v, vector<edge>());
            dist = vector<ll>(v);
            preve = vector<int>(v);
            prevv = vector<int>(v);
        }
        void add_edge(int from, int to, ll cap, ll cost){
            G[from].push_back((edge){to, cap, cost, G[to].size()});
            G[to].push_back((edge){from, 0, -cost, G[from].size()-1});
        }

        ll min_cost_flow(int s, int t, int f){
            ll res = 0;
            while(f > 0){
                fill(dist.begin(), dist.end(), INF);
                dist[s] = 0;
                bool update = true;
                while(update){
                    update = false;
                    for(int v = 0; v < V; v++){
                        if(dist[v] == INF) continue;
                        for(int i = 0; i < G[v].size(); i++){
                            edge &e = G[v][i];
                            if(e.cap > 0 && dist[e.to] > dist[v]+e.cost){
                                dist[e.to] = dist[v]+e.cost;
                                prevv[e.to] = v;
                                preve[e.to] = i;
                                update = true;
                            }
                        }
                    }
                }
                if(dist[t] == INF){
                    return -1;
                }
                ll d = f;
                for(int v = t; v != s; v = prevv[v]){
                    d = min(d, G[prevv[v]][preve[v]].cap);
                }
                f -= d;
                res += d*dist[t];
                for(int v = t; v != s; v = prevv[v]){
                    edge &e = G[prevv[v]][preve[v]];
                    e.cap -= d;
                    G[v][e.rev].cap += d;
                }
            }
            return res;
        }

    private:
        vector<ll> dist;
        vector<int> prevv, preve;
};

int n;
string s;
ll v[2000];

void input(){
    cin >> n >> s;
    for(int i = 0; i < n; i++) cin >> v[i];
}

int idx_from(int i){
    return i+1;
}

int idx_to(int i){
    return i+n+1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    input();
    MinCostFlow mcf(2*n+2);
    int f = 0, t = 2*n+1;
    int cnty = 0;
    for(int i = 0; i < n; i++){
        mcf.add_edge(idx_from(i), idx_to(i), 1, 0);
        if(s[i] == 'y'){
            cnty++;
            mcf.add_edge(0, idx_from(i), 1, 0);
            mcf.add_edge(idx_to(i), t, 1, 0);
        }
        if(s[i] == 'i'){
            mcf.add_edge(idx_to(i), t, 1, -v[i]);
        }
        for(int j = i+1; j < n; j++){
            bool find_same = false;
            bool find_after = false;
            if(!find_after){
                if(s[i] == 'y' && s[j] == 'u'){
                    mcf.add_edge(idx_to(i), idx_from(j), 1, -v[i]);
                    find_after = true;
                }
                if(s[i] == 'u' && s[j] == 'k'){
                    mcf.add_edge(idx_to(i), idx_from(j), 1, -v[i]);
                    find_after = true;
                }
                if(s[i] == 'k' && s[j] == 'i'){
                    mcf.add_edge(idx_to(i), idx_from(j), 1, -v[i]);
                    find_after = true;
                }
            }
            if(!find_same){
                if(s[i] == s[j]){
                    mcf.add_edge(idx_to(i), idx_from(j), 1, 0);
                    find_same = true;
                }
            }
        }
    }
    cout << -mcf.min_cost_flow(f, t, cnty) << endl;
}
0