Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<vector>
- #include<algorithm>
- #include<queue>
- #include<cstring>
- using namespace std;
- struct node
- {
- int i;
- int j;
- int c;
- int w;
- };
- bool operator <(const node &a,const node &b)
- {
- if(a.c<b.c) return false;
- return true;
- }
- int d[5005],mat[5005][5005];
- vector<int> sos[5005];
- int n,m,S,E;
- int main()
- {
- cin>>n>>m;
- int i,j,k,l;
- for(i=0;i<=n;i++) d[i]=999999;
- cin>>S>>E;
- for(i=0;i<m;i++)
- {
- int a,b,c;
- cin>>a>>b>>c;
- mat[a][b]=mat[b][a]=c;
- sos[a].push_back(b);
- sos[b].push_back(a);
- }
- node tmp;
- tmp.i=S;
- priority_queue<node> q;
- for(i=0;i<sos[S].size();i++)
- {
- tmp.j=sos[S][i];
- tmp.c=mat[S][tmp.j];
- tmp.w=0;
- tmp.w++;
- q.push(tmp);
- }
- while(!q.empty())
- {
- node tmp=q.top();
- q.pop();
- node t;
- t.i=tmp.j;
- t.c=tmp.c;
- if(t.i==E)
- {
- cout<<tmp.c<<endl<<tmp.w<<endl;
- return 0;
- }
- t.w=tmp.w+1;
- for(i=0;i<sos[t.i].size();i++)
- {
- if(t.c+mat[t.i][sos[t.i][i]]<d[sos[t.i][i]])
- {
- t.j=sos[t.i][i];
- t.c+=mat[t.i][t.j];
- d[t.j]=t.c;
- q.push(t);
- t.c-=mat[t.i][t.j];
- }
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment