QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#578896#9315. Rainbow Bracket Sequencewildfire032WA 3ms15928kbC++172.4kb2024-09-20 22:30:442024-09-20 22:30:44

Judging History

你现在查看的是最新测评结果

  • [2024-09-20 22:30:44]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:15928kb
  • [2024-09-20 22:30:44]
  • 提交

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,2*n+i,1,0);
        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: 15848kb

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: 15928kb

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:

4458752148
343766215
2445972178
3582951623
2145509671
1230630334
1351561190
2539318868
1013080942
4656646546
-1
4470027409
2231197660
2719131728
3983627641
5054286845
1773576034
1046749330
6317354452
4152047219
4126606263
4061902868
4464476541
2566553992
5268471900
5977120748
7505501534
4763448964
5...

result:

wrong answer 1st numbers differ - expected: '-1', found: '4458752148'