iscsi

Codeforces #77 Volleyball

Sep 24th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cctype>
  12. #include <cmath>
  13. using namespace std;
  14. typedef long long LL;
  15. #define FOR(k,a,b) for(int k(a); k < (b); ++k)
  16. #define REP(k,a) for(int k=0; k < (a); ++k)
  17.  
  18. int main()
  19. {
  20.     int n,m,S,E,tmp1,tmp2,tmp3;
  21.     cin >> n >> m >> S >> E;
  22.     --S;
  23.     --E;
  24.     vector<vector<pair<int, int> > > v(n);
  25.     vector<vector<int> > a(n);
  26.     vector<pair<int,int> > t(n);
  27.     REP(i,m)
  28.     {
  29.         cin >> tmp1 >> tmp2 >> tmp3;
  30.         --tmp1;
  31.         --tmp2;
  32.         tmp3;
  33.         v[tmp1].push_back(make_pair(tmp2,tmp3));
  34.         v[tmp2].push_back(make_pair(tmp1,tmp3));
  35.     }
  36.     REP(i,n)
  37.     {
  38.         cin >> t[i].first >> t[i].second ;
  39.     }
  40.     REP(i,n)
  41.     {//which nodes are not further than t[i].first
  42.         int max_dist=t[i].first+1;
  43.         vector<int> d(n,max_dist);
  44.         set<pair<int,int> > s;
  45.         s.insert(make_pair(0,i));
  46.         d[i]=0;
  47.         while(!s.empty())
  48.         {
  49.             int actN=s.begin()->second;
  50.             a[i].push_back(actN);
  51.             int actD=s.begin()->first;
  52.             s.erase(s.begin());
  53.             REP(j,v[actN].size())
  54.             {
  55.                 if(actD+v[actN][j].second<d[v[actN][j].first])
  56.                 {
  57.                     if(d[v[actN][j].first]<max_dist)
  58.                     {
  59.                         s.erase(s.find(make_pair(d[v[actN][j].first],v[actN][j].first)));
  60.                     }
  61.                     s.insert(make_pair(actD+v[actN][j].second,v[actN][j].first));
  62.                     d[v[actN][j].first]=actD+v[actN][j].second;
  63.                 }
  64.             }
  65.         }
  66.     }
  67.     const LL maxLL=1000000000000000LL;
  68.     vector<LL> c(n,maxLL);
  69.     c[S]=0;
  70.     set<pair<LL,int> > s;
  71.     s.insert(make_pair(0,S));
  72.     while(!s.empty())
  73.     {
  74.         int actN=s.begin()->second;
  75.         LL actC=s.begin()->first;
  76.         if(actN==E)
  77.         {
  78.             cout << actC << endl;
  79.             return 0;
  80.         }
  81.         s.erase(s.begin());
  82.         REP(i,a[actN].size())
  83.         {
  84.             if(actC+t[actN].second<c[a[actN][i]])
  85.             {
  86.                 if(c[a[actN][i]]<maxLL)
  87.                 {
  88.                     s.erase(s.find(make_pair(c[a[actN][i]],a[actN][i])));
  89.                 }
  90.                 s.insert(make_pair(actC+t[actN].second,a[actN][i]));
  91.                 c[a[actN][i]]=actC+t[actN].second;
  92.             }
  93.         }
  94.     }
  95.     cout << -1 << endl;
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment