Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. #define  inf 1000000000
  6.  
  7. using namespace std;
  8. ifstream fin("input.txt");
  9. ofstream fout("output.txt");
  10. int main()
  11. {
  12.     int N,M,da,to,a,b,p;
  13.     da--;
  14.     to--;
  15.     fin>>N>>M;
  16.     fin>>da>>to;
  17.     vector<pair<int,int>> G[N];
  18.     for(int i=0; i<M; i++)
  19.     {
  20.         fin>>a>>b>>p;
  21.         a--;
  22.         b--;
  23.         G[a].push_back(make_pair(b,p));
  24.         G[b].push_back(make_pair(a,p));
  25.     }
  26.     vector<int> dist(N,inf);
  27.     queue<int> q;
  28.     q.push(da);
  29.     dist[da]=0;
  30.     while(!q.empty())
  31.     {
  32.         int curr=q.front();
  33.         q.pop();
  34.         for(auto x:G[curr])
  35.         {
  36.             if(dist[curr]+x.second<dist[x.first])
  37.             {
  38.                 q.push(x.first);
  39.                 dist[x.first]=dist[curr]+x.second;
  40.             }
  41.         }
  42.     }
  43.     fout<<dist[to];
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement