Submission #2337652


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 T> class bellman_ford {
public:
	struct edge{
		int from,to;
		T cost;
	};
	vector<int> d;
	vector<edge> es;
	int edge_id;
	int V,E;
	bellman_ford(int node_size){
		edge_id = 0, V = node_size;
		d.resize(node_size,numeric_limits<T>::max());
	}
	void add_edge(int from,int to,T cost){
		es.push_back((edge){from,to,cost});
	}
	//sからの最短路長およびsからたどり着ける負の閉路の検出(trueなら負の閉路が存在する)
	bool solve(int s){
		E = (int)es.size();
		int cnt = 0;
		d[s] = 0;
		while(cnt < V){
			bool update = false;
			rep(i,E){
				edge e = es[i];
				if(d[e.from] != numeric_limits<T>::max() && d[e.to] > d[e.from] + e.cost){
					d[e.to] = d[e.from] + e.cost;
					update = true;
				}
			}
			if(!update) break;
			cnt++;
		}
		return (cnt == V);
	}
	//すべての負の閉路の検出(trueなら負の閉路が存在する)
	bool find_negative_loop(){
		E = (int)es.size();
	    fill(d.begin(),d.end(),0);
		int cnt = 0;
		while(cnt < V){
 		   bool update = false;
 		   rep(i,E){
 			   edge e = es[i];
 			   if(d[e.to] > d[e.from] + e.cost){
 				   d[e.to] = d[e.from] + e.cost;
 				   update = true;
 			   }
 		   }
 		   if(!update) break;
 		   cnt++;
 	   }
 	   return (cnt == V);
	}
	//sからtへの最短路上に存在する負の閉路の検出(trueなら負の閉路が存在する)
	bool find_negative_loop(int s,int t){
		E = (int)es.size();
		d[s] = 0;
		rep(i,2*V){
			rep(j,E){
				edge e = es[j];
				if(d[e.from] != numeric_limits<T>::max() && d[e.to] > d[e.from] + e.cost){
					d[e.to] = d[e.from] + e.cost;
					if(i >= V-1 && e.to == t){
						return true;
					}
				}
			}
		}
		return false;
	}
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    bellman_ford<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.find_negative_loop()){
        cout << "no\n";
    }else{
        cout << "yes\n";
    }
    return 0;
}

Submission Info

Submission Time
Task H - Asteroids2
User kopricky
Language C++14 (GCC 5.4.1)
Score 200
Code Size 3825 Byte
Status AC
Exec Time 629 ms
Memory 6512 KB

Compile Error

./Main.cpp: In instantiation of ‘bellman_ford<T>::bellman_ford(int) [with T = long long int]’:
./Main.cpp:120:30:   required from here
./Main.cpp:52:3: warning: overflow in implicit constant conversion [-Woverflow]
   d.resize(node_size,numeric_limits<T>::max());
   ^

Judge Result

Set Name All
Score / Max Score 200 / 200
Status
AC × 41
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 KB
00-sample-01 AC 1 ms 256 KB
10-small_yes-00 AC 1 ms 256 KB
10-small_yes-01 AC 1 ms 256 KB
10-small_yes-02 AC 1 ms 256 KB
10-small_yes-03 AC 1 ms 256 KB
10-small_yes-04 AC 1 ms 256 KB
10-small_yes-05 AC 1 ms 256 KB
10-small_yes-06 AC 4 ms 888 KB
10-small_yes-07 AC 5 ms 888 KB
10-small_yes-08 AC 4 ms 888 KB
20-small_disturb-00 AC 1 ms 256 KB
20-small_disturb-01 AC 1 ms 256 KB
20-small_disturb-02 AC 1 ms 256 KB
20-small_disturb-03 AC 1 ms 256 KB
20-small_disturb-04 AC 1 ms 256 KB
20-small_disturb-05 AC 1 ms 256 KB
20-small_disturb-06 AC 12 ms 888 KB
20-small_disturb-07 AC 15 ms 888 KB
20-small_disturb-08 AC 13 ms 888 KB
30-large_yes-00 AC 35 ms 5232 KB
30-large_yes-01 AC 35 ms 4848 KB
30-large_yes-02 AC 35 ms 6128 KB
30-large_yes-03 AC 35 ms 5360 KB
30-large_yes-04 AC 35 ms 5360 KB
40-large_disturb-00 AC 585 ms 6512 KB
40-large_disturb-01 AC 588 ms 5104 KB
40-large_disturb-02 AC 602 ms 5360 KB
40-large_disturb-03 AC 629 ms 6512 KB
40-large_disturb-04 AC 582 ms 6256 KB
40-large_disturb-05 AC 592 ms 5232 KB
40-large_disturb-06 AC 602 ms 6384 KB
40-large_disturb-07 AC 624 ms 6384 KB
40-large_disturb-08 AC 584 ms 5872 KB
40-large_disturb-09 AC 590 ms 6512 KB
40-large_disturb-10 AC 600 ms 6512 KB
40-large_disturb-11 AC 621 ms 5488 KB
40-large_disturb-12 AC 582 ms 5232 KB
40-large_disturb-13 AC 590 ms 5744 KB
40-large_disturb-14 AC 600 ms 4848 KB
40-large_disturb-15 AC 625 ms 6128 KB