Advertisement
cmiN

arb2 30%

Mar 30th, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3.  
  4. const unsigned long nMax = 100001, inf = 0xffFFffFE;
  5. unsigned long nodes, *** adList, globLen, count, total, parent[nMax];
  6. bool viz[nMax];
  7.  
  8. struct Q {
  9.     unsigned long node, dist;
  10.     Q()
  11.     {
  12.         node = 0;
  13.         dist = inf;
  14.     }
  15. } queue[nMax];
  16.  
  17. void read()
  18. {
  19.     unsigned long i, x, y, d, c;
  20.     FILE* fin = fopen("arb2.in", "rt");
  21.     fscanf(fin, "%lu", &nodes);
  22.     adList = (unsigned long***) malloc((nodes + 1) * sizeof(unsigned long**));
  23.     for (i = 1; i <= nodes; i++) {
  24.         adList[i] = (unsigned long**) malloc(sizeof(unsigned long*));
  25.         adList[i][0] = (unsigned long*) malloc(sizeof(unsigned long));
  26.         adList[i][0][0] = 0;
  27.     }
  28.     for (i = 1; i < nodes; i++) {
  29.         fscanf(fin, "%lu %lu %lu %lu", &x, &y, &d, &c);
  30.         adList[x][0][0]++;
  31.         adList[x] = (unsigned long**) realloc(adList[x], (adList[x][0][0] + 1) * sizeof(unsigned long*));
  32.         adList[x][adList[x][0][0]] = (unsigned long*) malloc(3 * sizeof(unsigned long));
  33.         adList[x][adList[x][0][0]][0] = y;
  34.         adList[x][adList[x][0][0]][1] = d;
  35.         adList[x][adList[x][0][0]][2] = c;
  36.     }
  37.     fclose(fin);
  38. }
  39.  
  40. void dfs(unsigned long node, unsigned long length)
  41. {
  42.     if (!adList[node][0][0]) {
  43.         queue[count].node = node;
  44.         queue[count].dist = length;
  45.         count++;
  46.         if (length > globLen) {
  47.             globLen = length;
  48.         }
  49.     }
  50.     for (unsigned long i = 1; i <= adList[node][0][0]; i++) {
  51.         parent[adList[node][i][0]] = node;
  52.         dfs(adList[node][i][0], length + adList[node][i][1]);
  53.     }
  54. }
  55.  
  56. void bfs()
  57. {
  58.     unsigned long lo = 0, i, node, minDist, curPar;
  59.     for (i = 0; i < count; i++) {
  60.         queue[i].dist = globLen - queue[i].dist;
  61.     }
  62.     while (lo < count) {
  63.         node = queue[lo].node;
  64.         if (!viz[node]) {
  65.             minDist = inf;
  66.             curPar = parent[node];
  67.             for (i = 0; i < count; i++) {
  68.                 if (parent[queue[i].node] == curPar) {
  69.                     if (minDist > queue[i].dist) {
  70.                         minDist = queue[i].dist;
  71.                     }
  72.                 }
  73.             }
  74.             for (i = 0; i < count; i++) {
  75.                 if (parent[queue[i].node] == curPar) {
  76.                     total += queue[i].dist - minDist;
  77.                     viz[queue[i].node] = 1;
  78.                 }
  79.             }
  80.             if (curPar) {
  81.                 queue[count].node = curPar;
  82.                 queue[count].dist = minDist;
  83.                 count++;
  84.             }
  85.         }
  86.         lo++;
  87.     }
  88. }
  89.  
  90. void process()
  91. {
  92.     parent[1] = 0;
  93.     dfs(1, 0);
  94.     bfs();
  95. }
  96.  
  97. void write()
  98. {
  99.     FILE* fout = fopen("arb2.out", "wt");
  100.     fprintf(fout, "%lu", total);
  101.     fclose(fout);
  102. }
  103.  
  104. int main()
  105. {
  106.     read();
  107.     process();
  108.     write();
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement