-
Notifications
You must be signed in to change notification settings - Fork 0
/
DinicMaxFlow
227 lines (200 loc) · 6.02 KB
/
DinicMaxFlow
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Problem : E. Soldier and Traveling
// Contest : Codeforces - Codeforces Round #304 (Div. 2)
// URL : https://codeforces.com/problemset/problem/546/E
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
// Dinics max flow algorithms O(E.V^2)
//look at my code
//my code is amazing
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll long long
// #define int long long
#define mod 1000000007 //998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define f(i,n) for(ll i=0;i<n;i++)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define nl "\n"
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl
#define trace2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define cout1(a) cout<<a<<nl
#define cout2(a,b) cout<<a<<" "<<b<<nl
#define cout3(a,b,c) cout<<a<<" "<<b<<" "<<c<<nl
#define cout4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<nl
#define vcout(v,i) cout<<v[i].fi<<" "<<v[i].se<<nl
#define x first
#define y second
#define vc vector
#define pb push_back
#define ar array
#define all(a) (a).begin(), (a).end()
#define ms(v,n,x) fill(v,v+n,x);
#define init(c,a) memset(c,a,sizeof(c))
#define pll pair<ll,ll>
#define mll map<ll,ll>
#define sll set<ll>
#define vll vector<ll>
#define vpll vector<pll>
#define maxi(a,b,c) max(a,max(b,c))
#define maxii(a,b,c,d) max(max(a,b),max(c,d))
#define mini(a,b,c) min(a,min(b,c))
#define md(a,b) ((a%mod)*(b%mod)+mod)%mod
#define inf 9e18
#define cases ll T;cin>>T;while(T--)
#define BLOCK 500
//const double PI = 3.141592653589793238460;
template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
// void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
namespace Dinic {
typedef int flow_t;
const int MAXN = 2056;
const int MAXE = 2024;
const flow_t INF = 2e5;
const flow_t EPS = 1;
flow_t LIM = EPS;
struct Edge {
int u, v;
flow_t cap, flow;
};
Edge edges[MAXE];
vector<int> adj[MAXN];
int lev[MAXN];
int ptr[MAXN];
int que[MAXN];
int N = MAXN, E = 0;
int source, sink;
bool bfs() {
int fp = 0, bp = 0;
fill(lev, lev + N, -1);
lev[source] = 0; que[bp++] = source;
while (fp < bp) {
int u = que[fp++];
for (const int id : adj[u]) {
if (edges[id].cap - edges[id].flow < LIM) continue;
if (lev[edges[id].v] != -1) continue;
lev[edges[id].v] = lev[u] + 1;
que[bp++] = edges[id].v;
}
}
return lev[sink] != -1;
}
flow_t dfs(int u, flow_t pushed) {
if (pushed == 0) return 0;
if (u == sink) return pushed;
for (int& cid = ptr[u]; cid < (int) adj[u].size(); cid++) {
int id = adj[u][cid];
int v = edges[id].v;
if (lev[u] + 1 != lev[v]) continue;
if (edges[id].cap - edges[id].flow < LIM) continue;
flow_t flow = dfs(v, min(pushed, edges[id].cap - edges[id].flow));
if (flow == 0) continue;
edges[id].flow += flow;
edges[id ^ 1].flow -= flow;
return flow;
}
return 0;
}
// scale = max capacity edge if scaling else default
flow_t max_flow(int s, int t, int scale = EPS) {
source = s, sink = t;
flow_t flow = 0, pushed;
for (LIM = scale; LIM >= EPS; LIM >>= 1) {
while (bfs()) {
fill(ptr, ptr + N, 0);
while ((pushed = dfs(source, INF)) >= EPS)
flow += pushed;
}
}
return flow;
}
inline void reset(int n = MAXN) {
N = n; E = 0;
for (int i = 0; i < N; ++i) adj[i].clear();
}
inline void set_edge(int u, int v, flow_t cap = 1) /*directed edge*/ {
adj[u].push_back(E);
edges[E++] = {u, v, cap, 0};
adj[v].push_back(E);
edges[E++] = {v, u, 0, 0};
}
} // namespace Dinic
using namespace Dinic;
ll ans[201][201];
int32_t main()
{
fast
cout << fixed << setprecision(12);
ll n,m;
cin>>n>>m;
ll a[n+1],b[n+1];
ll s1=0,s2=0;
fore(i,1,n){
cin>>a[i];
s1+=a[i];
set_edge(0,i,a[i]);
}
fore(i,1,n){
cin>>b[i];
s2+=b[i];
set_edge(i,i+n,INF);
set_edge(i+n,2*n+1,b[i]);
}
f(i,m){
ll u,v;
cin>>u>>v;
set_edge(u,v+n,INF);
set_edge(v,u+n,INF);
}
ll res=max_flow(0,2*n+1); //source sink
if(res==s1 and res==s2){
cout<<"YES\n";
f(i,E){
auto t=edges[i];
if(t.u>=1 and t.u<=n and t.v>n and t.v<=2*n)
ans[t.u][t.v-n]=t.flow;
}
fore(i,1,n){
fore(j,1,n){
cout<<ans[i][j]<<" ";
}
cout<<nl;
}
}
else
cout<<"NO";
return 0;
}