MATCHFIX 문제 어디서 오답나오는지 모르겠네요..

  • Wiz
    Wiz
    #define _CRT_SECURE_NO_WARNINGS
    #include<iostream>
    #include<vector>
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    
    const int INF = 987654321;
    const int MAX_V=115;
    
    int V, N, M;
    int capacity[MAX_V][MAX_V], flow[MAX_V][MAX_V];
    
    vector<int> wins;
    vector<pair<int, int> > match;
    
    int networkFlow(int source, int sink) {
        memset(flow, 0, sizeof(flow));
        int totalFlow = 0;
        while (1) {
            vector<int> parent(MAX_V, -1);
            queue<int> q;
            parent[source] = source;
            q.push(source);
            while (!q.empty() && parent[sink] == -1) {
                int here = q.front(); q.pop();
                for (int there = 0; there < V; ++there)
                if (capacity[here][there] - flow[here][there] > 0 && parent[there] == -1) {
                    q.push(there);
                    parent[there] = here;
                }
            }
            if (parent[sink] == -1) break;
            int amount = INF;
            for (int p = sink; p != source; p = parent[p])
                amount = min(capacity[parent[p]][p] - flow[parent[p]][p], amount);
            for (int p = sink; p != source; p = parent[p]) {
                flow[parent[p]][p] += amount;
                flow[p][parent[p]] -= amount;
            }
            totalFlow += amount;
        }
    
        return totalFlow;
    }
    
    bool solve(int total){
        for (int i = 1; i < N; ++i){
            if (wins[i] >= total)
                return false;
        }
        V = N + M + 2;
        memset(capacity, 0, sizeof(capacity));
        for (int i = 0; i < M; ++i){
            capacity[0][2 + i] = 1;
            capacity[2 + i][2 + M + match[i].first] = 1;
            capacity[2 + i][2 + M + match[i].second] = 1;
        }
    
        for (int i = 0; i < N; ++i){
            int max_wins = (i == 0 ? total : total - 1);
            capacity[2 + M + i][1] = max_wins - wins[i];
        }
        return networkFlow(0, 1) == M;
    }
    
    int main() {
        int c;
        cin >> c;
        while (c--){
            cin >> N >> M;
            wins = vector<int>(N);
            match = vector<pair<int,int> >(M);
    
            for (int i = 0; i < N; ++i)
                cin >> wins[i];
    
            int ret = 0;
            for (int i = 0; i < M; ++i){
                cin >> match[i].first >> match[i].second;
                if (match[i].first == 0 || match[i].second == 0)
                    ++ret;
            }
    
            bool success = false;
            for (int i = 0; i <= 1500; ++i){
                if (wins[0] + ret < i)
                    break;
                if (solve(i)){
                    cout << i << endl;
                    success = true;
                    break;
                }
            }
    
            if (!success)
                cout << -1 << endl;
        }
        return 0;
    }
    

    0번 선수가 남은 경기를 전부 승리했을 때 최소 승수 (total win)를 얻을 수 있는지를 미리 확인해서 불가능하면 -1을 출력하고

    아니라면 for 문을 모두 수행하여서 만족하는 최소의 i 값을 출력하고 있습니다.

    제출시에는 오답으로 나오는대 고민해보아도 해결이 안되네요..
    모델링이 잘못된건지.. 아시는분은 답변 부탁드립니다.


    8년 전
2개의 댓글이 있습니다.
  • Jinsanger
    Jinsanger

    저도 같이 고생하는 사람입니다 ㅜㅜ 그리디하게 답이 나올 수 없는 오답 케이스가 혹시 있으면 알고싶습니다.


    8년 전 link
  • Wiz
    Wiz

    그리디한 오답 케이스는 저도 공부 중이라 모르겠네요..
    문제는 해결하였습니다. 그리디한 방법이 왜안되는지 저도 오답케이스를 알고 싶네요


    8년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.