QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#504957#9102. Zayin and ElementsCheek_support#TL 2ms9936kbC++203.9kb2024-08-04 17:52:122024-08-04 17:52:12

Judging History

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

  • [2024-08-04 17:52:12]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:9936kb
  • [2024-08-04 17:52:12]
  • 提交

answer

#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#define DEBUG_VAR(x) { cerr << "* "#x" = " << x << endl; }
#define DEBUG_ARR(arr, l, r) { \ 
    cerr << "* "#arr"[" << l << ", " << r << "]:"; \
    for(auto it = l, itr = r; it <= itr; it++) \
        cerr << arr[it] << ", "; \
    cerr << endl; \
}
#define DEBUG_FMT(...) { cerr << "* "; fprintf(stderr, __VA_ARGS__); }
#define DEBUG_HERE { DEBUG_FMT("Passing [%s] in LINE %d\n", __FUNCTION__, __LINE__); } 

using namespace std;

const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;

int n, m;
int a[N], b[N], c[N];
vector<int> e[N];

int one[N], idx;
int ver[N], nxt[N], w[N], cap[N];
void addEdge(int a, int b, int c, int d)
{
    ver[idx] = b; cap[idx] = c; w[idx] = d; nxt[idx] = one[a]; one[a] = idx++;
    ver[idx] = a; cap[idx] = 0; w[idx] = -d; nxt[idx] = one[b]; one[b] = idx++;
}

int S, T;
int flow, cost;

int pre[N];
bool vis[N];

double min_cost;
int min_pre[N];
int cnt_t = 0;

void dfs(int x, double cur_cost)
{
    cnt_t++;
    if(cnt_t > 22) return;

    // DEBUG_VAR(x);
    if(cur_cost >= min_cost) return;
    if(x == T) {
        min_cost = cur_cost;
        copy_n(pre + 1, T, min_pre + 1);
        return;
    }

    for(int i = one[x]; ~i; i = nxt[i]) {
        // DEBUG_VAR(i);
        int y = ver[i];
        double z = w[i];
        if(vis[i] || vis[i ^ 1] || !cap[i]) continue;
        if(w[i] == 1) {
            if(cap[i] % 2 == 0) {
                z = 1.9999999;
            }  else {
                z = 0;
            }
        } else if(w[i] == -1) {
            if(cap[i] % 2 == 0) {
                z = 0;
            } else {
                z = -2.000001;
            }
        }
        pre[y] = i;
        vis[i] = true;
        dfs(y, cur_cost + z);
        vis[i] = false;
        pre[y] = -1;
    }
}

bool EK() 
{
    memset(pre, -1, sizeof(*pre)*(T + 1));

    min_cost = INF;
    cnt_t = 0;

    dfs(S, 0);
    DEBUG_VAR(min_cost);

    if(min_cost > INF - 1) return false;
    flow += 1;
    cost += round(min_cost);

    assert(flow <= n);
    // DEBUG_VAR(cost);

    int cur = T;
    while(cur != S) {
        int i = min_pre[cur];
        cap[i]--;
        cap[i ^ 1]++;
        cur = ver[i ^ 1];
    }
    return true;
}

void solve()
{
    cin >> n >> m;

    for(int i = 1; i <= m; i++) {
        e[i].clear();
    }

    int sum = 0;
    for(int i = 1; i <= m; i++) {
        cin >> a[i] >> b[i] >> c[i];
        sum += b[i] + c[i];
        int k; cin >> k;
        while(k--) {
            int x; cin >> x;
            e[i].push_back(x);
        }
    }

    S = n + m + 1, T = n + m + 2;
    fill_n(one, T + 1, -1); idx = 0;
    
    for(int i = 1; i <= m; i++) {
        addEdge(S, i, a[i], 0);
        for(int x : e[i]) {
            addEdge(i, m + x, 1, 0);
        }
    }
    for(int i = 1; i <= n; i++) {
        addEdge(m + i, T, 1, 0);
    }

    flow = 0, cost = 0;
    while(EK());
    if(flow == n) {
        sum -= cost/2;
        cout << sum << endl;
        return;
    }
    for(int i = 1; i <= m; i++) {
        addEdge(S, i, b[i] * 2, 1);
    }

    while(EK());
    if(flow == n) {
        sum -= cost/2;
        cout << sum << endl;
        return;
    }
    for(int i = 1; i <= m; i++) {
        addEdge(S, i, c[i], 2);
    }
    while(EK());
    if(flow == n) {
        sum -= cost/2;
        cout << sum << endl;
        return;
    }

    // DEBUG_HERE;
    if(flow < n) {
        cout << "-1" << endl;
        return;
    }

    sum -= cost/2;
    cout << sum << endl;
}

int main()
{
    // freopen("1.in", "r", stdin);
    ios::sync_with_stdio(false); cin.tie(nullptr);

    int T; cin >> T;
    while(T--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9936kb

input:

2
5
2
2 3 1 2 3 1
1 1 1 3 4 5 2
5
2
2 3 1 1 3
1 0 1 2 1 2

output:

5
-1

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

5
9
10
0 0 10 2 3 8
0 0 10 2 4 6
0 8 2 2 2 4
0 0 10 3 1 3 8
0 4 6 2 2 3
0 8 2 3 2 6 7
0 9 1 2 1 7
0 2 8 3 1 4 9
0 7 3 1 5
0 0 10 3 5 6 9
3
10
0 9 1 1 2
0 5 5 1 1
0 1 9 1 1
0 7 3 1 1
0 7 3 0
0 0 10 0
0 6 4 1 3
0 9 1 0
0 7 3 0
0 9 1 1 2
90
20
0 6 4 12 1 4 5 22 32 64 66 67 73 87 88 89
0 1 9 12 3 8 21 3...

output:

95
97
-1
-1

result: