QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#308416#7185. Poor Studentskevinshan#TL 152ms9136kbC++172.4kb2024-01-20 02:39:102024-01-20 02:39:10

Judging History

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

  • [2024-01-20 02:39:10]
  • 评测
  • 测评结果:TL
  • 用时:152ms
  • 内存:9136kb
  • [2024-01-20 02:39:10]
  • 提交

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],  n, m, k, s, t, cnt;
struct edge {
    int u, v, f, c, w;
} e[maxn*100];
 
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 && 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;
    s = 0, t = n + k + 1;
    for (int i=1, u, v, c, w; i<=n; i++)
    {
        add(0, i, 1, 0);
        u = i;
        for (v = n+1; v<=n+k; v++)
        {
            cin>>w;
            c = 1;
            add(u, v, c, w);
        }
    }
    for (int i=1; i<=k; i++)
    {
        int c; cin>>c;
        add(n+i, t, c, 0);
    }
    int ans = 0, need = n;
    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;
        }
    }
    assert(need == 0);
    cout<<ans;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 9056kb

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: 0
Accepted
time: 152ms
memory: 9136kb

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:

92039

result:

ok answer is '92039'

Test #4:

score: -100
Time Limit Exceeded

input:

5000 10
14 114 254 832 38 904 25 147 998 785
917 694 750 372 379 887 247 817 999 117
802 15 799 515 316 42 69 247 95 144
727 398 509 725 682 456 369 656 693 955
923 1 681 631 962 826 233 963 289 856
165 491 488 832 111 950 853 791 929 240
509 843 667 970 469 260 447 477 161 431
514 903 627 236 144 3...

output:


result: