/* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; LL m,k; class Possib { public: LL bestPrice[7010]; Possib() { FOR(i,0,7000) bestPrice[i]=-1; } void add(LL p_mass, LL p_price) { if (bestPrice[p_mass]==-1) { bestPrice[p_mass] = p_price; } else { if (p_price < bestPrice[p_mass]) bestPrice[p_mass] = p_price; } } void initFrom(const Possib& p2) { FOR(i,0,m-1) bestPrice[i]=p2.bestPrice[i]; } void show() { FOR(i,0,m-1) { cout << i << ": "; if (bestPrice[i]==-1) cout << "-\n"; else cout << bestPrice[i] << "\n"; } } }; class Zelek { public: LL mass; LL price; Zelek(LL p_mass, LL p_price) { mass=p_mass; price=p_price; } }; vector<Zelek> kolor[7010]; void readData() { LL n; cin >> n; cin >> k; cin >> m; LL kk, mm, cc; FOR(i,1,n) { cin >> kk; cin >> mm; cin >> cc; kolor[kk].push_back(Zelek(mm%m,cc)); } } Possib poss1, poss2, poss3; void step1() { poss1.add(0,0); LL price, newMass, newPrice; FOR(i,1,k) { // dodajemy 1 zelek koloru "i" Possib p2; // empty set of possibilities FOREACH(z,kolor[i]) { // probujemy dolozyc do istniejacych mozliwosci zelka "z" FOR(mass,0,m-1) //if (poss1.bestPrice[mass]!=-1) { price = poss1.bestPrice[mass]; if (price!=-1) { newMass = (mass + z->mass); while (newMass>=m) newMass-=m; newPrice = price + z->price; p2.add(newMass, newPrice); } } } poss1.initFrom(p2); } } void step2() { LL price, mm, pp; poss2.add(0,0); // wziecie 0 zelkow dowolnego typu FOR(mass,0,m-1) { price = poss1.bestPrice[mass]; if (price==-1) continue; mm = 0; pp = 0; FOR(j,1,m+1) { mm += mass; while (mm>=m) mm-=m; pp += price; if (poss2.bestPrice[mm]==-1) { poss2.bestPrice[mm]=pp; } else if (pp < poss2.bestPrice[mm]) { poss2.bestPrice[mm]=pp; } } } } void step3() { poss3.add(0,0); // 0 zelkow LL price, price2, newMass, newPrice; FOR(mass,1,m-1) if (poss2.bestPrice[mass]!=-1) { price = poss2.bestPrice[mass]; // dodajemy 1 zelek o masie "mass" i cenie "price" Possib p2; // empty set of possibilities p2.initFrom(poss3); // probujemy dolozyc do istniejacych mozliwosci zelka "z" FOR(mass2,0,m-1) //if (poss3.bestPrice[mass2]!=-1) { price2 = poss3.bestPrice[mass2]; if (price2!=-1) { newMass = (mass + mass2); while (newMass>=m) newMass-=m; newPrice = price + price2; p2.add(newMass, newPrice); } } poss3.initFrom(p2); //cout << "Po dodaniu zelka m=" << mass << " p=" << price << "\n"; //poss3.show(); } } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); readData(); FOR(i,1,k) if (kolor[i].size()==0) { //cout << "Brak koloru!\n"; cout << "0\n"; FOR(i,1,m-1) cout << "-1\n"; return 0; } step1(); step2(); step3(); FOR(i,0,m-1) cout << poss3.bestPrice[i] << '\n'; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; LL m,k; class Possib { public: LL bestPrice[7010]; Possib() { FOR(i,0,7000) bestPrice[i]=-1; } void add(LL p_mass, LL p_price) { if (bestPrice[p_mass]==-1) { bestPrice[p_mass] = p_price; } else { if (p_price < bestPrice[p_mass]) bestPrice[p_mass] = p_price; } } void initFrom(const Possib& p2) { FOR(i,0,m-1) bestPrice[i]=p2.bestPrice[i]; } void show() { FOR(i,0,m-1) { cout << i << ": "; if (bestPrice[i]==-1) cout << "-\n"; else cout << bestPrice[i] << "\n"; } } }; class Zelek { public: LL mass; LL price; Zelek(LL p_mass, LL p_price) { mass=p_mass; price=p_price; } }; vector<Zelek> kolor[7010]; void readData() { LL n; cin >> n; cin >> k; cin >> m; LL kk, mm, cc; FOR(i,1,n) { cin >> kk; cin >> mm; cin >> cc; kolor[kk].push_back(Zelek(mm%m,cc)); } } Possib poss1, poss2, poss3; void step1() { poss1.add(0,0); LL price, newMass, newPrice; FOR(i,1,k) { // dodajemy 1 zelek koloru "i" Possib p2; // empty set of possibilities FOREACH(z,kolor[i]) { // probujemy dolozyc do istniejacych mozliwosci zelka "z" FOR(mass,0,m-1) //if (poss1.bestPrice[mass]!=-1) { price = poss1.bestPrice[mass]; if (price!=-1) { newMass = (mass + z->mass); while (newMass>=m) newMass-=m; newPrice = price + z->price; p2.add(newMass, newPrice); } } } poss1.initFrom(p2); } } void step2() { LL price, mm, pp; poss2.add(0,0); // wziecie 0 zelkow dowolnego typu FOR(mass,0,m-1) { price = poss1.bestPrice[mass]; if (price==-1) continue; mm = 0; pp = 0; FOR(j,1,m+1) { mm += mass; while (mm>=m) mm-=m; pp += price; if (poss2.bestPrice[mm]==-1) { poss2.bestPrice[mm]=pp; } else if (pp < poss2.bestPrice[mm]) { poss2.bestPrice[mm]=pp; } } } } void step3() { poss3.add(0,0); // 0 zelkow LL price, price2, newMass, newPrice; FOR(mass,1,m-1) if (poss2.bestPrice[mass]!=-1) { price = poss2.bestPrice[mass]; // dodajemy 1 zelek o masie "mass" i cenie "price" Possib p2; // empty set of possibilities p2.initFrom(poss3); // probujemy dolozyc do istniejacych mozliwosci zelka "z" FOR(mass2,0,m-1) //if (poss3.bestPrice[mass2]!=-1) { price2 = poss3.bestPrice[mass2]; if (price2!=-1) { newMass = (mass + mass2); while (newMass>=m) newMass-=m; newPrice = price + price2; p2.add(newMass, newPrice); } } poss3.initFrom(p2); //cout << "Po dodaniu zelka m=" << mass << " p=" << price << "\n"; //poss3.show(); } } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); readData(); FOR(i,1,k) if (kolor[i].size()==0) { //cout << "Brak koloru!\n"; cout << "0\n"; FOR(i,1,m-1) cout << "-1\n"; return 0; } step1(); step2(); step3(); FOR(i,0,m-1) cout << poss3.bestPrice[i] << '\n'; return 0; } |