Submission #8559172
Source Code Expand
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
using namespace std;
typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
const int MAX_N = 100005;
template<typename _Tp> class Goldberg95 {
public:
static_assert(std::is_integral<_Tp>::value, "Integral required.");
private:
struct base_edge {
int to;
_Tp cost;
base_edge(const int _to, const _Tp _cost) : to(_to), cost(_cost){}
};
struct edge : base_edge {
_Tp roundup;
edge(const int _to, const _Tp _cost) : base_edge(_to, _cost), roundup(0){}
};
const int V;
int E, comp_count, head;
_Tp min_cost;
vector<vector<edge> > G;
vector<vector<base_edge> > graph;
vector<vector<int> > dag, que;
vector<int> ord, low, st, cmp, repr, dist, prv, ng, layer_cnt;
int compute_roundup(){
int cnt = 0;
_Tp tmp = -min_cost;
while(tmp >= 2) tmp >>= 1, ++cnt;
if(cnt > 0){
for(int i = 0; i < V; ++i){
for(edge& e : G[i]){
if(e.cost < 0){
_Tp tmp = -e.cost;
for(int j = 0; j < cnt; ++j){
e.roundup <<= 1, e.roundup |= (tmp & 1);
tmp >>= 1;
}
e.cost = -tmp;
}else{
for(int j = 0; j < cnt; ++j){
e.roundup <<= 1, e.roundup |= (e.cost & 1);
e.cost = ((e.cost + 1) >> 1);
}
}
}
}
}
return cnt;
}
void scc_dfs(const int u, int& tm){
ord[u] = low[u] = tm++, st[++head] = u;
for(const edge& e : G[u]){
if(e.cost + potential[u] - potential[e.to] > 0) continue;
const int v = e.to;
if(ord[v] < 0){
scc_dfs(v, tm);
low[u] = min(low[u], low[v]);
}else if(ord[v] < V){
low[u] = min(low[u], ord[v]);
}
}
if(ord[u] == low[u]){
while(true){
const int v = st[head--];
ord[v] = V, cmp[v] = comp_count;
if(v == u) break;
}
repr[comp_count++] = u;
}
}
void strongly_connected_component(){
comp_count = 0, head = -1;
fill(ord.begin(), ord.end(), -1);
int tm = 0;
for(int i = 0; i < V; ++i){
if(ord[i] < 0) scc_dfs(i, tm);
}
}
bool construct_dag(bool& finish){
for(int i = 0; i < comp_count; ++i) dag[i].clear();
bool minus_edge = false;
for(int i = 0; i < V; ++i){
for(const edge& e : G[i]){
const _Tp edge_cost = e.cost + potential[i] - potential[e.to];
if(cmp[i] == cmp[e.to]){
if(edge_cost < 0) return false;
}else{
if(edge_cost == 0) dag[cmp[i]].emplace_back(cmp[e.to]);
else if(edge_cost < 0){
dag[cmp[i]].emplace_back(cmp[e.to] + V);
minus_edge = true;
}
}
}
}
if(!minus_edge) finish = true;
return true;
}
bool contract_graph(bool& finish){
strongly_connected_component();
return construct_dag(finish);
}
void graph_layering(){
fill(dist.begin(), dist.begin() + comp_count, 0);
fill(ng.begin(), ng.begin() + comp_count, 0);
for(int i = comp_count - 1; i >= 0; --i){
for(const int v : dag[i]){
const int w = ((v < V) ? v : v - V);
ng[w] |= (v >= V);
if(dist[w] > dist[i] + (v < V) - 1){
dist[w] = dist[i] + (v < V) - 1, prv[w] = i;
}
}
}
}
void cancel_layer(const int layer_num){
for(int i = 0; i < V; ++i){
potential[i] -= (-dist[cmp[i]] >= layer_num);
}
}
bool cancel_path(const int max_len){
for(int i = 0; i < max_len; ++i){
for(int j = 0; j < (int)que[i].size(); ++j){
const int u = que[i][j];
if(dist[u] < i) continue;
for(const edge& e : G[u]){
const _Tp edge_cost = e.cost + potential[u] - potential[e.to];
const _Tp edge_cost_plus = (edge_cost >= 0) ? edge_cost : 0;
if(edge_cost < 0){
if(prv[cmp[e.to]] < 0 && dist[e.to] >= dist[u]) return false;
}else if(dist[e.to] > dist[u] + edge_cost_plus){
dist[e.to] = dist[u] + edge_cost_plus;
que[dist[e.to]].push_back(e.to);
}
}
}
}
for(int i = 0; i < V; ++i) potential[i] += dist[i];
return true;
}
bool cancel_negative_edges(){
fill(layer_cnt.begin(), layer_cnt.begin() + comp_count, 0);
int max_len = 0, max_vertex = 0, max_cnt = 0, max_layer = 0;
for(int i = 0; i < comp_count; ++i){
if(dist[i] < 0 && ng[i] > 0 && max_cnt < ++layer_cnt[-dist[i]]){
++max_cnt, max_layer = -dist[i];
}
if(max_len < -dist[i]){
max_len = -dist[i], max_vertex = i;
}
}
if((long long)(4 * E + 3 * max_len + V) * max_cnt >= (long long)V * max_len){
cancel_layer(max_layer);
return true;
}else{
fill(dist.begin(), dist.end(), max_len);
int nx = -1;
for(int i = max_len; i > 0; --i){
dist[repr[max_vertex]] = max_len - i;
que[max_len - i] = {repr[max_vertex]};
nx = prv[max_vertex];
prv[max_vertex] = -1;
max_vertex = nx;
}
cancel_path(max_len);
return true;
}
}
bool solve(){
bool finish = false;
while(!finish){
if(!contract_graph(finish)) return false;
if(finish) return true;
graph_layering();
if(!cancel_negative_edges()) return false;
}
return true;
}
void change_cost(){
_Tp min_potential = *min_element(potential.begin(), potential.end());
for(int i = 0; i < V; ++i){
potential[i] = 2 * (potential[i] - min_potential);
for(edge& e : G[i]){
e.cost = e.cost * 2 - (e.roundup & 1), e.roundup >>= 1;
}
}
}
public:
vector<_Tp> potential;
Goldberg95(const int node_size)
: V(node_size), E(0), min_cost(0), G(V), graph(V), dag(V), que(V),
ord(V), low(V), st(V), cmp(V), repr(V), dist(V), prv(V, 0), ng(V),
layer_cnt(V), potential(V, 0){}
void add_edge(const int u, const int v, const _Tp cost){
G[u].emplace_back(v, cost), ++E;
min_cost = min(min_cost, cost);
}
bool compute_potential(){
if(min_cost < 0){
int num = compute_roundup();
for(; num >= 0; --num){
if(!solve()) return false;
if(num > 0) change_cost();
}
}
return true;
}
// DEBUG
bool check(){
for(int i = 0; i < V; ++i){
for(const edge& e : G[i]){
if(e.cost + potential[i] - potential[e.to] < 0) return false;
}
}
return true;
}
};
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n,m;
cin >> n >> m;
Goldberg95<ll> bf(2*n+1);
rep(i,n){
int p;
cin >> p;
bf.add_edge(2*n,i,p);
bf.add_edge(i,2*n,0);
}
rep(i,n){
int q;
cin >> q;
bf.add_edge(2*n,n+i,0);
bf.add_edge(n+i,2*n,q);
}
rep(i,m){
int x,y,a,b;
cin >> x >> y >> a >> b;
--x,--y;
bf.add_edge(n+y,x,b);
bf.add_edge(x,n+y,-a);
}
if(bf.compute_potential()){
cout << "yes\n";
}else{
cout << "no\n";
}
return 0;
}
Submission Info
| Submission Time |
|
| Task |
H - Asteroids2 |
| User |
kopricky |
| Language |
C++14 (GCC 5.4.1) |
| Score |
200 |
| Code Size |
9700 Byte |
| Status |
AC |
| Exec Time |
177 ms |
| Memory |
7808 KiB |
Judge Result
| Set Name |
All |
| Score / Max Score |
200 / 200 |
| Status |
|
| Set Name |
Test Cases |
| All |
00-sample-00, 00-sample-01, 10-small_yes-00, 10-small_yes-01, 10-small_yes-02, 10-small_yes-03, 10-small_yes-04, 10-small_yes-05, 10-small_yes-06, 10-small_yes-07, 10-small_yes-08, 20-small_disturb-00, 20-small_disturb-01, 20-small_disturb-02, 20-small_disturb-03, 20-small_disturb-04, 20-small_disturb-05, 20-small_disturb-06, 20-small_disturb-07, 20-small_disturb-08, 30-large_yes-00, 30-large_yes-01, 30-large_yes-02, 30-large_yes-03, 30-large_yes-04, 40-large_disturb-00, 40-large_disturb-01, 40-large_disturb-02, 40-large_disturb-03, 40-large_disturb-04, 40-large_disturb-05, 40-large_disturb-06, 40-large_disturb-07, 40-large_disturb-08, 40-large_disturb-09, 40-large_disturb-10, 40-large_disturb-11, 40-large_disturb-12, 40-large_disturb-13, 40-large_disturb-14, 40-large_disturb-15 |
| Case Name |
Status |
Exec Time |
Memory |
| 00-sample-00 |
AC |
1 ms |
256 KiB |
| 00-sample-01 |
AC |
1 ms |
256 KiB |
| 10-small_yes-00 |
AC |
1 ms |
256 KiB |
| 10-small_yes-01 |
AC |
1 ms |
256 KiB |
| 10-small_yes-02 |
AC |
1 ms |
256 KiB |
| 10-small_yes-03 |
AC |
2 ms |
384 KiB |
| 10-small_yes-04 |
AC |
2 ms |
384 KiB |
| 10-small_yes-05 |
AC |
2 ms |
384 KiB |
| 10-small_yes-06 |
AC |
17 ms |
1280 KiB |
| 10-small_yes-07 |
AC |
16 ms |
1024 KiB |
| 10-small_yes-08 |
AC |
16 ms |
1280 KiB |
| 20-small_disturb-00 |
AC |
1 ms |
256 KiB |
| 20-small_disturb-01 |
AC |
1 ms |
256 KiB |
| 20-small_disturb-02 |
AC |
1 ms |
256 KiB |
| 20-small_disturb-03 |
AC |
1 ms |
384 KiB |
| 20-small_disturb-04 |
AC |
1 ms |
384 KiB |
| 20-small_disturb-05 |
AC |
1 ms |
256 KiB |
| 20-small_disturb-06 |
AC |
6 ms |
1024 KiB |
| 20-small_disturb-07 |
AC |
6 ms |
1024 KiB |
| 20-small_disturb-08 |
AC |
6 ms |
1024 KiB |
| 30-large_yes-00 |
AC |
172 ms |
7552 KiB |
| 30-large_yes-01 |
AC |
168 ms |
7552 KiB |
| 30-large_yes-02 |
AC |
177 ms |
7552 KiB |
| 30-large_yes-03 |
AC |
173 ms |
7552 KiB |
| 30-large_yes-04 |
AC |
174 ms |
7552 KiB |
| 40-large_disturb-00 |
AC |
51 ms |
7680 KiB |
| 40-large_disturb-01 |
AC |
50 ms |
7680 KiB |
| 40-large_disturb-02 |
AC |
50 ms |
7680 KiB |
| 40-large_disturb-03 |
AC |
50 ms |
7680 KiB |
| 40-large_disturb-04 |
AC |
50 ms |
7680 KiB |
| 40-large_disturb-05 |
AC |
50 ms |
7680 KiB |
| 40-large_disturb-06 |
AC |
51 ms |
7808 KiB |
| 40-large_disturb-07 |
AC |
50 ms |
7680 KiB |
| 40-large_disturb-08 |
AC |
50 ms |
7680 KiB |
| 40-large_disturb-09 |
AC |
50 ms |
7808 KiB |
| 40-large_disturb-10 |
AC |
50 ms |
7680 KiB |
| 40-large_disturb-11 |
AC |
50 ms |
7680 KiB |
| 40-large_disturb-12 |
AC |
50 ms |
7680 KiB |
| 40-large_disturb-13 |
AC |
51 ms |
7680 KiB |
| 40-large_disturb-14 |
AC |
51 ms |
7680 KiB |
| 40-large_disturb-15 |
AC |
50 ms |
7680 KiB |