QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#606910 | #9315. Rainbow Bracket Sequence | hungry | TL | 1ms | 4128kb | C++20 | 3.0kb | 2024-10-03 12:57:14 | 2024-10-03 12:57:15 |
Judging History
answer
#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
const int N = 310;
typedef long long ll;
struct Edge{
int p, v;
ll w;
};
struct MCMF{
int sz, id[N][N];
bool vis[N];
vector<Edge> tu[N];
int S, T;
int max_flow;
ll dis[N], min_cost;
void init(){
for(int i = 0; i <= sz; i++){
fill(id[i], id[i]+sz+1, 0), tu[i].clear(), vis[i] = false;
}
S = T = 0;
max_flow = min_cost = 0;
}
int new_p(){
return ++sz;
}
void add_edge(int x, int y, int v, int w){
tu[x].push_back({y, v, w}), id[x][y] = tu[x].size()-1;
tu[y].push_back({x, 0, -w}), id[x][y] = tu[y].size()-1;
}
bool spfa(){
fill(vis, vis+sz+1, 0);
fill(dis, dis+sz+1, 1e15);
dis[S] = 0;
queue<int> q;
q.push(S), vis[S] = 1;
while(!q.empty()){
int x = q.front(); q.pop();
vis[x] = false;
for(auto E: tu[x]){
if(E.v && dis[E.p] > dis[x] + E.w){
dis[E.p] = dis[x] + E.w;
if(!vis[E.p]) q.push(E.p), vis[E.p] = true;
}
}
}
return dis[T] < 1e14;
}
ll dfs(int x, int flow){
if(x == T) return flow;
int res = flow;
vis[x] = true;
for(auto &E: tu[x]){
if(!E.v) continue;
if(!vis[E.p] && dis[E.p] == dis[x] + E.w){
int tf = dfs(E.p, min(res, E.v));
res -= tf;
E.v -= tf, tu[E.p][id[E.p][x]].v += tf;
min_cost += E.w * tf;
if(!res) break;
}
}
vis[x] = false;
return flow - res;
}
pair<int, ll> dinic(){
while(spfa()){
int f = dfs(S, 1e9);
while(f) max_flow += f, f = dfs(S, 1e9);
}
return {max_flow, min_cost};
}
};
MCMF F;
int n, m;
int col[N], val[N], lim[N];
ll solve(){
static int ic[N], id[N];
F.S = F.new_p(), F.T = F.new_p();
for(int i = 1; i <= m; i++) ic[i] = F.new_p();
for(int i = 1; i <= 2*n; i++) id[i] = F.new_p();
for(int i = 1; i <= 2*n; i++) lim[col[i]]--;
for(int i = 1; i <= m; i++){
if(lim[i] > 0) return -1;
F.add_edge(F.S, ic[i], -lim[i], 0);
}
ll sum = 0;
for(int i = 1; i <= 2*n; i++){
sum += val[i];
F.add_edge(ic[col[i]], id[i], 1, val[i]);
}
for(int i = 1; i < 2*n; i++) F.add_edge(id[i], id[i+1], i/2, 0);
F.add_edge(id[2*n], F.T, n, 0);
auto ans = F.dinic();
if(ans.X != n) return -1;
return sum - ans.Y;
}
int main(){
int t; scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) scanf("%d", &lim[i]);
for(int i = 1; i <= 2*n; i++) scanf("%d", &col[i]);
for(int i = 1; i <= 2*n; i++) scanf("%d", &val[i]);
printf("%lld\n", solve());
F.init();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 4128kb
input:
2 3 2 1 2 1 2 2 2 1 2 3 1 4 2 2 1 3 2 2 2 1 2 2 2 1 2 3 1 4 2 2 1
output:
9 -1
result:
ok 2 number(s): "9 -1"
Test #2:
score: -100
Time Limit Exceeded
input:
50 8 6 0 2 2 0 3 1 3 5 1 1 5 3 5 2 3 2 2 1 2 2 4 6 998133227 879226371 59632864 356493387 62611196 827258251 296576565 204244054 812713672 780267148 614679390 447700005 102067050 544546349 116002772 761999375 1 1 1 1 1 343766215 374461155 3 1 2 1 1 1 1 1 1 796323508 303640854 701432076 853325162 610...