Pagini recente » Cod sursa (job #1185417) | Cod sursa (job #1086641) | Cod sursa (job #2318812) | Cod sursa (job #747071) | Cod sursa (job #3130406)
#include <fstream>
#include <deque>
#include <vector>
using namespace std;
ifstream fin("aprindere.in");
ofstream fout("aprindere.out");
struct swit {
int room;
int time;
int nr;
deque<int> bulbs;
} ;
vector<swit> switches;
vector<swit>::iterator maxi;
int n, m, timecounter = 0;
int state[1000], done = false;
int scoreof (vector<swit>::iterator s){
int result = 0;
for (deque<int>::iterator dit = s->bulbs.begin(); dit != s->bulbs.end(); ++dit){
if (! state[*dit])
++result;
else --result;
}
return result;
}
int main(){
fin >> n >> m;
switches.resize(m);
for (int i = 0; i < n; i++)
fin >> state[i];
for (int i = 0; i < m; i++){
fin >> switches[i].room >> switches[i].time >> switches[i].nr;
for (int j = 0; j < switches[i].nr; j++){
int aux;
fin >> aux;
switches[i].bulbs.push_back(aux);
}
}
while (! done){
//we find the optimal switch to trigger
maxi = switches.begin();
for (vector<swit>::iterator it = switches.begin(); it != switches.end(); ++it){
if (it != switches.begin()){
int scoreit = scoreof(it), scoremaxi = scoreof(maxi);
if (scoreit * maxi->time > scoremaxi * it->time)
maxi = it;
}
}
//we actually trigger the said switch
for (int i = 0; i < maxi->nr; i++)
state[maxi->bulbs[i]] ^= 1;
//we count the time
timecounter += maxi->time;
//we remove the switch such that we dont trigger it again
switches.erase(maxi);
//we check to see if we're done
done = true;
for (int i = 0; i < n; i++){
if (state[i] == 0)
done = false;
}
}
fout << timecounter;
return 0;
}