반응형

문제 출처 :


https://www.acmicpc.net/problem/2367



알고리즘 분석 :


문제 해결에 필요한 사항

1. 네트워크 플로우

2. 최대 유량


기본적인 최대 유량 문제이다.


1. source와 사람을 이어주고 간선의 cost는 k로 둔다.


2. sink와 음식을 이어주는데 간선의 cost는 각 음식의 종류마다 가져올 수 있는 양(d를 통한 값)으로 둔다.


3. 사람과 음식간에 간선을 생성해주는데 가중치는 INF로, 간선을 두는 방법은 각 사람이 요리 할 줄 아는 음식번호를 연결해주면 된다.


4. 최대 유량을 구하면 정답을 도출할 수 있다.





소스 코드 : 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <memory.h>
#include <algorithm>
 
#define fastio() ios::sync_with_stdio(0),cin.tie(0);
 
using namespace std;
 
const int INF = 987654321;
const int FOOD = 250;
 
int c[502][502];
int f[502][502];
 
vector<int> adj[502];
 
int S = 0, T = 500;
int main()
{
    int n, k, d;
    cin >> n >> k >> d;
 
    for (int i = 1; i <= n; i++)
    {
        c[S][i] = k;
 
        adj[S].push_back(i);
        adj[i].push_back(S);
    }
 
    for (int i = 1; i <= d; i++)
    {
        int val;
        cin >> val;
 
        c[i + FOOD][T] = val;
        adj[i + FOOD].push_back(T);
        adj[T].push_back(i + FOOD);
    }
 
    for (int i = 1; i <= n; i++)
    {
        int cnt;
        cin >> cnt;
 
        for (int j = 0; j < cnt; j++)
        {
            int pos;
            cin >> pos;
 
            c[i][pos + FOOD] = 1;
 
            adj[i].push_back(pos + FOOD);
            adj[pos + FOOD].push_back(i);
        }
    }
 
 
    int totalFlow = 0;
 
    while (1)
    {
        vector<int> prev(502-1);
 
        queue<int> q;
        q.push(S);
 
        while (!q.empty() && prev[T] == -1)
        {
            int here = q.front();
            q.pop();
 
            for (int i = 0; i < adj[here].size(); i++)
            {
                int next = adj[here][i];
                if (prev[next] != -1)
                    continue;
 
                if (c[here][next] - f[here][next] > 0)
                {
                    q.push(next);
                    prev[next] = here;
                }
            }
        }
        if (prev[T] == -1)
            break;
 
        int flow = INF;
 
        for (int i = T; i != S; i = prev[i])
            flow = min(flow, c[prev[i]][i] - f[prev[i]][i]);
 
        for (int i = T; i != S; i = prev[i])
        {
        //    cout << "i :: " << i << endl;
            f[prev[i]][i] += flow;
            f[i][prev[i]] -= flow;
        }
        totalFlow += flow;
 
    }
 
    cout << totalFlow;
 
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형