Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5.  
  6. typedef vector<ll> vll;
  7. typedef vector<vll> vvll;
  8. const ll INF = 1LL << 60;
  9.  
  10. struct MCMF {
  11. int N;
  12. vll found, dad, dist, pi;
  13. vvll cap, flow, cost;
  14.  
  15. MCMF(int N): N(N), cap(N, vll(N)), flow(cap), cost(cap),
  16. dad(N), found(N), pi(N), dist(N+1) {};
  17.  
  18. void add_edge(int from, int to, ll ca, ll co) {
  19. cap[from][to] = ca; cost[from][to] = co;
  20. }
  21. bool search(int source, int sink) {
  22. fill(found.begin(), found.end(), 0);
  23. fill(dist.begin(), dist.end(), INF);
  24. dist[source] = 0;
  25. while(source != N) {
  26. int best = N;
  27. found[source] = 1;
  28. for(int k = 0; k < N; k++) {
  29. if(found[k]) continue;
  30. if(flow[k][source]) {
  31. ll val = dist[source] + pi[source] - pi[k] - cost[k][source];
  32. if(dist[k] > val) {
  33. dist[k] = val;
  34. dad[k] = source;
  35. }
  36. }
  37. if(flow[source][k] < cap[source][k]) {
  38. ll val = dist[source] + pi[source] - pi[k] + cost[source][k];
  39. if(dist[k] > val) {
  40. dist[k] = val;
  41. dad[k] = source;
  42. }
  43. }
  44. if(dist[k] < dist[best]) best = k;
  45. }
  46. source = best;
  47. }
  48. for(int k = 0; k < N; k++)
  49. pi[k] = min((ll)(pi[k] + dist[k]), INF);
  50. return found[sink];
  51. }
  52. pair<ll, ll> mcmf(int source, int sink) {
  53. ll totflow = 0, totcost = 0;
  54. while(search(source, sink)) {
  55. ll amt = INF;
  56. for(int x = sink; x != source; x = dad[x])
  57. amt = min(amt, (ll)(flow[x][dad[x]] != 0 ?
  58. flow[x][dad[x]] : cap[dad[x]][x] - flow[dad[x]][x]));
  59. for(int x = sink; x != source; x = dad[x]) {
  60. if(flow[x][dad[x]] != 0) {
  61. flow[x][dad[x]] -= amt;
  62. totflow -= amt * cost[x][dad[x]];
  63. } else {
  64. flow[dad[x]][x] += amt;
  65. totcost += amt * cost[dad[x]][x];
  66. }
  67. }
  68. totflow += amt;
  69. }
  70. return {totflow, totcost};
  71. }
  72. };
  73.  
  74. int ts[505][505];
  75. int ps[505];
  76. int fl[505][3];
  77. bool adj[505][505];
  78. int n,m;
  79.  
  80. bool valid(int airplanes) {
  81. MCMF alg(2*m+5);
  82. for(int i = 0; i < m; i++) {
  83. for(int j = 0; j < m; j++) {
  84. if(i == j) alg.add_edge(i,m+j,1,-1);
  85. else if(adj[i][j]) alg.add_edge(m+i,j,1,0);
  86. }
  87. alg.add_edge(2*m+1,i,1,0);
  88. alg.add_edge(m+i,2*m+2,1,0);
  89. }
  90. alg.add_edge(2*m+3,2*m+1,airplanes,0);
  91. return alg.mcmf(2*m+3,2*m+2).second == -m;
  92. }
  93.  
  94. int main() {
  95. ios_base::sync_with_stdio(0);
  96. cin.tie(0); cout.tie(0); cout.precision(15);
  97.  
  98. cin >> n >> m;
  99. for(int i = 0; i < n; i++) cin >> ps[i];
  100. for(int i = 0; i < n; i++) {
  101. for(int j = 0; j < n; j++) {
  102. cin >> ts[i][j];
  103. }
  104. }
  105. for(int i = 0; i < m; i++) {
  106. cin >> fl[i][0] >> fl[i][1] >> fl[i][2];
  107. }
  108. for(int i = 0; i < m; i++) {
  109. for(int j = 0; j < m; j++) {
  110. if( i == j ) continue;
  111. adj[i][j] = (fl[i][1] == fl[j][0]) && (fl[i][2] + ts[ fl[i][0] ][ fl[i][1] ] + ps[ fl[i][1] ] <= fl[j][2]);
  112. }
  113. }
  114.  
  115. int low = 0, high = 505;
  116. while(high - low > 1) {
  117. int mid = (low + high) / 2;
  118. if( valid(mid) ) high = mid;
  119. else low = mid;
  120. }
  121. if(valid(low)) high = low;
  122.  
  123. cout << high << endl;
  124.  
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement