QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#578453#9315. Rainbow Bracket SequenceDBsoleilWA 1ms3848kbC++203.3kb2024-09-20 19:19:222024-09-20 19:19:22

Judging History

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

  • [2024-09-20 19:19:22]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3848kb
  • [2024-09-20 19:19:22]
  • 提交

answer

#define pb push_back
#define fi first
#define se second
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;

const int N=505,M=2005;
const int INF=0x3f3f3f3f;
int T,n,m;
int l[N],c[N],v[N],ss[N];

void input()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++) scanf("%d",&l[i]);
    for(int i=1;i<=n*2;i++) scanf("%d",&c[i]);
    for(int i=1;i<=n*2;i++) scanf("%d",&v[i]);
}

bool spjudge()
{
    for(int i=1,tot=0,cnt;i<=m;i++)
    {
        cnt=0,tot+=l[i];
        for(int j=1;j<=n*2;j++) if(c[j]==i) cnt++;
        if(cnt%2||tot>n) return 0;
    }
    return 1;
}

struct hlflow
{
    struct edge{int u,v,c,w;}p[M];
    int n,m,S,T,tot,ans;
    int head[N],nxt[M],deg[N];
    int dis[N],flow[N],lst[N];

    void init(int nw)
    {
        n=nw,m=1,S=++n,T=++n,ans=tot=0;
        for(int i=1;i<=n;i++) head[i]=0,deg[i]=0;
    }

    void ade(int u,int v,int c,int w) 
    {
        if(c<=0) return;
        //cerr<<u<<' '<<v<<' '<<'('<<c<<','<<w<<')'<<endl;
        p[++m]={u,v,c,w},nxt[m]=head[u],head[u]=m;
        p[++m]={v,u,0,-w},nxt[m]=head[v],head[v]=m;
    }
    void addedge(int u,int v,int l,int h,int w) {deg[u]-=l,deg[v]+=l,ans+=l*w,ade(u,v,h-l,w);}

    void build()
    {
        for(int i=1;i<=n-2;i++)
        {
            if(deg[i]>0) ade(S,i,deg[i],0);
            if(deg[i]<0) ade(i,T,-deg[i],0);
        }
    }

    bool dijkstra(int S,int T)
    {
        priority_queue<pii> pq;
        for(int i=1;i<=n;i++) dis[i]=-INF,flow[i]=0,lst[i]=-1;
        dis[S]=0,flow[S]=INF,pq.push({0,S});
        //cerr<<' '<<S<<endl;
        while(!pq.empty())
        {
            auto [d,u]=pq.top(); pq.pop();
            if(d!=dis[u]) continue;
            //cerr<<' '<<u<<' '<<flow[u]<<' '<<dis[u]<<endl;
            for(int i=head[u];i;i=nxt[i]) if(p[i].c>0)
            {
                auto [u,v,c,w]=p[i];
                if(dis[v]<d+w) 
                {
                    dis[v]=d+w,flow[v]=min(c,flow[u]),lst[v]=i;
                    pq.push(pii{dis[v],v});
                }
            }
        }
        //cerr<<' '<<flow[T]<<' '<<dis[T]<<endl;
        return flow[T]>0&&dis[T]>0;
    }

    void doflow(int S,int T)
    {
        tot+=flow[T],ans+=flow[T]*dis[T];
        for(int x=T;lst[x]>0;x=p[lst[x]].u)
        {
            int a=lst[x],b=a^1;
            p[a].c-=flow[T],p[b].c+=flow[T];
        }
    }

    int solve()
    {
        while(dijkstra(S,T)) doflow(S,T);
        //for(int i=head[S];i;i=nxt[i]) if(p[i].c>0) return -1;
        //for(int i=head[T];i;i=nxt[i]) if(p[i^1].c>0) return -1;
        return ans;
    }
}flow;

void build()
{
    flow.init(n*2+m+2);
    int S=n*2+m+1,T=n*2+m+2;
    for(int i=1;i<=m;i++) flow.addedge(S,n*2+i,l[i],n,0);
    for(int i=1;i<=n*2;i++)
    {
        //cerr<<i<<' '<<n*2+c[i]<<' '<<i<<endl;
        flow.addedge(n*2+c[i],i,0,1,v[i]);
        if(i<n*2) flow.addedge(i,i+1,i/2,n,0); 
    }
    flow.addedge(n*2,T,n,n,0);
    flow.addedge(T,S,0,INF,0);
    flow.build();
    //cerr<<' '<<flow.n<<endl;
}

void solve()
{
    input();
    if(!spjudge()) {printf("-1\n"); return;}
    build();
    int ans=flow.solve();
    printf("%d\n",ans);
}

int main()
{
    scanf("%d",&T);
    while(T--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3800kb

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: 1ms
memory: 3848kb

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
374461155
-1943886550
-1
-1
-1
1352480010
-1755648428
1093935906
-1
1354988825
-1
-1
-1
295554701
-1
-1
1194080633
2108618133
-1
-1
-1
-1
-1679379273
-1
1887461804
-1
-1
-1
1467678317
883992368
1979437452
-1
-114764893
-1
-1
-1641174143
-1
-1
-1
-1
-1422419198
-1
-561529093
935481079
-1
618382946...

result:

wrong answer 2nd numbers differ - expected: '343766215', found: '374461155'