fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int _test, TEST_FROM = INT_MIN, TEST_TO = INT_MAX;
  4.  
  5. const int MAX = 1010;
  6. int color[MAX][32];
  7. int N, K, P;
  8. vector<int> adjw[MAX];
  9. vector<int> tree[MAX];
  10.  
  11. void Dfs(int i, int par) {
  12. for (auto x : adjw[i])
  13. if (x != par) {
  14. tree[i].push_back(x);
  15. Dfs(x, i);
  16. }
  17. }
  18. void ReadInput() {
  19. scanf("%d %d %d", &N, &K, &P);
  20. for (int i = 0; i < N; ++i) {
  21. for (int j = 0; j < K; ++j)
  22. scanf("%d ", &color[i][j]);
  23. adjw[i].clear();
  24. tree[i].clear();
  25. }
  26. int a, b;
  27. for (int i = 1; i < N; ++i) {
  28. scanf("%d %d", &a, &b);
  29. --a, --b;
  30. adjw[a].push_back(b);
  31. adjw[b].push_back(a);
  32. }
  33. Dfs(0, -1);
  34. }
  35.  
  36. long long dp[MAX][32][32];
  37.  
  38. #define FOR(i,n) for(int i = 0 ; i < (n) ; i++)
  39. #define Set(a, x) memset( a, x, sizeof( a ) )
  40. #define INF 987654321
  41. #define MAX 60
  42. set<pair<int,int> > h;
  43. int cap[MAX][MAX],cost[MAX][MAX], fnet[MAX][MAX], adj[MAX][MAX], deg[MAX],pi[MAX] ,par[MAX],d[MAX];
  44.  
  45. #define Pot(u,v) (d[u] + pi[u] - pi[v])
  46. inline void add( int v , int dis , int d_old ) {
  47. pair<int,int> p(dis,v) , old( d_old , v); h.erase(old); h.insert( p );
  48. }
  49. bool dijkstra( int n, int s, int t ) {
  50. Set( d, 0x3F ) ; Set( par, -1 ); h.clear(); par[s] = n ; d[s] = 0; add( s , 0 , 0 );
  51.  
  52. while( h.size() ) {
  53. int old, u = h.begin()->second; h.erase(h.begin());
  54.  
  55. for( int k = 0, v = adj[u][k] ; old = d[v] , k < deg[u] ; v = adj[u][++k] ){
  56. if( fnet[v][u] && d[v] > Pot(u,v) - cost[v][u] )
  57. d[v] = Pot(u,v) - cost[v][par[v] = u];
  58. if( fnet[u][v] < cap[u][v] && d[v] > Pot(u,v) + cost[u][v] )
  59. d[v] = Pot(u,v) + cost[par[v] = u][v];
  60. if( par[v] == u )
  61. add( v , d[v], old );
  62. } }
  63. for( int i = 0; i < n; i++ ) if( pi[i] < INF ) pi[i] += d[i];
  64. return par[t] >= 0;
  65. }
  66. int mcmf( int n, int s, int t, int &fcost ) {
  67. Set( deg, 0 ); Set( fnet, 0 ); Set( pi, 0 );
  68. int i , j , bot , u , v , flow = fcost = 0;
  69. FOR(i,n) FOR(j,n) if( cap[i][j] || cap[j][i] ) adj[i][deg[i]++] = j;
  70.  
  71. for( ; dijkstra( n, s, t ) ; flow += bot ) {
  72. for( bot = INF , v = t, u = par[v]; v != s; u = par[v = u] )
  73. bot = min( bot , fnet[v][u] ? fnet[v][u] : ( cap[u][v] - fnet[u][v] ));
  74.  
  75. for( v = t , u = par[v] ; v != s ; u = par[ v = u ] )
  76. if( fnet[v][u] ) { fnet[v][u] -= bot; fcost -= bot * cost[v][u]; }
  77. else { fnet[u][v] += bot; fcost += bot * cost[u][v]; }
  78. }
  79. return flow;
  80. }
  81.  
  82. int Match(int v, int pc, int vc) {
  83. int w = 0;
  84. map<int, int> ind;
  85. int n = tree[v].size();
  86. for (auto x : tree[v])
  87. ind[x] = ++w;
  88. int source = 0;
  89. int sink = n + K + 1;
  90. int ng = sink+1;
  91. Set(cap, 0);
  92. for (int i = 1; i <= n; ++i) {
  93. cap[source][i] = 1;
  94. cost[source][i] = 0;
  95. }
  96. for (int i = 1; i <= K; ++i) {
  97. cap[n+i][sink] = 1;
  98. cost[n+i][sink] = 0;
  99. }
  100. for (auto x : tree[v]) {
  101. for (int c = 0; c < K; ++c) {
  102. if (c != pc) {
  103. cap[ind[x]][n+c+1] = 1;
  104. cost[ind[x]][n+c+1] = dp[x][c][vc];
  105. }
  106. }
  107. }
  108. int fcost = 0;
  109. int res = mcmf(ng, source, sink, fcost);
  110. if (res != n)
  111. return 1 << 29;
  112. return fcost;
  113. }
  114.  
  115. long long Go(int i, int c, int pc) {
  116. long long &ret = dp[i][c][pc];
  117. if (ret != -1)
  118. return ret;
  119. long long sum = color[i][c];
  120.  
  121. for (auto x : tree[i]) {
  122. long long u = 1LL << 60;
  123. for (int d = 0; d < K; ++d) {
  124. u = min(u, Go(x, d, c));
  125. }
  126. sum += u;
  127. }
  128. sum += P;
  129. int am = tree[i].size() + (pc < K);
  130. if (am <= K) {
  131. sum = min(sum, (long long)color[i][c] + Match(i, pc, c));
  132. }
  133. return ret = sum;
  134. }
  135. void Solve() {
  136. memset(dp, -1, sizeof dp);
  137. long long ans = 1LL << 60;
  138. for (int c = 0; c < K; ++c)
  139. ans = min(ans, Go(0, c, K));
  140. printf("%lld\n", ans);
  141. }
  142.  
  143. int main(int argc, char* argv[]) {
  144. if (argc == 3) {
  145. TEST_FROM = atoi(argv[1]), TEST_TO = atoi(argv[2]);
  146. }
  147. int ntests;
  148. scanf("%d", &ntests);
  149. for (_test = 1; _test <= ntests; ++_test) {
  150. ReadInput();
  151. if (_test >= TEST_FROM && _test <= TEST_TO) {
  152. printf("Case #%d: ", _test);
  153. Solve();
  154. }
  155. }
  156. return 0;
  157. }
  158.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'void Dfs(int, int)':
prog.cpp:12:13: error: 'x' does not name a type
   for (auto x : adjw[i])
             ^
prog.cpp:17:1: error: expected ';' before '}' token
 }
 ^
prog.cpp:17:1: error: expected primary-expression before '}' token
prog.cpp:17:1: error: expected ';' before '}' token
prog.cpp:17:1: error: expected primary-expression before '}' token
prog.cpp:17:1: error: expected ')' before '}' token
prog.cpp:17:1: error: expected primary-expression before '}' token
prog.cpp: In function 'int Match(int, int, int)':
prog.cpp:86:13: error: 'x' does not name a type
   for (auto x : tree[v]) 
             ^
prog.cpp:88:3: error: expected ';' before 'int'
   int source = 0;
   ^
prog.cpp:89:3: error: expected primary-expression before 'int'
   int sink = n + K + 1;
   ^
prog.cpp:89:3: error: expected ')' before 'int'
prog.cpp:90:12: error: 'sink' was not declared in this scope
   int ng = sink+1;
            ^
prog.cpp:93:9: error: 'source' was not declared in this scope
     cap[source][i] = 1;
         ^
prog.cpp:100:13: error: 'x' does not name a type
   for (auto x : tree[v]) {
             ^
prog.cpp:108:3: error: expected ';' before 'int'
   int fcost = 0;
   ^
prog.cpp:109:3: error: expected primary-expression before 'int'
   int res = mcmf(ng, source, sink, fcost);
   ^
prog.cpp:109:3: error: expected ')' before 'int'
prog.cpp:109:22: error: 'source' was not declared in this scope
   int res = mcmf(ng, source, sink, fcost);
                      ^
prog.cpp:110:7: error: 'res' was not declared in this scope
   if (res != n)
       ^
prog.cpp:112:10: error: 'fcost' was not declared in this scope
   return fcost;
          ^
prog.cpp: In function 'long long int Go(int, int, int)':
prog.cpp:121:13: error: 'x' does not name a type
   for (auto x : tree[i]) {
             ^
prog.cpp:128:3: error: expected ';' before 'sum'
   sum += P;
   ^
prog.cpp:129:3: error: expected primary-expression before 'int'
   int am = tree[i].size() + (pc < K);
   ^
prog.cpp:129:3: error: expected ')' before 'int'
prog.cpp:130:7: error: 'am' was not declared in this scope
   if (am <= K) {
       ^
stdout
Standard output is empty