QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#567919#9315. Rainbow Bracket SequenceLavender_FieldTL 0ms3780kbC++202.9kb2024-09-16 14:39:002024-09-16 14:39:00

Judging History

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

  • [2024-09-16 14:39:00]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3780kb
  • [2024-09-16 14:39:00]
  • 提交

answer

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(Int i=a;i>=b;i--)
#define pb push_back
typedef long long ll;
using namespace std;

inline int rd() {
    int r = 0; bool w = false; char ch = getchar();
    while( ch < '0' || ch > '9' ) w = !(ch^45), ch = getchar();
    while( ch >= '0' && ch <= '9' ) r = (r<<1) + (r<<3) + (ch^48), ch = getchar();
    return w ? -r : r;
}

#define MAXN 100
#define MAXM 100
#define MAXE 20000
#define MAXD 500
const int INF = 1e9;

int n, m, s, t, tr, ans, ans_edg;
ll cost, total; 
int lim[MAXM+5], v[MAXN*2+5], c[MAXN*2+5];
struct Edge
{
	int fwd,nxt,wht;
    ll dis;
}edg[MAXE+5];
int head[MAXD+5],edg_num=1;
inline void add( int u , int v , int w , int d )
{
	++edg_num;
	edg[edg_num].fwd=v;
	edg[edg_num].wht=w;
	edg[edg_num].dis=d;
	edg[edg_num].nxt=head[u];
	head[u]=edg_num;
}
inline void adde( int u , int v , int w , int d ) {
    add(u,v,w,d);
    add(v,u,0,-d);
}
vector<int> chk_max;

ll dist[MAXD+5];
int flow[MAXD+5],pre[MAXD+5];
bool vis[MAXD+5];
queue<int> q;

inline bool SPFA()
{
	memset(dist,0x3c,sizeof(dist));
	memset(vis,false,sizeof(vis));
	while( !q.empty() ) q.pop();
	q.push(s),dist[s]=0,flow[s]=INF;
	while( !q.empty() )
	{
		int u=q.front();q.pop();
		vis[u]=false;
		for(int i=head[u];i;i=edg[i].nxt) if( edg[i].wht )
		{
			int v=edg[i].fwd;
			if( dist[v]>dist[u]+edg[i].dis )
			{
				dist[v]=dist[u]+edg[i].dis;
				pre[v]=i;
				flow[v]=min(flow[u],edg[i].wht);
				if( !vis[v] )
					q.push(v),vis[v]=true;
			}
		}
	}
	return dist[t]!=dist[0];
}

inline void MCMF()
{
	while( SPFA() )
	{
		ans+=flow[t],cost+=dist[t]*flow[t];
		int p=pre[t];
		while( p )
		{
			edg[p].wht-=flow[t],edg[p^1].wht+=flow[t];
			p=pre[edg[p^1].fwd];
		}
	}
}

void solve() {
    edg_num = 1;
    memset(head, 0, sizeof(head));
    chk_max.clear();
    n = rd(), m = rd();
    s = 3 * n + m + 1; tr = s + 1; t = tr + 1;
    cost = ans = 0;
    total = (ll)(1e9+1) * n;
    FOR(i,1,m) lim[i] = rd();
    FOR(i,1,2*n) c[i] = rd();
    FOR(i,1,2*n) v[i] = rd();
    FOR(i,1,n) adde(s,i,1,0);
    FOR(i,1,n) {
        FOR(j,i,2*i-1)
            adde(i,n+j,1,0);
    }
    FOR(i,1,2*n) {
        adde(n+i,3*n+c[i],1,1e9+1-v[i]);
    }
    FOR(i,1,m) {
        adde(s, tr, lim[i], 0);
        chk_max.pb(edg_num-1);
        adde(3*n+i, t, lim[i], 0);
        chk_max.pb(edg_num-1);
        adde(3*n+i, tr, n, 0);
    }
    adde(tr, t, n, 0);
    ans_edg = edg_num-1;

    MCMF();

    if( edg[ans_edg].wht != 0 ) {
        // printf("%d\n", ans);
        puts("-1");
        return;
    }
    for(auto e: chk_max) if( edg[e].wht != 0 ) {
        // puts("edg-1");
        puts("-1");
        return;
    }
    printf("%lld\n", total - cost);
}

int main() {
    int T = rd(); while( T-- ) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: