include iostream include vector include queue using namespace std stru

  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
114
115
116
117
118
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct node
{
long long to;
long long cap;
long long f;
long long inv;
node(long long T = 0, long long C = 0, long long F = 0, long long I = 0): to(T), cap(C), f(F), inv(I){};
};
vector<vector<node> > fl;
vector<long long> d;
vector<long long> stop;
long long n;
long long t;
long long inf = 1000000000;
bool bfs()
{
queue<long long> q;
d.assign(2 * n + 2, -1);
q.push(0);
d[0] = 0;
while (!q.empty() && d[t] == -1)
{
long long v = q.front();
q.pop();
for (int i = 0; i < fl[v].size(); ++i){
if (d[fl[v][i].to] == -1 && fl[v][i].f < fl[v][i].cap){
q.push(fl[v][i].to);
d[fl[v][i].to] = d[v] + 1;
}
}
}
return (d[t] != -1);
}
long long dfs(long long v, long long flow)
{
if (flow == 0)
return flow;
if (v == t)
return flow;
for (; stop[v] < fl[v].size(); stop[v]++){
if (d[fl[v][stop[v]].to] != d[v] + 1)
continue;
if (fl[v][stop[v]].cap - fl[v][stop[v]].f <= 0)
continue;
long long pushed = dfs(fl[v][stop[v]].to, min(flow, fl[v][stop[v]].cap - fl[v][stop[v]].f));
if (pushed)
{
fl[v][stop[v]].f += pushed;
fl[fl[v][stop[v]].to][fl[v][stop[v]].inv].f -= pushed;
return pushed;
}
}
return 0;
}
int main()
{
cin >> n;
vector<long long> banks(n);
vector<long long> plants(n);
long long sum = 0;
for (int i = 0; i < n; ++i){
cin >> plants[i];
sum += plants[i];
}
for (int i = 0; i < n; i++)
cin >> banks[i];
fl.resize(2 * n + 2);
for (int i = 0; i < fl.size(); i++)
fl[i].reserve(2 * n + 2);
for (int i = 1; i <= n; i++){
fl[0].push_back(node(i, plants[i-1], 0, fl[i].size()));
fl[i].push_back(node(0, 0, 0, fl[0].size() - 1));
}
t = fl.size() - 1;
for (int i = n + 1; i < t; ++i){
fl[i].push_back(node(t, banks[i - n - 1], 0, fl[t].size()));
fl[t].push_back(node(i, 0, 0, fl[i].size() - 1));
}
for (int i = 1; i <= n; i++){
for (long long j = n + 1; j < t; ++j){
fl[i].push_back(node(j, 100, 0, fl[j].size()));
fl[j].push_back(node(i, 0, 0, fl[i].size() - 1));
}
}
long long flow = 0;
while (true){
if (!bfs()) break;
stop.assign(2 * n + 2, 0);
if (long long p = dfs(0, inf))
flow += p;
}
// cout << flow;
if (sum != flow){
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < fl[i].size(); j++){
cout << fl[i][j].f << " ";
}
cout << endl;
}
return 0;
}