QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#566768#9315. Rainbow Bracket SequencetarjenTL 2ms9904kbC++202.3kb2024-09-16 01:37:172024-09-16 01:37:21

Judging History

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

  • [2024-09-16 01:37:21]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:9904kb
  • [2024-09-16 01:37:17]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long 
const int inf=1e5;
const int N=1e4,M=1e6;
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;
int solve()
{
    int n,m;cin>>n>>m;
    vector<int>l(m+2);
    vector<int>c(2*n+1),v(2*n+1);
    for(int i=1;i<=m;i++)cin>>l[i];
    for(int i=1;i<=2*n;i++)cin>>c[i];
    for(int i=1;i<=2*n;i++)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 -1;
    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 -cost;
    return -1;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;while(T--)cout<<solve()<<"\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9904kb

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...

output:


result: