Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const long long INF = 1e18;
- using pii=pair<long long,pair<int,int>>;
- int main(){
- int n,m,x,y;
- int t;
- scanf("%d%d%d%d%d",&n,&m,&t,&x,&y);
- vector <pair<int,int>> g[n+10];
- vector <vector<long long>> dis(n+10,vector <long long>(10,INF) );
- vector <vector<bool>> visited(n+10,vector <bool>(10,false) );
- for(int i=1;i<=m;i++){
- int u,v,w;
- scanf("%d%d%d",&u,&v,&w);
- g[u].push_back({v,w});
- }
- priority_queue <pii,vector<pii>,greater<pii>> pq;
- dis[x][1]=0;
- pq.push({dis[x][1],{x,1}});
- while(pq.size()>0){
- int u,c;
- long long d;
- d=pq.top().first;
- u=pq.top().second.first;
- c=pq.top().second.second;
- pq.pop();
- if(u==y && c==t){
- printf("%lld",d);
- return 0;
- }
- for(auto vw:g[u]){
- int v,w,cv;
- v=vw.first;
- w=vw.second;
- cv=c+1;
- if(cv==t+1) cv=1;
- if(dis[u][c%(t+1)]+w<dis[v][cv%(t+1)]){
- dis[v][cv%(t+1)]=dis[u][c%(t+1)]+w;
- pq.push({dis[v][cv%(t+1)],{v,cv%(t+1)}});
- }
- }
- }
- printf("-1");
- return 0;
- }
Add Comment
Please, Sign In to add comment