結果

問題 No.1288 yuki collection
ユーザー milanis48663220milanis48663220
提出日時 2020-11-14 00:23:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,478 bytes
コンパイル時間 1,473 ms
コンパイル使用メモリ 101,180 KB
実行使用メモリ 78,364 KB
最終ジャッジ日時 2024-07-22 22:26:28
合計ジャッジ時間 8,185 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,948 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 3 ms
6,944 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: In member function 'void MinCostFlow::add_edge(int, int, ll, ll)':
main.cpp:40:63: warning: 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]
   40 |             G[from].push_back((edge){to, cap, cost, G[to].size()});
      |                                                     ~~~~~~~~~~^~
main.cpp:41:66: warning: 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]
   41 |             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;
typedef pair<ll, int> P;

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;
                priority_queue<P, vector<P>, greater<P>> que;
                que.push(P(0, s));
                while(!que.empty()){
                    P top = que.top(); que.pop();
                    int v = top.second;
                    ll d = top.first;
                    if(dist[v] != d) 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;
                            que.push(P(dist[e.to], e.to));
                        }
                    }
                }
                
                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;
    ll max_edge = 1e15;
    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, 4*max_edge);
        }
        if(s[i] == 'i'){
            mcf.add_edge(idx_to(i), t, 1, max_edge-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, max_edge-v[i]);
                    find_after = true;
                }
                if(s[i] == 'u' && s[j] == 'k'){
                    mcf.add_edge(idx_to(i), idx_from(j), 1, max_edge-v[i]);
                    find_after = true;
                }
                if(s[i] == 'k' && s[j] == 'i'){
                    mcf.add_edge(idx_to(i), idx_from(j), 1, max_edge-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 << 4*max_edge*cnty-mcf.min_cost_flow(f, t, cnty) << endl;
}
0