#include<iostream>
#include"ilcplex/ilocplex.h";

using namespace std;


int main(int, char**) {

	//ucitavamo podatke
	ifstream data("aclp.txt");
	IloEnv env;
	try{
	IloModel model(env);
	IloInt  m,  R,M, check;
	
	data >> m;
	
	IloArray<IloNumArray>c(env, m);
	for (int i = 0; i < m; i++)
	{
		c[i] = IloNumArray(env, m);
		for (int j = 0; j < m; j++)
		{
			data >> c[i][j];
		}
	}

	IloNumArray v(env, m);
	for (int i = 0; i < m; i++)
	{
		data >> v[i];
	}
	data >> R;
	data >> M;
	data >> check;

	//definisemo promenljive
	IloNumVarArray x(env, m, 0, 1, ILOBOOL);
	//definisemo funkciju cilja
	IloExpr fc(env);
	for (int i = 0; i < m; i++)
	{
		fc += v[i] * x[i];
	}
	model.add(IloMaximize(env, fc));
	//definisemo uslove: ako je na lokaciji i otvoren centar, na lokacijama j koje su na udaljenosti manjoj od R ne sme da se otvori centar
	for (int i = 0; i < m; i++)
	{
		IloExpr cond1(env);
		cond1 += M * x[i];
		for (int j = 0; j < m; j++)
		{
			if (c[i][j] <= R & i!=j)
				cond1 += x[j];
		}
		model.add(cond1 <= M);
		cond1.end();
		
	}

	//resavamo i stampamo 
	IloCplex cplex(model);
	cplex.setOut(env.getNullStream());

	if (!cplex.solve())
		cout << "Nije nadjeno resenje.";

	cout << "Vrednost f-je cilja: " << cplex.getObjValue() << endl;
	cout << "Vreme: " << cplex.getTime() << endl;
	cout << "Status: " << cplex.getStatus();

	ofstream izlaz("aclp_izlaz.txt");

	izlaz << "Vrednost f-je cilja: " << cplex.getObjValue() << endl;
	izlaz << "Status: " << cplex.getStatus() << endl;
	izlaz << "Vreme: " << cplex.getTime() << endl;

	izlaz << "Centre treba otvoriti na lokacijama:(numeracija centara krece od 1) " << endl;
	for (int j = 0; j < m; j++)
	{
		if (cplex.getValue(x[j]) == 1) {
			izlaz << j + 1 << " ";

		}
	}
	izlaz << endl;

	}
	catch (IloException& ex) {
		cerr << "Desila se greska: " << ex;
	}

}





