QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#567397#9315. Rainbow Bracket Sequencerikka_lylyWA 1ms3884kbC++205.4kb2024-09-16 11:45:242024-09-16 11:45:24

Judging History

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

  • [2024-09-16 11:45:24]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3884kb
  • [2024-09-16 11:45:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define MAXN 210
#define MAXE 2 * 5010
#define INF 0x3f3f3f3f3f3f3f3f

struct Edge
{
    int to, next = 0; //用STL会MLE,所以用链式前向星
    ll cap, cost;
};

int head[MAXN] = {0};
Edge edge[MAXE];
int tot = 2; // 从2开始,配对表示正/反边,不用0/1

void add(int from, int to, ll cap, ll cost)
{
    edge[tot].to = to;
    edge[tot].cap = cap;
    edge[tot].cost = cost;
    edge[tot].next = head[from];
    head[from] = tot;
    tot++;
}

void addline(int from, int to, ll cap, ll cost)
{
    add(from, to, cap, cost);
    add(to, from, 0, -cost);
}

int n, m, s, t;
ll maxf[MAXN];
// int pre[MAXN];//存前驱边,每次bfs后跟着前驱边能从t走到s
int deep[MAXN];
int curhead[MAXN];

bool bfs_f()//不能dfs会被卡掉
{
    memset(deep, 0, sizeof(deep));
    deep[s] = 1;
    queue<int> q;
    q.push(s);
    while (!q.empty())
    {
        int cur = q.front();
        q.pop();
        for (int i = head[cur]; i; i = edge[i].next)
        {
            int to = edge[i].to;
            ll cap = edge[i].cap;
            if(!deep[to] && cap)
            {
                deep[to] = deep[cur] + 1;
                q.push(to);
                if(to == t)
                    return true;
            }
        }
    }
    return false;
}

ll dfs_f(int cur, ll mf)
{
    if(cur == t)
        return mf;
    ll sum = 0;
    for (int i = curhead[cur]; i; i = edge[i].next)
    {
        curhead[cur] = i;//当前弧优化
        int to = edge[i].to;
        ll cap = edge[i].cap;
        if (deep[to] == deep[cur] + 1 && cap)
        {
            ll f = dfs_f(to, min(mf, cap));
            edge[i].cap -= f;
            edge[i ^ 1].cap += f;
            sum += f;
            mf -= f;
            if(!mf)//流量优化
                break;
        }
    }
    if(!sum)//残枝优化
        deep[cur] = 0;
    return sum;
}

ll dinic()
{
    ll flow = 0;
    while (bfs_f())
    {
        memcpy(curhead, head, sizeof(curhead));
        flow += dfs_f(s, INF);
    }
    return flow;
}

bool vis_ge[MAXN]; // vis 0/1 的划分就是最小割的划分
void mincut(int cur)
{
    vis_ge[cur] = 1;
    for (int i = head[cur]; i; i = edge[i].next)
    {
        if (!vis_ge[edge[i].to] && edge[i].cap)
            mincut(edge[i].to);
    }
}

ll dis[MAXN], vis[MAXN];
int pre[MAXN];
bool spfa_f()
{
    memset(dis, 0x3f, sizeof(ll) * (n + 5));
    // memset(vis, 0, sizeof(ll) * (n + 5));
    memset(maxf, 0, sizeof(ll) * (n + 5));
    queue<int> q;
    q.push(s);
    dis[s] = 0, vis[s] = 1, maxf[s] = INF;
    while (!q.empty())
    {
        int cur = q.front();
        q.pop();
        vis[cur] = 0;
        for (int i = head[cur]; i; i = edge[i].next)
        {
            int to = edge[i].to;
            ll cap = edge[i].cap, cost = edge[i].cost;
            if(cap && dis[cur] + cost < dis[to])
            {
                dis[to] = dis[cur] + cost;
                maxf[to] = min(maxf[cur], cap);
                pre[to] = i;
                if(!vis[to])
                {
                    vis[to] = 1;
                    q.push(to);
                }
            }
        }
    }
    return maxf[t] > 0;
}

pair<ll, ll> SSP()
{
    ll flow = 0, cost = 0;
    while (spfa_f())
    {
        for (int cur = t; cur != s; )
        {
            int i = pre[cur];
            edge[i].cap -= maxf[t];
            edge[i ^ 1].cap += maxf[t];
            cur = edge[i ^ 1].to;
        }
        flow += maxf[t];
        cost += dis[t] * maxf[t];
    }
    return make_pair(flow, cost);
}

void init(int n)
{
    tot = 2;
    for (int i = 1; i <= n; i++)
    {
        head[i] = 0;
    }
    memset(vis, 0, sizeof(ll) * (n + 1));
}

void solve()
{
    int nn, mm;
    cin >> nn >> mm;
    init(1 + nn * 2 + mm + 1);
    s = 1, t = 1 + nn * 2 + mm + 1, n = t;
    const int BARCKET = 1, COLOR = 1 + nn * 2;
    vector<int> l(mm + 1);
    for (int i = 1; i <= mm; i++)
    {
        cin >> l[i];
    }
    vector<int> c(nn * 2 + 1);
    vector<int> colorlinenum(mm + 1);
    for (int i = 1; i <= nn * 2; i++)
    {
        cin >> c[i];
        colorlinenum[c[i]]++;
    }
    ll totval = 0;
    vector<int> v(nn * 2 + 1);
    for (int i = 1; i <= nn * 2; i++)
    {
        cin >> v[i];
        totval += v[i];
    }
    for (int i = 2; i <= nn * 2; i += 2)
    {
        addline(s, BARCKET + i, 1, 0);
        // cerr << s << "->" << BARCKET + i << endl;
    }
    for (int i = 1; i + 1 <= nn * 2; i++)
    {
        addline(BARCKET + i, BARCKET + i + 1, INF, 0);
        // cerr << BARCKET + i << "->" << BARCKET + i + 1 << endl;
    }
    for (int i = 1; i <= nn * 2; i++)
    {
        addline(BARCKET + i, COLOR + c[i], 1, v[i]);
        // cerr << BARCKET + i << "->" << COLOR + c[i] << endl;
    }
    for (int i = 1; i <= mm; i++)
    {
        addline(COLOR + i, t, colorlinenum[i] - l[i], 0);
        // cerr << COLOR + i << "->" << t << endl;
    }
    auto [a, b] = SSP();
    // cerr << "a=" << a << " b=" << b << endl;
    if(a != nn)
        cout << -1 << '\n';
    else
        cout << totval - b << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3876kb

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: 1ms
memory: 3884kb

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
1497027962
1351561190
2539318868
1013080942
4656646546
-1
-1
2231197660
2719131728
3983627641
4712174168
3121174891
1046749330
6115214757
3920988203
3914858167
3902088946
-1
2566553992
5268471900
5977120748
7505501534
-1
5054275471
1467678317
883992368
104456298...

result:

wrong answer 6th numbers differ - expected: '-1', found: '1497027962'