Advertisement
Guest User

bogdan e tare

a guest
Jan 28th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <map>
  5. #include <queue>
  6. #define DIM 1030
  7. #define DIMM 16005
  8. #define DIMT 130
  9. #define INF 2000000000
  10. #define pint vector< pair<int, int> >::iterator
  11. #define infile "patrol.in"
  12. #define outfile "patrol.out"
  13.  
  14. using namespace std;
  15.  
  16. ifstream f(infile);
  17. ofstream g(outfile);
  18.  
  19. struct comp {
  20.  
  21.     bool operator()(pair< pair<int, int>, int> a, pair< pair<int, int>, int> b) {
  22.         return a.second > b.second;
  23.     }
  24.  
  25. };
  26.  
  27. priority_queue < pair< pair<int, int>, int>, vector< pair< pair<int, int>, int> >, comp > H;
  28.  
  29. int n, m, p;
  30.  
  31. int cost[DIM], polizei[530][10], dp[DIM][DIMT], ok[DIM][DIMT];
  32.  
  33. bool okNode[DIM][DIMT], okEdge[DIMM][DIMT];
  34.  
  35. vector< pair<int, int> > L[DIM];
  36.  
  37. map< pair<int, int>, int> muchii;
  38.  
  39. int main() {
  40.  
  41.     f >> n >> m >> p;
  42.  
  43.     for (int i = 1; i <= n; ++i)
  44.         f >> cost[i];
  45.  
  46.     int nrMuchii = 0;
  47.  
  48.     for (int i = 1; i <= m; ++i) {
  49.          
  50.         int x, y;
  51.  
  52.         f >> x >> y;
  53.  
  54.         muchii[make_pair(x, y)] = ++nrMuchii;
  55.         L[x].push_back(make_pair(y, nrMuchii));
  56.  
  57.         muchii[make_pair(y, x)] = ++nrMuchii;
  58.         L[y].push_back(make_pair(x, nrMuchii));
  59.  
  60.     }
  61.  
  62.     int cmmmc = 1;
  63.  
  64.     for (int i = 1; i <= p; ++i) {
  65.  
  66.         f >> polizei[i][0];
  67.  
  68.         for (int j = 1; j <= polizei[i][0]; ++j)
  69.             f >> polizei[i][j];
  70.  
  71.         int a = cmmmc, b = 2 * (polizei[i][0] - 1);
  72.  
  73.         cmmmc *= 2 * (polizei[i][0] - 1);
  74.  
  75.         while (b) {
  76.  
  77.             int r = a%b;
  78.             a = b;
  79.             b = r;
  80.          
  81.         }
  82.  
  83.         cmmmc /= a;
  84.  
  85.     }
  86.  
  87.     for (int i = 1; i <= p; ++i) {
  88.  
  89.         for (int step = 0, j = 1, dir = 1; step < cmmmc; ++step) {
  90.  
  91.             if (j == polizei[i][0] + 1)
  92.                 dir = -1, j = polizei[i][0] - 1;
  93.  
  94.             if (j == 0)
  95.                 dir = 1, j = 2;
  96.  
  97.             okNode[polizei[i][j]][step] = 1;
  98.  
  99.             int nextj = j + dir;
  100.  
  101.             if (nextj == polizei[i][0] + 1)
  102.                 nextj = polizei[i][0] - 1;
  103.             else
  104.                 if (nextj == 0)
  105.                     nextj = 2;
  106.  
  107.             int edge = muchii[make_pair(polizei[i][nextj], polizei[i][j])];
  108.  
  109.             okEdge[edge][step] = 1;
  110.  
  111.             j += dir;
  112.  
  113.         }
  114.  
  115.     }
  116.  
  117.     for (int i = 1; i <= n; ++i)
  118.         for (int j = 0; j < cmmmc; ++j)
  119.             dp[i][j] = INF;
  120.  
  121.     H.push(make_pair(make_pair(1, 0), cost[1]));
  122.  
  123.     dp[1][0] = cost[1];
  124.  
  125.     while (!H.empty()) {
  126.  
  127.         while (!H.empty() && ok[H.top().first.first][H.top().first.second])
  128.             H.pop();
  129.  
  130.         if (H.empty())
  131.             break;
  132.  
  133.         int node = H.top().first.first;
  134.         int crtTime = H.top().first.second;
  135.  
  136.         dp[node][crtTime] = H.top().second;
  137.  
  138.         ok[node][crtTime] = 1;
  139.  
  140.         int nextTime = crtTime + 1;
  141.  
  142.         if (nextTime >= cmmmc)
  143.             nextTime -= cmmmc;
  144.  
  145.         for (pint it = L[node].begin(); it != L[node].end(); ++it) {
  146.  
  147.             if (okNode[it->first][nextTime] || okEdge[it->second][crtTime])
  148.                 continue;
  149.  
  150.             if (dp[it->first][nextTime] > dp[node][crtTime] + cost[it->first])
  151.                 H.push(make_pair(make_pair(it->first, nextTime), dp[node][crtTime] + cost[it->first]));
  152.  
  153.         }
  154.  
  155.     }
  156.  
  157.     int sol = INF;
  158.  
  159.     for (int i = 0; i < cmmmc; ++i)
  160.         sol = std::min(sol, dp[n][i]);
  161.  
  162.     g << sol;
  163.  
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement