#include<bits/stdc++.h> using namespace std; constexpr int N = 7007; constexpr long long INF = 1e18; vector<pair<long long,long long>> zelki[N];//[kolor zelka] first masa second cena long long n,k,m; long long a,b,c; long long krawedzie[N], pom[N]; long long odl[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> m; for(int i=0;i<n;i++) { cin >> a >> b >> c; zelki[a].push_back({b,c}); } for(int i=1;i<m;i++) krawedzie[i]=INF; for(int kol=1;kol<=k;kol++) { if(zelki[kol].size()==0) { //cout << "error"; cout << "0\n"; for(int r=1;r<m;r++) cout << "-1\n"; return 0; } for(int i=0;i<m;i++) pom[i]=INF; for(int r=0;r<m;r++) { if(krawedzie[r]==INF) continue; for(auto x:zelki[kol]) { pom[(r+x.first)%m] = min(pom[(r+x.first)%m], krawedzie[r]+x.second); } } for(int r=0;r<m;r++) krawedzie[r]=pom[r]; // for(int r=0;r<m;r++) // cout << krawedzie[r] << "\n"; } // for(int r=0;r<m;r++) // cout << krawedzie[r] << "\n"; for(int r=1;r<m;r++) odl[r]=INF; odl[0]=0; priority_queue<pair<long long,long long>> dikstra; dikstra.push({0,0}); // wierzcholek/koszt vector<pair<long long,long long>> realK; //prawdziwe krawedzie kierunek/koszt for(int r=1;r<m;r++) { if(krawedzie[r]!=INF) { realK.push_back({r,krawedzie[r]}); } } pair<long long,long long> act; while(!dikstra.empty()) { act=dikstra.top(); //cout << act.first << " | " << act.second << "\n"; dikstra.pop(); if(act.second>odl[act.first]) continue; //cout << act.first << " | " << act.second << "\n"; for(auto x:realK) { if(act.second+x.second < odl[(act.first+x.first)%m]) { odl[(act.first+x.first)%m] = act.second+x.second; dikstra.push({(act.first+x.first)%m,act.second+x.second}); } } } for(int r=0;r<m;r++) { // cout << r << " "; if(odl[r]==INF) cout << "-1\n"; else cout << odl[r] << "\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 | #include<bits/stdc++.h> using namespace std; constexpr int N = 7007; constexpr long long INF = 1e18; vector<pair<long long,long long>> zelki[N];//[kolor zelka] first masa second cena long long n,k,m; long long a,b,c; long long krawedzie[N], pom[N]; long long odl[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> m; for(int i=0;i<n;i++) { cin >> a >> b >> c; zelki[a].push_back({b,c}); } for(int i=1;i<m;i++) krawedzie[i]=INF; for(int kol=1;kol<=k;kol++) { if(zelki[kol].size()==0) { //cout << "error"; cout << "0\n"; for(int r=1;r<m;r++) cout << "-1\n"; return 0; } for(int i=0;i<m;i++) pom[i]=INF; for(int r=0;r<m;r++) { if(krawedzie[r]==INF) continue; for(auto x:zelki[kol]) { pom[(r+x.first)%m] = min(pom[(r+x.first)%m], krawedzie[r]+x.second); } } for(int r=0;r<m;r++) krawedzie[r]=pom[r]; // for(int r=0;r<m;r++) // cout << krawedzie[r] << "\n"; } // for(int r=0;r<m;r++) // cout << krawedzie[r] << "\n"; for(int r=1;r<m;r++) odl[r]=INF; odl[0]=0; priority_queue<pair<long long,long long>> dikstra; dikstra.push({0,0}); // wierzcholek/koszt vector<pair<long long,long long>> realK; //prawdziwe krawedzie kierunek/koszt for(int r=1;r<m;r++) { if(krawedzie[r]!=INF) { realK.push_back({r,krawedzie[r]}); } } pair<long long,long long> act; while(!dikstra.empty()) { act=dikstra.top(); //cout << act.first << " | " << act.second << "\n"; dikstra.pop(); if(act.second>odl[act.first]) continue; //cout << act.first << " | " << act.second << "\n"; for(auto x:realK) { if(act.second+x.second < odl[(act.first+x.first)%m]) { odl[(act.first+x.first)%m] = act.second+x.second; dikstra.push({(act.first+x.first)%m,act.second+x.second}); } } } for(int r=0;r<m;r++) { // cout << r << " "; if(odl[r]==INF) cout << "-1\n"; else cout << odl[r] << "\n"; } return 0; } |