#include<bits/stdc++.h> using namespace std; #define f first #define s second #define PB push_back typedef long long ll; typedef long double ld; #define REP(x,y) for(int x=0;x<(y);x++) #define ROF(x,y) for(int x=(y);x>0;x--) #define FOR(x,z,y) for(int x=(z);x<=(y);x++) #define INT(x) int x;scanf("%d",&x) #define LL(x) ll x;scanf("%lld",&x) #define CZ(x) char x;scanf(" %c",&x) typedef pair<int,int> pii; typedef pair<int,pii> piii; vector<pii> candies[7005]; bool continue_flag = false; ll res[7005]; int n,k,m,x; vector<int> lookup[2]; ll cnt_A[7005],tmp_A[7005]; void check_edge_case(int k, int m){ REP(i,k){ if(candies[i].size()==0){ puts("0"); REP(j,m-1) puts("-1"); exit(0); } } } int main(){ scanf("%d %d %d",&n,&k,&m); REP(i,n){ INT(a);INT(b);INT(c); candies[a-1].PB({b,c}); } check_edge_case(k,m); size_t s = sizeof(ll)*m; ll *cnt = cnt_A; ll *tmp = tmp_A; memset(res,-1,s); memset(cnt,-1,s); res[0] = 0; cnt[0] = 0; memset(tmp,-1,s); int y=0, lookup_no=0,lookup_tmp=1; lookup[lookup_no].PB(0); while(1){ REP(i,k){ lookup[lookup_tmp].clear(); for(pii p:candies[i]){ for(int j:lookup[lookup_no]){ x = (j+p.f)%m; if(tmp[x] == -1){ tmp[x] = cnt[j]+p.s; lookup[lookup_tmp].PB(x); } else tmp[x] = min(tmp[x],cnt[j]+p.s); } } for(int j:lookup[lookup_no]){ cnt[j] = -1; } swap(lookup_no,lookup_tmp); swap(cnt,tmp); } continue_flag = false; REP(i,m){ if(cnt[i] == -1)continue; if(res[i] == -1 || cnt[i] < res[i]){ res[i] = cnt[i]; continue_flag = true; lookup[lookup_no].PB(i); } } if(!continue_flag)break; } REP(i,m) printf("%lld\n",res[i]); }
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 | #include<bits/stdc++.h> using namespace std; #define f first #define s second #define PB push_back typedef long long ll; typedef long double ld; #define REP(x,y) for(int x=0;x<(y);x++) #define ROF(x,y) for(int x=(y);x>0;x--) #define FOR(x,z,y) for(int x=(z);x<=(y);x++) #define INT(x) int x;scanf("%d",&x) #define LL(x) ll x;scanf("%lld",&x) #define CZ(x) char x;scanf(" %c",&x) typedef pair<int,int> pii; typedef pair<int,pii> piii; vector<pii> candies[7005]; bool continue_flag = false; ll res[7005]; int n,k,m,x; vector<int> lookup[2]; ll cnt_A[7005],tmp_A[7005]; void check_edge_case(int k, int m){ REP(i,k){ if(candies[i].size()==0){ puts("0"); REP(j,m-1) puts("-1"); exit(0); } } } int main(){ scanf("%d %d %d",&n,&k,&m); REP(i,n){ INT(a);INT(b);INT(c); candies[a-1].PB({b,c}); } check_edge_case(k,m); size_t s = sizeof(ll)*m; ll *cnt = cnt_A; ll *tmp = tmp_A; memset(res,-1,s); memset(cnt,-1,s); res[0] = 0; cnt[0] = 0; memset(tmp,-1,s); int y=0, lookup_no=0,lookup_tmp=1; lookup[lookup_no].PB(0); while(1){ REP(i,k){ lookup[lookup_tmp].clear(); for(pii p:candies[i]){ for(int j:lookup[lookup_no]){ x = (j+p.f)%m; if(tmp[x] == -1){ tmp[x] = cnt[j]+p.s; lookup[lookup_tmp].PB(x); } else tmp[x] = min(tmp[x],cnt[j]+p.s); } } for(int j:lookup[lookup_no]){ cnt[j] = -1; } swap(lookup_no,lookup_tmp); swap(cnt,tmp); } continue_flag = false; REP(i,m){ if(cnt[i] == -1)continue; if(res[i] == -1 || cnt[i] < res[i]){ res[i] = cnt[i]; continue_flag = true; lookup[lookup_no].PB(i); } } if(!continue_flag)break; } REP(i,m) printf("%lld\n",res[i]); } |