#include<iostream>
#include"ilcplex/ilocplex.h";

using namespace std;

int main(int, char**) { 

	//ucitavamo podatke
	ifstream data("mclp.txt");
	IloEnv env;
	try {
		IloModel model(env);
		IloInt n, m, p, S, check;
		data >> n;
		data >> m;
		data >> p;

		IloArray<IloNumArray>c(env, n);
		for (int i = 0; i < n; i++)
		{
			c[i] = IloNumArray(env, m);
			for (int j = 0; j < m; j++)
			{
				data >> c[i][j];
			}
		}

		IloNumArray a(env, n);
		for (int i = 0; i < n; i++)
		{
			data >> a[i];
		}
		data >> S;
		data >> check;
		//kreiramo promenljive
		IloNumVarArray y(env, m, 0, 1, ILOBOOL);
		IloNumVarArray x(env, n, 0, 1, ILOBOOL);
		//definisemo fukciju cilja
		IloExpr fc(env);
		for (int i = 0; i < n; i++)
		{
			fc += a[i] * x[i];
		}
		model.add(IloMaximize(env, fc));
		//definisemo uslove:korisnik je pokriven samo ukoliko je dovoljno blizu barem jednog otvornog centra j. 
		// Ukoliko su svi centri na rastojanju manjem od S od korisnika i zatvoreni, onda on ne moze biti pokriven.
		for (int i = 0; i < n; i++)
		{
			IloExpr cond1(env);
			for (int j = 0; j < m; j++)
			{
				if (c[i][j] <= S) {
					cond1 += y[j];
				}

			}
			model.add(cond1 >= x[i]);
			cond1.end();
		}
		//definisemo uslov: otvara se tacno p centara
		IloExpr cond2(env);
		for (int j = 0; j < m; j++)
		{
			cond2 += y[j];
		}
		model.add(cond2 == p);

		//resavamo i stampamo na standardni izlaz i u datoteku
		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("mclp_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(y[j]) == 1) {
				izlaz << j + 1 << " ";

			}
		}
		izlaz << endl;

	}
	catch (IloException& ex) {
		cerr << "Desila se greska: " << ex;
	}
	

}
	







