Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<vector>
- #include<queue>
- #define inf 1e18+1
- using namespace std;
- typedef long long int ll;
- struct edge{
- int v;
- ll w;
- };
- struct elem{
- int u;
- ll dis;
- ll cnt;
- bool operator<(const elem& rhs)const
- {
- /* if(dis == rhs.dis){
- return cnt < rhs.cnt;
- }*/
- return dis > rhs.dis;
- }
- };
- int main()
- {
- //var
- int n,m,T;
- int s,d;
- vector<edge> adj[10010];
- //input
- scanf("%d %d %d",&n,&m,&T);
- scanf("%d %d",&s,&d);
- for(int i = 0 ; i < m ; i ++){
- int u,v;
- ll w;
- scanf("%d %d %lld",&u,&v,&w);
- adj[u].push_back({v,w});
- }
- //dijktra
- priority_queue<elem> pq;
- ll dist[n+1][T];
- bool visited[n+1][T];
- for(int i = 0 ; i <= n ; i ++)
- {
- for(int j = 0 ; j < T ; j ++){
- dist[i][j] = inf;
- visited[i][j] = false;
- }
- }
- dist[s][1%T] = 0;
- pq.push({s,dist[s][1%T],1});
- while(!pq.empty()){
- int u = pq.top().u;
- int cnt = pq.top().cnt;
- // printf("Test elem (%d,%d) dist : %d\n",u,cnt%T,dist[u][cnt%T]);
- pq.pop();
- if(visited[u][cnt%T])continue;
- visited[u][cnt%T] = true;
- if(u == d && cnt % T == 0){
- break;
- }
- for(int i = 0 ; i < adj[u].size() ; i ++){
- int v = adj[u][i].v;
- int w = adj[u][i].w;
- // printf("Test if %d < %d ?\n",dist[u][cnt%T] + w,dist[v][(cnt+1)%T]);
- if(!visited[v][(cnt+1)%T] && dist[u][cnt%T] + w <= dist[v][(cnt+1)%T]){
- dist[v][(cnt+1)%T] = dist[u][cnt%T] + w;
- pq.push({v,dist[v][(cnt+1)%T],cnt+1});
- }
- }
- }
- printf("%lld",(dist[d][0] == inf ? -1 : dist[d][0]));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment