Advertisement
Guest User

Network

a guest
May 28th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. typedef long long ll;
  4. using namespace std;
  5.  
  6. #define all(x) x.begin(), x.end()
  7. #define f(i,a,b) for(int i = (a); i <= (b); i++)
  8. #define fd(i,a,b) for(int i = (a); i >= (b); i--)
  9. #define mp make_pair
  10. #define faster_io() ios_base::sync_with_stdio(false)
  11. #define pb push_back
  12. #define pii pair<int,int>
  13. #define SZ(x) ((int)x.size())
  14. #define vii vector<pair<int,int>>
  15.  
  16. const int INF = 1000000000;
  17. const ll INFLL = 100000000000000000ll;
  18. const ll MOD = 1000000007;
  19.  
  20. // ----------------------------------------------------------------------------------------------------------
  21.  
  22. int aa[1005], bb[1005], cc[1005], buf[6][64500], pos[6], sz[6], n, m;
  23.  
  24. int tryOut(vector<int> p)
  25. {
  26.     int buf_sz = 0, curr = 0, ret = 0;
  27.     f(i,1,n) pos[i] = 1;
  28.  
  29.     f(i,1,m)
  30.     {
  31.         int a = aa[i], b = bb[i], c = cc[i];
  32.         buf_sz += c-b+1;
  33.         buf[a][b] = c;
  34.         int curr_msg = p[curr];
  35.  
  36.         while(buf[curr_msg][pos[curr_msg]])
  37.         {
  38.             int nx = buf[curr_msg][pos[curr_msg]] + 1;
  39.             buf_sz -= nx - pos[curr_msg];
  40.             buf[curr_msg][pos[curr_msg]] = 0;
  41.             pos[curr_msg] = nx;
  42.             if(pos[curr_msg] > sz[curr_msg])
  43.             {
  44.                 curr++;
  45.                 if(curr < SZ(p)) curr_msg = p[curr];
  46.             }
  47.         }
  48.  
  49.         ret = max(ret, buf_sz);
  50.     }
  51.  
  52.     return ret;
  53. }
  54.  
  55. int main()
  56. {
  57.     f(t,1,1000000)
  58.     {
  59.         cin >> n >> m;
  60.         if(n <= 0) return 0;
  61.  
  62.         f(i,1,n) cin >> sz[i];
  63.         f(i,1,m) cin >> aa[i] >> bb[i] >> cc[i];
  64.  
  65.         vector<int> perm;
  66.         f(i,1,n) perm.pb(i);
  67.  
  68.         int ans = 999999;
  69.  
  70.         do
  71.         {
  72.             int k = tryOut(perm);
  73.             ans = min(ans, k);
  74.         }while(next_permutation(all(perm)));
  75.  
  76.         cout << "Case " << t << ": " << ans << "\n\n";
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement