QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#318051#7185. Poor StudentsMinhhoTL 2ms8900kbC++172.6kb2024-01-30 12:13:532024-01-30 12:13:54

Judging History

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

  • [2024-01-30 12:13:54]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:8900kb
  • [2024-01-30 12:13:53]
  • 提交

answer

#define taskname "MCMF"
#include <bits/stdc++.h>
#define int long long
#define ii pair<int,int>
#define ff first
#define ss second

using namespace std;

const int maxn = 100005;
vector<int> adj[maxn];
int d[maxn], cur[maxn], cost, n, m, k, s, t, cnt;
struct edge {
    int u, v, f, c, w;
} e[maxn*100];
 mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

inline void add(int u, int v, int c, int w)
{
    // cout<<"ADD: "<<u<<" "<<v<<" "<<c<<" "<<w<<"\n";
    adj[u].emplace_back(cnt);
    e[cnt++] = {u, v, 0, c, w};
    adj[v].emplace_back(cnt);
    e[cnt++] = {v, u, 0, 0, -w};
}

inline bool BFS()
{
    fill(d+s, d+t+1, 1e18);
    d[s] = 0;
    priority_queue<ii, vector<ii>, greater<ii>> pq;
    pq.emplace(0, s);
    while (pq.size())
    {
        auto [du, u] = pq.top(); pq.pop();
        for (int id: adj[u])
        {
            auto [_, v, f, c, w] = e[id];
            if (f < c && cost >= w && d[v] > d[u] + w)
            {
                d[v] = d[u] + w;
                pq.emplace(d[v], v);
            }
        }
    }
    return (d[t] != 1e18);
}


int DFS(int u, int flow)
{
    if (u == t) return flow;
    for (int &i=cur[u]; i<adj[u].size();)
    {
        int id = adj[u][i++];
        auto [_, v, f, c, w] = e[id];
        if (f < c && d[v] == d[u] + w)
        {
            int x = DFS(v, min(flow, c - f));
            if (x)
            {
                e[id].f += x;
                e[id^1].f -= x;
                return x;
            }
        }
    }
    return 0;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    if (fopen("input.in", "r")) {
        freopen("input.in", "r", stdin);
        freopen("output.out", "w", stdout);
    }
    cin>>n>>k;
    assert(n <= 1e4);
    s = 0, t = n + k + 1;
    for (int i=1, u, v, c, w; i<=n; i++)
    {
        add(0, i, 1, 0);
        u = i;
        vector<ii> ve;
        for (v=n+1; v<=n+k; v++)
        {
            cin>>w;
            ve.emplace_back(w, v);
        }
        shuffle(ve.begin(), ve.end(), rng);
        for (auto [w, v]: ve) add(u, v, 1, w);
    }
    for (int i=1; i<=k; i++)
    {
        int c; cin>>c;
        if (c) add(n+i, t, c, 0);
    }
    int ans = 0, need = n;
    for (cost = 1; cost <= 1e9; cost <<= 1)
    {
        while (BFS() && need)
        {
            fill(cur+s, cur+t+1, 0);
            int x;
            while (x = DFS(s, need))
            {
                ans += x * d[t];
                need -= x;
                if (!need) break;
            }
        }
    }
    cout<<ans;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6 2
1 2
1 3
1 4
1 5
1 6
1 7
3 4

output:

12

result:

ok answer is '12'

Test #2:

score: 0
Accepted
time: 2ms
memory: 7840kb

input:

3 3
1 2 3
2 4 6
6 5 4
1 1 1

output:

8

result:

ok answer is '8'

Test #3:

score: -100
Time Limit Exceeded

input:

1000 10
734 303 991 681 755 155 300 483 702 442
237 256 299 675 671 757 112 853 759 233
979 340 288 377 718 199 935 666 576 842
537 363 592 349 494 961 864 727 84 813
340 78 600 492 118 421 478 925 552 617
517 589 716 7 928 638 258 297 706 787
266 746 913 978 436 859 701 951 137 44
815 336 471 720 2...

output:


result: