#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을 출력하고
Wiz
0번 선수가 남은 경기를 전부 승리했을 때 최소 승수 (total win)를 얻을 수 있는지를 미리 확인해서 불가능하면 -1을 출력하고
아니라면 for 문을 모두 수행하여서 만족하는 최소의 i 값을 출력하고 있습니다.
제출시에는 오답으로 나오는대 고민해보아도 해결이 안되네요..
모델링이 잘못된건지.. 아시는분은 답변 부탁드립니다.
10년 전