QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#570926#9315. Rainbow Bracket SequencezqxTL 0ms0kbC++232.9kb2024-09-17 19:10:552024-09-17 19:10:56

Judging History

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

  • [2024-09-17 19:10:56]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-09-17 19:10:55]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long 
#define all(tar) tar.begin(),tar.end()
#define pii pair<int,int>
#define AC return 0;
int n, m, t;
const int INF = 0x3f3f3f3f;
const int maxx = 5e3 + 5;
const int mod = 1e9 + 7;
int cnt = -1, ans, first[10010], dis[10010], vis[10010];
struct edge {
	int to, next, w, cap;
} a[200010];
inline void add(int u, int v, int cap, int w) {
	a[++cnt] = (edge) {
		v, first[u], w, cap
	};
	first[u] = cnt;
	a[++cnt] = (edge) {
		u, first[v], -w, 0
	};
	first[v] = cnt;
}
int q[100010];
bool spfa(int s, int t) {
	int head = 0, tail = 1, i, u, v, w;
	memset(dis, -1, sizeof(dis));
	memset(vis, 0, sizeof(vis));
	q[0] = t;
	vis[t] = 1;
	dis[t] = 0;
	while (head < tail) {
		u = q[head++];
		vis[u] = 0;
		for (i = first[u]; ~i; i = a[i].next) {
			v = a[i].to;
			w = a[i].w;
			if (a[i ^ 1].cap && ((dis[v] == -1) || (dis[v] > dis[u] - w))) {
				dis[v] = dis[u] - w;
				if (!vis[v]) q[tail++] = v, vis[v] = 1;
			}
		}
	}
	return ~dis[s];
}
int dfs(int u, int t, int limit) {
	if (u == t || !limit) {
		vis[u] = 1;
		return limit;
	}
	int i, v, f, flow = 0, w;
	vis[u] = 1;
	for (i = first[u]; ~i; i = a[i].next) {
		v = a[i].to;
		w = a[i].w;
		if (!vis[v] && (dis[v] == dis[u] - w) && (a[i].cap)) {
			if (!(f = dfs(v, t, min(limit, a[i].cap)))) continue;
			a[i].cap -= f;
			a[i ^ 1].cap += f;
			ans += f * w;
			flow += f;
			limit -= f;
			if (!limit) return flow;
		}
	}
	return flow;
}
int zkw(int s, int t) { //zkw费用流
	int re = 0;
	while (spfa(s, t)) {
		vis[t] = 1;
		while (vis[t]) {
			memset(vis, 0, sizeof(vis));
			re += dfs(s, t, 1e9);
		}
	}
	return re;
}
const int N=2005;
int id[N],c1[N],c2[N],val[N],idc[N],col[N],idx;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int T;
   cin>>T;
   while(T--){
      int n,m;
      cin>>n>>m;
      n*=2;
      for(int i=0;i<=idx;i++)first[i]=-1;
      ans=0;
      cnt=-1;
      idx=0;
      for(int i=1;i<=m;i++){
         cin>>c1[i];
         c2[i]=0;
         idc[i]=++idx;
      }
      for(int i=1;i<=n;i++){
         cin>>col[i];
         c2[col[i]]++;
      } 
      int res=0;
      for(int i=1;i<=n;i++){
        cin>>val[i];
        id[i]=++idx;
        res+=val[i];
      }
      int s=++idx;
      int t=++idx;
      add(s,id[n],n/2,0);
      bool flag=0;
      for(int i=1;i<=m;i++){
        if(c1[i]>c2[i]){
          flag=0;
          break;
        }
        add(idc[i],t,c2[i]-c1[i],0);
      }
      if(flag){
         cout<<"-1\n";
         continue;
      }
      for(int i=1;i<=n;i++){
         add(id[i],idc[col[i]],1,val[i]);
      }
      for(int i=n-1;i>=1;i--){
         add(id[i+1],id[i],i/2,0);
      }
    int sum=zkw(s,t);
    if(sum==n/2){
      cout<<res-ans<<'\n';
    }
    else cout<<"-1\n";
  }
	AC
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

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:


result: