QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#578900 | #9315. Rainbow Bracket Sequence | wildfire032 | WA | 3ms | 16060kb | C++17 | 2.4kb | 2024-09-20 22:36:19 | 2024-09-20 22:36:19 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define int long long
const int inf=1e5;
const int N=1e4,M=1e6;
int l[M],c[M],v[M];
struct SSP {
int cnt = 1, hd[N], nxt[M << 1], to[M << 1], limit[M << 1], cst[M << 1];
void init(){
memset(hd,0,sizeof(hd));
cnt=1;
}
// w limit c cost
void add(int u, int v, int w, int c) {
nxt[++cnt] = hd[u], hd[u] = cnt, to[cnt] = v, limit[cnt] = w, cst[cnt] = c;
nxt[++cnt] = hd[v], hd[v] = cnt, to[cnt] = u, limit[cnt] = 0, cst[cnt] = -c;
}
int fr[N], fl[N], in[N], dis[N];
pair<int, int> min_cost(int s, int t) {
int flow = 0, cost = 0;
while (true) { // SPFA
queue<int> q;
memset(dis, 0x3f, sizeof(dis));
memset(in, 0, sizeof(in));
fl[s] = 1e9, dis[s] = 0, q.push(s);
while (!q.empty()) {
int cur = q.front();
q.pop(), in[cur] = 0;
for (int i = hd[cur]; i; i = nxt[i]) {
int it = to[i], d = dis[cur] + cst[i];
if (limit[i] && d < dis[it]) {
fl[it] = min(limit[i], fl[cur]), fr[it] = i, dis[it] = d;
if (!in[it]) in[it] = 1, q.push(it);
}
}
}
if (dis[t] > 1e9) return {flow, cost};//改成>0就是可行流
flow += fl[t], cost += dis[t] * fl[t];
for (int u = t; u != s; u = to[fr[u] ^ 1]) limit[fr[u]] -= fl[t], limit[fr[u] ^ 1] += fl[t];
}
}
} sol;
void solve()
{
sol.init();
int n,m;cin>>n>>m;
rep(i,1,m)cin>>l[i];
rep(i,1,n*2)cin>>c[i];
rep(i,1,n*2)cin>>v[i];
l[m+1]=n;
for(int i=1;i<=m;i++) l[m+1]-=l[i];
if(l[m+1]<0) return cout<<"-1\n",void();
int s=0,t=2*n+m+2;
for(int i=1;i<=2*n;i+=2)sol.add(s,i,1,0);
for(int i=1;i<2*n;i++)sol.add(i+1,i,inf,0);
for(int i=1;i<=2*n;i++){
sol.add(i,c[i]+2*n,1,-v[i]);
sol.add(i,m+1+2*n,1,-v[i]);
}
for(int i=1;i<=m+1;i++)sol.add(2*n+i,t,l[i],0);
auto [flow,cost]=sol.min_cost(s,t);
if(flow==n)return cout<<-cost<<"\n",void();
return cout<<"-1\n",void();
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int T;cin>>T;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 16060kb
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
Wrong Answer
time: 3ms
memory: 15912kb
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...
output:
-1 343766215 2445972178 3582951623 -1 -1 1351561190 2539318868 1013080942 4656646546 -1 -1 2231197660 2719131728 3983627641 5054286845 -1 1046749330 6317354452 4034264617 -1 4061902868 -1 2566553992 5268471900 5977120748 7505501534 -1 5191285454 1467678317 883992368 1044562986 -1 4384264660 -1 17421...
result:
wrong answer 3rd numbers differ - expected: '2351080746', found: '2445972178'