#include <iostream> #include <vector> using namespace std; typedef long long ll; struct zel { int m; ll c; }; int main() { cin.tie(0)->sync_with_stdio(0); int n,k,m,o; cin>>n>>k>>m; o=n; vector <zel> pus(0); vector<vector<zel>> cuk(k,pus); for(int i=0;i<n;i++) { int a,b,c; //kolor,masa,cena cin>>a>>b>>c; zel ek; ek.m=b; ek.c=c; cuk[a-1].push_back(ek); } cout<<0<<"\n"; vector <ll> moz(m,0); for(int i=0;i<k;i++) { vector <zel> oni=cuk[i]; if(oni.size()==0) { for(int j=1;j<m;j++) cout<<"-1 \n"; return 0; } int rit=oni.size(); o=min(o,rit); vector <ll> doz(m,0); for(int j=0;j<oni.size();j++) { for(int t=0;t<m;t++) if(moz[t]!=0||(i==0&&t==0)) { if(doz[(t+oni[j].m)%m]==0) doz[(t+oni[j].m)%m]=oni[j].c+moz[t]; else doz[(t+oni[j].m)%m]=min(doz[(t+oni[j].m)%m],oni[j].c+moz[t]); } } moz=doz; /*for(int ik=0;ik<m;ik++) cout<<moz[ik]<<" "; cout<<"\n";*/ } //o--; vector <zel> bog(0),acz(0); vector <ll> bied=moz; //moz stary,bied do zwrotu vector <ll> star(m,0); for(int i=1;i<m;i++) { zel kim; kim.m=i; kim.c=moz[i]; if(kim.c>0) bog.push_back(kim),acz.push_back(kim); } int bulka=1; while(bulka>0) { star=bied; bulka=0; for(int i=0;i<bog.size();i++) for(int j=0;j<acz.size();j++) { if(bied[(bog[i].m+acz[j].m)%m]==0||bog[i].c+acz[j].c<bied[(bog[i].m+acz[j].m)%m]) {bied[(bog[i].m+acz[j].m)%m]=bog[i].c+acz[j].c; bulka=1;} } if(bulka==0) break; bog.clear(),acz.clear(); for(int i=1;i<m;i++) { zel kim; kim.m=i; kim.c=moz[i]; if(kim.c>0) bog.push_back(kim); kim.c=bied[i]; if(kim.c>0&&bied[i]!=star[i]) acz.push_back(kim); } o--; } for(int i=1;i<m;i++) { if(bied[i]==0) cout<<"-1 \n"; else cout<<bied[i]<<"\n"; } }
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 | #include <iostream> #include <vector> using namespace std; typedef long long ll; struct zel { int m; ll c; }; int main() { cin.tie(0)->sync_with_stdio(0); int n,k,m,o; cin>>n>>k>>m; o=n; vector <zel> pus(0); vector<vector<zel>> cuk(k,pus); for(int i=0;i<n;i++) { int a,b,c; //kolor,masa,cena cin>>a>>b>>c; zel ek; ek.m=b; ek.c=c; cuk[a-1].push_back(ek); } cout<<0<<"\n"; vector <ll> moz(m,0); for(int i=0;i<k;i++) { vector <zel> oni=cuk[i]; if(oni.size()==0) { for(int j=1;j<m;j++) cout<<"-1 \n"; return 0; } int rit=oni.size(); o=min(o,rit); vector <ll> doz(m,0); for(int j=0;j<oni.size();j++) { for(int t=0;t<m;t++) if(moz[t]!=0||(i==0&&t==0)) { if(doz[(t+oni[j].m)%m]==0) doz[(t+oni[j].m)%m]=oni[j].c+moz[t]; else doz[(t+oni[j].m)%m]=min(doz[(t+oni[j].m)%m],oni[j].c+moz[t]); } } moz=doz; /*for(int ik=0;ik<m;ik++) cout<<moz[ik]<<" "; cout<<"\n";*/ } //o--; vector <zel> bog(0),acz(0); vector <ll> bied=moz; //moz stary,bied do zwrotu vector <ll> star(m,0); for(int i=1;i<m;i++) { zel kim; kim.m=i; kim.c=moz[i]; if(kim.c>0) bog.push_back(kim),acz.push_back(kim); } int bulka=1; while(bulka>0) { star=bied; bulka=0; for(int i=0;i<bog.size();i++) for(int j=0;j<acz.size();j++) { if(bied[(bog[i].m+acz[j].m)%m]==0||bog[i].c+acz[j].c<bied[(bog[i].m+acz[j].m)%m]) {bied[(bog[i].m+acz[j].m)%m]=bog[i].c+acz[j].c; bulka=1;} } if(bulka==0) break; bog.clear(),acz.clear(); for(int i=1;i<m;i++) { zel kim; kim.m=i; kim.c=moz[i]; if(kim.c>0) bog.push_back(kim); kim.c=bied[i]; if(kim.c>0&&bied[i]!=star[i]) acz.push_back(kim); } o--; } for(int i=1;i<m;i++) { if(bied[i]==0) cout<<"-1 \n"; else cout<<bied[i]<<"\n"; } } |