QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#570136 | #9315. Rainbow Bracket Sequence | paoxiaomo | WA | 0ms | 3692kb | C++20 | 4.3kb | 2024-09-17 14:10:54 | 2024-09-17 14:10:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
// #define max(a, b) ((a) > (b) ? (a) : (b))
// #define min(a, b) ((a) < (b) ? (a) : (b))
#define pb push_back
#define LNF 1e18
#define INF 0x7fffffff
#define int long long
#define lowbit(x) ((x) & (-x))
#define abs(x) llabs(x)
#define endl '\n'
#define Y() cout << "Yes" << endl
#define N() cout << "No" << endl
const db eps = 1e-9;
const int mod = 998244353;
const int MAXN = 2001;
const int MAXM = 8001;
// 最小费用最大流
struct MCMF // 如果求最大费用则费用取反
{
int head[MAXN], cnt = 1;
int maxflow, mincost;
void init(int nu)
{ // 要跑多次先初始化
fill(head, head + nu + 1, 0);
maxflow = 0, mincost = 0;
cnt = 1;
}
struct Edge
{
int to, w, c, next;
} edges[MAXM * 2];
inline void add(int from, int to, int w, int c)
{
edges[++cnt] = {to, w, c, head[from]};
head[from] = cnt;
}
inline void addEdge(int from, int to, int w, int c)
{
add(from, to, w, c);
add(to, from, 0, -c);
}
int s, t, dis[MAXN], cur[MAXN];
bool inq[MAXN], vis[MAXN];
queue<int> Q;
bool SPFA()
{
while (!Q.empty())
Q.pop();
copy(head, head + MAXN, cur);
fill(dis, dis + MAXN, INF);
dis[s] = 0;
Q.push(s);
while (!Q.empty())
{
int p = Q.front();
Q.pop();
inq[p] = 0;
for (int e = head[p]; e != 0; e = edges[e].next)
{
int to = edges[e].to, vol = edges[e].w;
if (vol > 0 && dis[to] > dis[p] + edges[e].c)
{
dis[to] = dis[p] + edges[e].c;
if (!inq[to])
{
Q.push(to);
inq[to] = 1;
}
}
}
}
return dis[t] != INF;
}
int dfs(int p, int flow)
{
if (p == t)
return flow;
vis[p] = 1;
int rmn = flow;
for (int eg = cur[p]; eg && rmn; eg = edges[eg].next)
{
cur[p] = eg;
int to = edges[eg].to, vol = edges[eg].w;
if (vol > 0 && !vis[to] && dis[to] == dis[p] + edges[eg].c)
{
int c = dfs(to, min(vol, rmn));
rmn -= c;
edges[eg].w -= c;
edges[eg ^ 1].w += c;
}
}
vis[p] = 0;
return flow - rmn;
}
inline void run(int ss, int tt)
{
s = ss, t = tt;
while (SPFA())
{
int flow = dfs(s, INF);
maxflow += flow;
mincost += dis[t] * flow;
}
}
} mcmf; // namespace MCMF
void solve()
{
int n, m, sum = 0;
cin >> n >> m;
n *= 2;
mcmf.init(n + n + m + 3);
vector<int> cnt(m + 1), co(n + 1), val(n + 1);
vector<vector<int>> nu(m + 1);
for (int i = 1; i <= m; i++)
cin >> cnt[i];
for (int i = 1; i <= n; i++)
{
cin >> co[i];
nu[co[i]].push_back(i);
}
for (int i = 1; i <= n; i++)
cin >> val[i], sum += val[i];
int s = n + n + m + 1, t = n + n + m + 2;
for (int i = 1; i <= m; i++)
{
if (nu[i].size() - cnt[i] < 0)
{
cout << -1 << endl;
return;
}
mcmf.addEdge(s, i, nu[i].size() - cnt[i], 0);
for (auto j : nu[i])
{
mcmf.addEdge(i, m + j, 1, val[j]);
}
}
for (int i = 1; i <= n; i++)
{
if (i > 1)
{
mcmf.addEdge(i + m, i + n + m - 1, 1, 0);
mcmf.addEdge(i + n + m, i + n + m - 1, INF, 0);
}
mcmf.addEdge(i + n + m, t, 1, 0);
}
mcmf.run(s, t);
// cout << mcmf.maxflow << endl;
if (mcmf.maxflow == n / 2)
{
cout << sum - mcmf.mincost << endl;
}
else
{
cout << -1 << endl;
}
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
cin >> T;
while (T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3676kb
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: 0ms
memory: 3692kb
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:
5220751523 343766215 -1 -1 2201471991 -1 -1 2539318868 1093935906 -1 -1 -1 2231197660 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 5751395565 -1 -1 -1 -1 -1 883992368 1044562986 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1309966981 610708479 -1 -1 1373323696
result:
wrong answer 1st numbers differ - expected: '-1', found: '5220751523'