Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<ctime>
- #include<map>
- #include<vector>
- #include<queue>
- using namespace std;
- vector < pair<int,int> > g[10005];
- bool visited[10005];
- int main()
- {
- int t1,t2;
- cin>>t1>>t2;
- int k,n;
- cin>>k>>n;
- for(int i=0;i<n;i++)
- {
- int a,b,c;
- cin>>a>>b>>c;
- g[a].push_back(make_pair(b,c));
- g[b].push_back(make_pair(a,c));
- }
- queue <long long> q;
- q.push(t1);
- q.push(0);
- for(int i=0;i<=k;i++)
- {
- visited[i]=false;
- }
- while(!q.empty())
- {
- long long node=q.front();
- q.pop();
- long long dis=q.front();
- q.pop();
- if(node==t2)
- {
- cout<<llabs(dis);
- return 0;
- }
- for(int i=0;i<g[node].size();i++)
- {
- int sosed=g[node][i].first;
- long long d=g[node][i].second;
- if(!visited[sosed])
- {
- visited[sosed]=true;
- q.push(sosed);
- if(node<sosed)
- {
- q.push(dis+d);
- }else
- {
- q.push(dis-d);
- }
- }
- }
- }
- cout<<-1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement