#include #include #include #include #include #include using namespace std; //dynamic programming! int max_city; int max_money; int max_road; int from[1500]; //from * 60 + to基準ソート int to[1500]; int costd[1500]; int timed[1500]; int table[50+1][300+1]; int mind; //縦city 横cost //デフォルト昇順 void combsort(int max){ bool flg; int h; h = max / 1.3; while(true){ flg = true; for(int i=0; i+h from[i+h] * 60 + to[i+h]){ //ソート基準変更位置 //swap swap(from[i], from[i+h]); swap(to[i], to[i+h]); swap(costd[i], costd[i+h]); swap(timed[i], timed[i+h]); flg = false; } } if(h == 1){ if(flg){ break; } }else{ h /= 1.3; } } } int main(){ cin >> max_city >> max_money >> max_road; for(int i=0; i> from[i]; } for(int i=0; i> to[i]; } for(int i=0; i> costd[i]; } for(int i=0; i> timed[i]; } //初期化 for(int iy=0; iy<=50; iy++){ for(int ix=0; ix<=300; ix++){ if(iy <= 1){ table[iy][ix] = 0; }else{ table[iy][ix] = 99999; } } } //道データソート! combsort(max_road); //dynamic programming(1→iyまで行く) //縦city 横cost for(int iy=1; iy<=max_city; iy++){ //city //ルート検索初期化 int i=-1; while(true){ do{ i++; }while(i < max_road && iy != from[i]); if(i >= max_road){ break; } for(int ix=1; ix<=max_money; ix++){ //コスト限界 if(iy != from[i]){ continue; } if(ix >= costd[i]){ //移動するよ!(時間短縮できるなら。) table[to[i]][ix] = min(table[to[i]][ix], table[iy][ix-costd[i]] + timed[i]); //cout << "table[" << to[i] << "][" << ix << "] = " << table[to[i]][ix] << endl; } } } //結局の所... mind = table[iy][0]; for(int i=1; i<=max_money; i++){ mind = min(mind,table[iy][i]); table[iy][i] = mind; } } //ぜぇー... if(table[max_city][max_money] == 99999){ cout << -1 << endl; }else{ cout << table[max_city][max_money] << endl; } return 0; }