QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#592520#9315. Rainbow Bracket SequenceAshbourneWA 1ms3964kbC++143.5kb2024-09-26 23:09:312024-09-26 23:09:31

Judging History

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

  • [2024-09-26 23:09:31]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3964kb
  • [2024-09-26 23:09:31]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2555;
const int INF = 0x3f3f3f3f;
struct Edge{
        int to, cap, wt, next;
}edge[N * 100];
int tot = 1, head[N];
int s, t, cnt = 1, num;
void add(int u, int v, int w, int x){
    edge[++tot] = {v, w, x, head[u]};
    head[u] = tot;
    edge[++tot] = {u, 0, -x, head[v]};
    head[v] = tot;
}
int dis[N], vis[N], maxflow = 0;
void SPFA(){
    queue<int> que;
    memset(dis, 0x3f, sizeof dis[0] * (num + 1));
    que.push(s);
    vis[s] = 1, dis[s] = 0;
    while(!que.empty()){
        int u = que.front();
        que.pop();
        vis[u] = 0;
        for(int i = head[u]; i; i = edge[i].next){
            int v = edge[i].to;
            if(edge[i].cap && dis[v] > dis[u] + edge[i].wt){
                dis[v] = dis[u] + edge[i].wt;
                if(!vis[v]) que.push(v), vis[v] = 1;
            }
        }
    }
}
int dis_dij[N], cur[N];
struct node{
    int dis, u;
    friend bool operator < (node a, node b){
        return a.dis > b.dis;
    }
};
bool dij(){
    priority_queue<node> que; // greater number should change
    memset(dis_dij, 0x3f, sizeof dis_dij[0] * (num + 1));
    dis_dij[s] = 0;
    que.push({0, s});
    while(!que.empty()){
        int u = que.top().u;
        que.pop();
        if(vis[u] == cnt) continue;
        vis[u] = cnt; cur[u] = head[u];
        for(int i = head[u]; i; i = edge[i].next){
            int v = edge[i].to;
            if(edge[i].cap && dis_dij[v] > dis_dij[u] + edge[i].wt + dis[u] - dis[v]){
                    dis_dij[v] = dis_dij[u] + edge[i].wt + dis[u] - dis[v];
                    if(vis[v] != cnt) que.push({dis_dij[v], v});
            }
        }
    }
    return dis_dij[t] < INF;
}
int dfs(int u, int flow){
    if(u == t) return flow;
    int ret = 0, x;
    vis[u] = cnt;
    for(int i = cur[u]; i; i = cur[u]){
        cur[u] = edge[i].next;
        int v = edge[i].to;
        if(edge[i].cap == 0 || vis[v] == cnt || dis_dij[v] != dis_dij[u] + edge[i].wt + dis[u] - dis[v]) continue;
        x = dfs(v, min(flow, edge[i].cap));
        if(x){
            flow -= x; ret += x;
            edge[i].cap -= x, edge[i ^ 1].cap += x;
            maxflow += x * edge[i].wt;
            if(flow == 0) break;
        }
    }
    vis[u] = 0;
    return ret;
}
int dinic(){
    int flow = 0;
    SPFA();
    while(dij()){
        ++cnt, flow += dfs(s, INF);
        for(int i = 1; i <= num; ++ i) dis[i] += dis_dij[i];
    }
    return flow;
}
void clear(){
    memset(head, 0, sizeof head);
    tot = 1; cnt = 1; maxflow = 0;
    memset(vis, 0, sizeof vis);
}
void solve(){
	clear();
	int n, m, sum = 0, num1 = 0;
	cin >> n >> m;
	vector<int> l(m + 1), col(2 * n + 1), val(2 * n + 1);
	for(int i = 1; i <= m; ++ i) cin >> l[i], sum += l[i];
	for(int i = 1; i <= 2 * n; ++ i) cin >> col[i];
	for(int i = 1; i <= 2 * n; ++ i) cin >> val[i];
	int tot = m + 2 * n;
	s = tot + 3, t = tot + 4; num = tot + 4;
	for(int i = 1; i <= m; ++ i){
		add(tot + 1, i, INF, 0);
		add(s, i, l[i], 0);
	}
	for(int i = 1; i <= 2 * n; ++ i){
		add(col[i], m + i, 1, -val[i]);
		if(i & 1) num1++, add(m + i, t, 1, 0);
		add(m + i, m + i + 1, n - num1, 0);
	}
	sum = n - sum;
	if(sum < 0) {
		cout << -1 << endl;
		return;
	}
	add(s, tot + 1, sum, 0);
	int flow;
	flow = dinic();
	// cout << flow << endl;
	if(flow != n){
		cout << -1 << endl;
		return;
	}
	cout << -maxflow << endl;
}
signed main(){
	ios::sync_with_stdio(0);
	int T; cin >> T;
	while(T--){
		solve();
	}
}

详细

Test #1:

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

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: 0
Accepted
time: 1ms
memory: 3568kb

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
343766215
2351080746
3426965219
-1
-1
1351561190
2539318868
1013080942
4656646546
-1
-1
2231197660
2719131728
3983627641
4712174168
-1
1046749330
6115214757
3920988203
-1
3902088946
-1
2566553992
5268471900
5977120748
7505501534
-1
5054275471
1467678317
883992368
1044562986
-1
4024634503
-1
14472...

result:

ok 50 numbers

Test #3:

score: 0
Accepted
time: 1ms
memory: 3776kb

input:

25
20 4
0 0 0 1
2 2 2 2 4 4 4 1 2 2 2 1 3 4 1 3 4 4 3 1 3 2 1 4 2 2 4 1 2 2 3 3 4 1 3 1 4 1 2 1
569363376 821862673 89663213 862407789 442940149 823902948 903631686 838830270 645793571 397350060 806574247 373166844 448916252 435880456 969841293 998227951 276194969 654967687 396909705 696186514 16992...

output:

16418796680
10558213381
-1
1502390329
8534183652
13571062458
8007768610
12656453595
3378659874
8410746077
12352187024
5743130624
5952844559
2285684441
4242613506
836778846
4838639494
8586807028
8535185475
8089358175
5627473863
14399529671
-1
11483578919
4919631490

result:

ok 25 numbers

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 3964kb

input:

83
6 5
1 0 1 1 0
2 4 4 5 3 2 4 5 3 3 3 3
597659626 649040970 33207011 223207847 960704874 418600362 658594226 417168695 767527655 622701955 867509363 235369723
6 2
0 0
1 1 2 2 2 2 1 1 1 2 2 1
405752009 976807343 267881918 26193206 947664189 555835767 587219021 231445627 755417826 541362608 846129246...

output:

-1
4518989634
3550182642
4529809699
4042429510
4145000717
-1
3635082691
-1
-1
3476472607
3732904849
3631909633
4479534795
-1
3380039505
2946284506
3615784040
-1
-1
-1
4940773463
3195952843
4073152216
4177883697
3398540362
3578975642
4308395607
-1
3078447178
3069102942
3135294474
5256676097
-1
327863...

result:

wrong answer 15th numbers differ - expected: '3586223781', found: '-1'