Codeforces Round #304 (Div. 2) E. Soldier and Traveling
フローわかってる人にとってはとても簡単な問題。僕はよくわかってなかったのでVirtual participationした時には解けませんでした。
解法
kmjpさんの解法を参考にしました。kmjp.hatenablog.jp
// Dinic法:最小カット,最大フローで使う // 使い方: Dinic* dinic = new Dinic(V)で初期化(Vは頂点数) // dinic->add_edgeまたはdinic->add_edge_bothで点をつなげてdinic->max_flowで最大フローを求める #define NG -1 #define SZ(a) ((int)((a).size())) class Dinic { public: Dinic(int input_maxv) : maxv(input_maxv) { G.resize(input_maxv); level.resize(input_maxv); iter.resize(input_maxv); } void add_edge_both(int from, int to, int cap) { const int rev_from = SZ(G[from]); const int rev_to = SZ(G[to]); G[from].push_back(edge(to,cap,rev_to)); G[to].push_back(edge(from,cap,rev_from)); } void add_edge(int from, int to, int cap) { const int rev_from = SZ(G[from]); const int rev_to = SZ(G[to]); G[from].push_back(edge(to,cap,rev_to)); G[to].push_back(edge(from,0,rev_from)); } int max_flow(int s, int t) { int flow = 0; for(;;) { bfs(s); if(level[t]<0) break; fill(iter.begin(),iter.end(),0); int f; while( (f=dfs(s,t,DINIC_INF))>0) { flow += f; } } return flow; } vector <bool> get_nodes_in_group(int s) { vector <bool> ret(maxv); queue<int> que; que.push(s); while(!que.empty()) { int v = que.front(); que.pop(); ret[v]=true; for(int i=0;i<SZ(G[v]);i++) { edge &e = G[v][i]; if(e.cap>0 && !ret[e.to]) { que.push(e.to); } } } return ret; } void disp() { for (int v = 0; v < maxv; v++) { printf("%d:",v); for(int i=0;i<SZ(G[v]);i++) { if(G[v][i].init_cap>0) { printf("->%d(%d),",G[v][i].to,G[v][i].init_cap); } } printf("\n"); } } static const int DINIC_INF = INT_MAX; struct edge { edge(int input_to, int input_cap, int input_rev) : to(input_to), cap(input_cap), rev(input_rev), init_cap(input_cap) {} int to; int cap; int rev; int init_cap; }; int maxv; vector < vector <edge> > G; vector < int > level; vector < int > iter; private: void bfs(int s) { fill(level.begin(),level.end(),NG); queue<int> que; level[s]=0; que.push(s); while(!que.empty()) { int v = que.front(); que.pop(); for(int i=0;i<SZ(G[v]);i++) { edge &e = G[v][i]; if(e.cap>0 && level[e.to]<0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if(v==t) return f; for (int &i=iter[v];i<SZ(G[v]);i++) { edge& e = G[v][i]; if(e.cap>0 && level[v]<level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if(d>0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } }; const int INF = 1e9; int n, m; int a[111], b[111]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; ll target = 0, bb = 0; for (int i = 0; i < n; i++) { cin >> a[i]; target += a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; bb += b[i]; } int s = 2*n, t = s+1; Dinic* dinic = new Dinic(2*n+5); for (int i = 0; i < n; i++) { dinic->add_edge(s, i, a[i]); } for (int i = 0; i < n; i++) { dinic->add_edge(i+n, t, b[i]); } for (int i = 0; i < n; i++) { dinic->add_edge(i, i+n, INF); } for (int i = 0; i < m; i++) { int p, q; cin >> p >> q; p--; q--; dinic->add_edge(p, q+n, INF); dinic->add_edge(q, p+n, INF); } ll f = (dinic->max_flow(s, t)); if (f < target || target != bb) { cout << "NO" << endl; } else { cout << "YES" << endl; vector<vll> mat(n, vll(n)); for (int i = 0; i < n; i++) { for (auto e : dinic->G[i]) { ll tmp = INF-e.cap; mat[i][e.to-n] = tmp; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << mat[i][j] << " "; } cout << endl; } } delete dinic; return 0; }