Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : Budget Travelling
- Author : Phumipat C. [MAGCARI]
- Language : C++
- */
- #include<bits/stdc++.h>
- using namespace std;
- struct A{
- int v,w;
- bool operator < (const A&o) const{
- if(w!=o.w) return w>o.w;
- else return v>o.v;
- }
- };
- struct B{
- int v,w;
- };
- priority_queue<A > heap;
- vector<B > g[10010];
- int dis[10010],dis2[10010];
- int main(){
- int n,m,st,en,k,u,v,w;
- scanf("%d %d %d %d %d",&n,&m,&st,&en,&k);
- for(int i=1;i<=m;i++){
- scanf("%d %d %d",&u,&v,&w);
- g[u].push_back({v,w});
- g[v].push_back({u,w});
- }
- for(int i=0;i<n;i++)
- dis[i] = dis2[i] = 1e9;
- dis[st] = 0;
- heap.push({st,0});
- while(!heap.empty()){
- A now = heap.top();
- heap.pop();
- if(now.v == en){
- printf("%d %d 0\n",en,now.w);
- return 0;
- }
- for(auto x:g[now.v]){
- if(x.w+now.w <= k && dis[x.v] > x.w+now.w){
- dis[x.v] = x.w+now.w;
- heap.push({x.v,dis[x.v]});
- }
- }
- }
- dis2[en] = 0;
- heap.push({en,0});
- while(!heap.empty()){
- A now = heap.top();
- heap.pop();
- if(dis[now.v] != 1e9){
- printf("%d %d %d",now.v,dis[now.v],dis2[now.v]);
- return 0;
- }
- for(auto x:g[now.v]){
- if(dis2[x.v] > x.w+now.w){
- dis2[x.v] = x.w+now.w;
- heap.push({x.v,dis2[x.v]});
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment