結果
| 問題 |
No.1288 yuki collection
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2020-11-22 20:45:19 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 447 ms / 5,000 ms |
| コード長 | 2,159 bytes |
| コンパイル時間 | 4,769 ms |
| コンパイル使用メモリ | 206,000 KB |
| 最終ジャッジ日時 | 2025-01-16 04:47:11 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 40 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
struct mcf{
static const int xN = 10000;
template<class T>
using nega_queue = priority_queue<T,vector<T>,greater<T>>;
struct Edge{ int u,v,r; LL c; LL d; };
int N;
int fSrc=0;
int fSnk=1;
vector<Edge> J;
vector<int> E[xN];
LL F;
LL fAns = 0;
LL fD[xN];
LL fPE[xN];
LL fP[xN]={};
void addEdge(int u,int v,LL c,LL d){
E[u].push_back(J.size());
J.push_back({u,v,(int)J.size()+1,c,d});
E[v].push_back(J.size());
J.push_back({v,u,(int)J.size()-1,0,-d});
}
void fDijkstra(){
rep(i,N) fD[i]=fPE[i]=-1;
nega_queue<pair<LL,pair<int,LL>>> Q; Q.push({0,{fSrc,-1}});
while(Q.size()){
LL d=Q.top().first;
int p=Q.top().second.first;
LL pre=Q.top().second.second;
Q.pop();
if(fD[p]!=-1) continue;
fD[p]=d;
fPE[p]=pre;
for(int j:E[p]){
if(J[j].c==0)continue;
LL nxd = d+J[j].d+fP[p]-fP[J[j].v];
if(fD[J[j].v] >= nxd) continue;
Q.push({nxd,{J[j].v,j}});
}
}
rep(i,N) fP[i]+=fD[i];
}
void flow(){
fDijkstra();
if(fD[fSnk]==-1){ F=0; fAns=-1; return; }
int p=fSnk;
LL c=F;
while(p!=fSrc){
c=min(c,J[fPE[p]].c);
p=J[fPE[p]].u;
}
p=fSnk;
while(p!=fSrc){
J[fPE[p]].c-=c;
J[J[fPE[p]].r].c+=c;
fAns += (int)c * J[fPE[p]].d;
p=J[fPE[p]].u;
}
F-=c;
}
} G;
int main(){
int N; cin>>N;
string S; cin>>S;
vector<int> S2(N); rep(i,N){ S2[i]=string("yuki").find(S[i]); }
vector<LL> V(N); rep(i,N) cin>>V[i];
G.fSrc=N*2; G.fSnk=N*2+1;
G.N = N*2+2;
static const LL maxCost = 1000000000;
int preP[4]; rep(i,4) preP[i]=-1;
rep(i,N){
int ch = S2[i];
G.addEdge(i,i+N,1,maxCost-V[i]);
if(preP[ch]!=-1) G.addEdge(preP[ch]+N,i+N,1000000,0);
if(ch!=0) if(preP[ch-1]!=-1) G.addEdge(preP[ch-1]+N,i,1000000,0);
if(ch==0) G.addEdge(G.fSrc,i,1,0);
if(ch==3) G.addEdge(i+N,G.fSnk,1,0);
preP[ch]=i;
}
LL ans=0;
for(LL f=1; ; f++){
G.F=1;
while(G.F) G.flow();
if(G.fAns==-1) break;
ans = max(ans,maxCost*4*f-G.fAns);
}
cout<<ans<<endl;
return 0;
}
Nachia