#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define int long long #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int INF = 1e18+7, mxN = 7e3+7; vector<pair<int,int>> c[mxN]; int dp[mxN][mxN]; //k, reszta int dist[mxN]; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,k,m; cin >> n >> k >> m; rep(i,1,n){ int x,y,z; cin >> x >> y >> z; c[x].pb({y,z}); } rep(i,0,k) rep(j,0,m) dp[i][j] = INF; dp[0][0] = 0; rep(i,1,k){ for(auto [r, C] : c[i]){ rep(j,0,m-1) dp[i][(j+r)%m] = min(dp[i][(j+r)%m], dp[i-1][j] + C); } } rep(i,1,m-1) dist[i] = INF; priority_queue<pair<int,int>> q; q.push({0, 0}); while(sz(q)){ auto [d, v] = q.top(); q.pop(); if(dist[v] != -d) continue; rep(r,1,m-1) if(dist[(v+r)%m] > dist[v] + dp[k][r]){ dist[(v+r)%m] = dist[v] + dp[k][r]; q.push({-dist[(v+r)%m], (v+r)%m}); } } rep(i,0,m-1){ if(dist[i] == INF) cout << "-1\n"; else cout << dist[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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define int long long #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int INF = 1e18+7, mxN = 7e3+7; vector<pair<int,int>> c[mxN]; int dp[mxN][mxN]; //k, reszta int dist[mxN]; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,k,m; cin >> n >> k >> m; rep(i,1,n){ int x,y,z; cin >> x >> y >> z; c[x].pb({y,z}); } rep(i,0,k) rep(j,0,m) dp[i][j] = INF; dp[0][0] = 0; rep(i,1,k){ for(auto [r, C] : c[i]){ rep(j,0,m-1) dp[i][(j+r)%m] = min(dp[i][(j+r)%m], dp[i-1][j] + C); } } rep(i,1,m-1) dist[i] = INF; priority_queue<pair<int,int>> q; q.push({0, 0}); while(sz(q)){ auto [d, v] = q.top(); q.pop(); if(dist[v] != -d) continue; rep(r,1,m-1) if(dist[(v+r)%m] > dist[v] + dp[k][r]){ dist[(v+r)%m] = dist[v] + dp[k][r]; q.push({-dist[(v+r)%m], (v+r)%m}); } } rep(i,0,m-1){ if(dist[i] == INF) cout << "-1\n"; else cout << dist[i] << "\n"; } } |