#include<bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); #define mod 998244353 #define int long long #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f inline int read() { char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();} int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();} if(nega==-1) return -ans; return ans; } #define N 7005 int n,k,m,col[N],w[N],s[N]; inline int add(int x,int y) {return x+y>=m?x+y-m:x+y;} vector<int> ids[N]; int f[N],g[N],dis[N],vis[N]; signed main() { cin>>n>>k>>m; for(int i=1;i<=n;i++) { col[i]=read(),w[i]=read()%m,s[i]=read(); ids[col[i]].push_back(i); } memset(f,0x3f,sizeof(f)); f[0]=0; for(int i=1;i<=k;i++) { memset(g,0x3f,sizeof(g)); for(int u:ids[i]) { for(int j=0;j<m;j++) g[add(j,w[u])]=min(g[add(j,w[u])],f[j]+s[u]); } memcpy(f,g,sizeof(f)); } // for(int i=0;i<m;i++) printf("%lld%c",f[i]," \n"[i==m-1]); memset(dis,0x3f,sizeof(dis)); dis[0]=0; for(int T=0;T<m;T++) { int mn=INF,mnid=-1; for(int j=0;j<m;j++) if(!vis[j]&&dis[j]<mn) mn=dis[j],mnid=j; if(mnid==-1) break; vis[mnid]=1; for(int j=0;j<m;j++) dis[add(mnid,j)]=min(dis[add(mnid,j)],dis[mnid]+f[j]); } for(int i=0;i<m;i++) { if(dis[i]==INF) printf("-1\n"); else printf("%lld\n",dis[i]); } 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 | #include<bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); #define mod 998244353 #define int long long #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f inline int read() { char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();} int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();} if(nega==-1) return -ans; return ans; } #define N 7005 int n,k,m,col[N],w[N],s[N]; inline int add(int x,int y) {return x+y>=m?x+y-m:x+y;} vector<int> ids[N]; int f[N],g[N],dis[N],vis[N]; signed main() { cin>>n>>k>>m; for(int i=1;i<=n;i++) { col[i]=read(),w[i]=read()%m,s[i]=read(); ids[col[i]].push_back(i); } memset(f,0x3f,sizeof(f)); f[0]=0; for(int i=1;i<=k;i++) { memset(g,0x3f,sizeof(g)); for(int u:ids[i]) { for(int j=0;j<m;j++) g[add(j,w[u])]=min(g[add(j,w[u])],f[j]+s[u]); } memcpy(f,g,sizeof(f)); } // for(int i=0;i<m;i++) printf("%lld%c",f[i]," \n"[i==m-1]); memset(dis,0x3f,sizeof(dis)); dis[0]=0; for(int T=0;T<m;T++) { int mn=INF,mnid=-1; for(int j=0;j<m;j++) if(!vis[j]&&dis[j]<mn) mn=dis[j],mnid=j; if(mnid==-1) break; vis[mnid]=1; for(int j=0;j<m;j++) dis[add(mnid,j)]=min(dis[add(mnid,j)],dis[mnid]+f[j]); } for(int i=0;i<m;i++) { if(dis[i]==INF) printf("-1\n"); else printf("%lld\n",dis[i]); } return 0; } |