Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define needforspeed ios::sync_with_stdio(0);cin.tie(0)
- #define endl '\n'
- #define pb push_back
- #define mp make_pair
- #define mp3(a,b,c) make_pair(a,make_pair(b,c))
- #define mp4(a,b,c,d) make_pair(make_pair(a,b),make_pair(c,d))
- #define trace1(a) cout << (a) << endl;
- #define trace2(a,b) cout << (a) << " " << (b) << endl;
- #define trace3(a,b,c) cout << (a) << " " << (b) << " " << (c) << endl;
- #define trace4(a,b,c) cout << (a) << " " << (b) << " " << (c) << << (d) << endl;
- #define ms(a,b) memset( (a), (b), sizeof(a))
- #define fi(x) freopen(x,"r",stdin)
- #define fo(x) freopen(x,"w",stdout)
- #define all(x) (x).begin(),(x).end()
- #define len(x) (int)(x).size()
- #define xx first
- #define yy second
- #define LL long long
- #define MAXN (int)10005
- #define inf 0x3f3f3f3f
- #define DB 0
- using namespace std;
- int N,M;
- bool visited[MAXN];
- map< pair<int,int>, int>dist;// node,sitance -> occurrences
- vector< pair<int,int> >adj[MAXN];// node,distance
- void dijkstra(int start,int dest){
- set< pair< pair<int,int>,int > >q;// distance, node, occurrences
- q.insert(mp(mp(0,start),1));
- dist[mp(start,0)] = 1;// node,distance -> occurrence
- ms(visited,false);
- visited[start] = true;
- while(!q.empty()){
- pair< pair<int,int>, int> C = *(q.begin());
- swap(C.xx.xx,C.xx.yy);// swap the distance with the node, we only had this so that the set would prioritize by distance
- C.yy = dist[C.xx];// we update it to have the most recent occurrrence count
- // C = (node,distance,occurrences)
- q.erase(q.begin());
- if(C.xx.xx == dest)return;
- if(DB)trace3(C.xx.xx,C.xx.yy,C.yy);
- for(pair<int,int>&E : adj[C.xx.xx]){
- pair< pair<int,int>,int >NEXT = mp(mp(E.xx,E.yy+C.xx.yy),C.yy);
- if(DB){
- cout << "EDGE ";
- trace3(NEXT.xx.xx,NEXT.xx.yy,NEXT.yy);
- }
- if(dist.count(NEXT.xx))
- dist[NEXT.xx]+=NEXT.yy;
- else{
- if(!visited[NEXT.xx.xx]){// the node hasn't been visited
- if(DB)cout << "VISITED" << endl;
- visited[NEXT.xx.xx] = true;
- dist[NEXT.xx] = NEXT.yy;
- swap(NEXT.xx.xx,NEXT.xx.yy);
- q.insert(NEXT);
- }
- else{// the node has been visisted so we want to limit what we push
- pair< pair<int,int>,int> D = *(--dist.upper_bound(NEXT.xx));
- if(DB){
- cout << "VISITED" << endl;
- trace3(D.xx.xx,D.xx.yy,D.yy);
- }
- if(D.xx.yy > NEXT.xx.xx){
- visited[NEXT.xx.xx] = true;
- dist[NEXT.xx] = NEXT.yy;
- swap(NEXT.xx.xx,NEXT.xx.yy);// swap them so it becomes (distance,node,occurrences) again
- q.insert(NEXT);
- }
- }
- }
- }
- }
- }
- int main() {
- #ifdef DBa
- fi("input.txt");
- #endif
- needforspeed;
- cin >> N >> M;
- for(int i = 0;i < M;i++){
- int u,v,w;
- cin >> u >> v >> w;
- adj[u].pb(mp(v,w));
- }
- int s,t;
- cin >> s >> t;
- dijkstra(s,t);
- if(visited[t]){
- pair< pair<int,int>,int> OUT = *(dist.upper_bound(mp(t,-1)));
- cout << OUT.yy << endl;
- }
- else
- cout << -1 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement