Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include<fstream>
  3. #include<vector>
  4. #include<fstream>
  5. #include<algorithm>
  6. #include<cmath>
  7. #define Nmax 20000002
  8. typedef int coada[30001];
  9. using namespace std;
  10. ifstream f("sate.in");
  11. ofstream g("sate.out");
  12. int cst[30001],n,m,x,y,i,j,cost,q,k,l;
  13. coada c;
  14. vector<pair <int,int> >v[30001];
  15. inline void BFS(int nod)
  16. {
  17. cst[nod]=0;
  18. int siz=1,start=1;
  19. c[start]=nod;
  20. while(siz>=start)
  21. {
  22. int k=c[start];
  23. for(int i=0;i<v[k].size();i++)
  24. if(cst[v[k][i].first]>cst[k]+v[k][i].second)
  25. {
  26. cst[v[k][i].first]=cst[k]+v[k][i].second;
  27. siz++;
  28. c[siz]=v[k][i].first;
  29. }
  30. start++;
  31. }
  32. }
  33. int main()
  34. {
  35. f>>n>>m>>x>>y;
  36. for(q=1;q<=m;q++)
  37. {
  38. f>>i>>j>>cost;
  39. v[i].push_back(make_pair(j,cost));
  40. v[j].push_back(make_pair(i,cost));
  41. for(k=0;k<v[i].size()-1;k++)
  42. for(l=1;l<v[i].size();l++)
  43. {
  44. if(binary_search(v[v[i][k].first].begin(),v[v[i][k].first].end(),v[i][l].first))
  45. {v[v[i][k].first].push_back(make_pair(v[i][l].first,abs( v[i][k].second-v[i][l].second )));
  46. v[v[i][l].first].push_back(make_pair(v[i][k].first,abs( v[i][k].second-v[i][l].second )));}
  47. }
  48. for(k=0;k<v[j].size()-1;k++)
  49. for(l=1;l<v[j].size();l++)
  50. {
  51. if(binary_search(v[v[j][k].first].begin(),v[v[j][k].first].end(),v[j][l].first))
  52. {v[v[j][k].first].push_back(make_pair(v[j][l].first,abs( v[j][k].second-v[j][l].second )));
  53. v[v[j][l].first].push_back(make_pair(v[j][k].first,abs( v[j][k].second-v[j][l].second )));}
  54. }
  55. }
  56. for(i=1;i<=n;i++)
  57. cst[i]=Nmax;
  58. BFS(x);
  59. g<<cst[y];
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement