QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#575938#9315. Rainbow Bracket SequenceRillloWA 0ms50424kbC++233.5kb2024-09-19 17:31:532024-09-19 17:31:53

Judging History

This is the latest submission verdict.

  • [2024-09-19 17:31:53]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 50424kb
  • [2024-09-19 17:31:53]
  • Submitted

answer

#include<bits/stdc++.h>

using i64 = long long;
const int V = 1000;
const int E = 1000000;
template<class T>
struct MCF {
	int s, t, n, m;
	T flow, cost, dis[V];
	int head[V], cur[V], pre[V];
	bool vis[V];
	struct edge {
		int y, nxt;
		T f, c;
	} e[E * 2];
	
	void init(int _s, int _t, int _n) {
		m = 0;
		n = _n;
		s = _s;
		t = _t;
		for (int i = 0; i < n; i++) {
			head[i] = -1;
		}
	}
	void addedge(int x, int y, T f1, T c, T f2 = 0) {
		e[m] = {y, head[x], f1, c}; head[x] = m++;
		e[m] = {x, head[y], f2, -c}; head[y] = m++;
	}
	bool spfa() {
		T inf = std::numeric_limits<T>::max() / 2;
		for (int i = 0; i < n; i++) {
			dis[i] = inf;
			vis[i] = false;
			pre[i] = -1;
		}
		dis[s] = 0;
		vis[s] = true;
		std::queue<int> q;
		q.push(s);
		while (!q.empty()) {
			int x = q.front();
			for (int i = head[x]; ~i; i = e[i].nxt) {
				int y = e[i].y;
				if (e[i].f && dis[y] > dis[x] + e[i].c) {
					dis[y] = dis[x] + e[i].c;
					pre[y] = i;
					if (!vis[y]) {
						vis[y] = 1;
						q.push(y);
					}
				}
			}
			q.pop();
			vis[x] = false;
		}
        // for (int i = 0; i < n; i++) {
        //     std::cout << dis[i] << " \n"[i == n - 1];
        // }
		return dis[t] != inf;
		
	}
	void augment() {
		int x = t;
		T f = std::numeric_limits<T>::max();
        
		while (~pre[x]) {
            std::cout << x << " ";
			f = std::min(f, e[pre[x]].f);
			x = e[pre[x] ^ 1].y;
		}
        std::cout << x<< "\n";
        
		flow += f;
		cost += f * dis[t];
        // std::cout << "flow : " << flow << " " << "cost : " << cost << "\n";
		x = t;
		while (~pre[x]) {
			e[pre[x]].f -= f;
			e[pre[x] ^ 1].f += f;
			x = e[pre[x] ^ 1].y;
		}
	}
	std::pair<T, T> work() {
		flow = 0;
		cost = 0;
		while (spfa()) augment();
		return {flow, cost};
	}
};

void solve() {
    int n, m;
    std::cin >> n >> m;
    std::vector<int> l(m), c(2 * n), v(2 * n);
    for (int i = 0; i < m; i++) {
        std::cin >> l[i];
    }
    for (int i = 0; i < 2 * n; i++) {
        std::cin >> c[i];
        c[i]--;
    }
    for (int i = 0; i < 2 * n; i++) {
        std::cin >> v[i];
    }

    int s = 4 * n + m, t = 4 * n + m + 1;
    int ss = s + 2, st = s + 3;
    MCF<i64> flow;
    flow.init(ss, st, st + 1);

    int sum = 0;
    int cs = std::accumulate(l.begin(), l.end(), 0);
    if (cs > n) {
        std::cout << -1 << "\n";
        return ;
    }

    flow.addedge(t, st, n, 0);
    flow.addedge(ss, s, n - cs, 0);
    sum += n - cs;
    for (int i = 0; i < m; i++) {
        int id = 4 * n + i;
        flow.addedge(s, id, n, 0);
        flow.addedge(ss, id, l[i], 0);
        sum += l[i];
    }
    for (int i = 0; i < 2 * n; i++) {
        int id = c[i] + 4 * n;
        flow.addedge(id, i, 1, -v[i]);  
        flow.addedge(i, st, 1, 0);
        flow.addedge(ss, i + n, 1, 0);
        sum++;
        for (int j = i + 1; j < n; j++) {
            flow.addedge(i + n, j, 1, 0);
        }
        flow.addedge(i + n, t, 1, 0);
    }
    auto[f, cost] = flow.work();
    // std::cout << f << " " << -cost << "\n";
    if (sum == f) {
        std::cout << -cost << "\n";
    } else {
        std::cout << -1 << "\n";
    }
    // std::cout << -cost << "\n";

}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout << std::fixed << std::setprecision(10);

    int t;
    std::cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 50424kb

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:

17 2 13 16
17 0 12 16
17 3 13 16
17 5 16
17 4 16
17 15 8 16
17 15 7 16
17 15 6 16
17 1 3 16
9
-1

result:

wrong answer 1st numbers differ - expected: '9', found: '17'