Guest User

Untitled

a guest
Jul 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<queue>
  5. #include<cstring>
  6. using namespace std;
  7. struct node
  8. {
  9.     int i;
  10.     int j;
  11.     int c;
  12.     int w;
  13. };
  14. bool operator <(const node &a,const node &b)
  15. {
  16.     if(a.c<b.c) return false;
  17.     return true;
  18. }
  19. int d[5005],mat[5005][5005];
  20. vector<int> sos[5005];
  21. int n,m,S,E;
  22. int main()
  23. {
  24.     cin>>n>>m;
  25.     int i,j,k,l;
  26.     for(i=0;i<=n;i++) d[i]=999999;
  27.     cin>>S>>E;
  28.     for(i=0;i<m;i++)
  29.     {
  30.         int a,b,c;
  31.         cin>>a>>b>>c;
  32.         mat[a][b]=mat[b][a]=c;
  33.         sos[a].push_back(b);
  34.         sos[b].push_back(a);
  35.     }
  36.     node tmp;
  37.     tmp.i=S;
  38.     priority_queue<node> q;
  39.     for(i=0;i<sos[S].size();i++)
  40.     {
  41.         tmp.j=sos[S][i];
  42.         tmp.c=mat[S][tmp.j];
  43.         tmp.w=0;
  44.         tmp.w++;
  45.         q.push(tmp);
  46.     }
  47.     while(!q.empty())
  48.     {
  49.         node tmp=q.top();
  50.         q.pop();
  51.         node t;
  52.         t.i=tmp.j;
  53.         t.c=tmp.c;
  54.         if(t.i==E)
  55.         {
  56.             cout<<tmp.c<<endl<<tmp.w<<endl;
  57.             return 0;
  58.         }
  59.         t.w=tmp.w+1;
  60.         for(i=0;i<sos[t.i].size();i++)
  61.         {
  62.             if(t.c+mat[t.i][sos[t.i][i]]<d[sos[t.i][i]])
  63.             {
  64.                 t.j=sos[t.i][i];
  65.                 t.c+=mat[t.i][t.j];
  66.                 d[t.j]=t.c;
  67.                 q.push(t);
  68.                 t.c-=mat[t.i][t.j];
  69.             }
  70.         }
  71.     }
  72.     return 0;
  73. }
Add Comment
Please, Sign In to add comment