結果

問題 No.19 ステージの選択
ユーザー mudbdb
提出日時 2015-07-14 22:36:16
言語 C90
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 2,239 bytes
コンパイル時間 231 ms
コンパイル使用メモリ 24,448 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-23 10:03:07
合計ジャッジ時間 1,078 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘main’:
main.c:18:3: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   18 |   scanf("%d", &N);
      |   ^~~~~~~~~~~~~~~
main.c:23:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   23 |     scanf("%d", &(L[i]));
      |     ^~~~~~~~~~~~~~~~~~~~
main.c:24:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   24 |     scanf("%d", &(S[i]));
      |     ^~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
struct cycle {
  struct cycle *next;
  int nu;
  int *mem;
};
struct cycle *Root = 0;
int cmp(const void* a, const void* b)
{
  return (*(int*)a - *(int*)b);
}
int main()
{
  int N;
  int *L;
  int *S;
  scanf("%d", &N);
  L = (int*)malloc(sizeof(int)*N);
  S = (int*)malloc(sizeof(int)*N);
  int i;
  for (i=0; i<N; i++) {
    scanf("%d", &(L[i]));
    scanf("%d", &(S[i]));
  }

  int *path = (int*)malloc(sizeof(int)*N);
  int path_i = 0;
  int *work = (int*)malloc(sizeof(int)*N);
  int work_i = 0;
  int *v = (int*)malloc(sizeof(int)*N);
  for (i=0; i<N; i++) v[i] = 0;
  int no;
  int up;
  int j;
  struct cycle *p;
  for (no=1; no<=N; no++) {
    if (v[no-1] == 0) {
      v[no-1] = 1;
      path_i = 0;
      path[path_i] = no;
      path_i++;
      up = S[no-1];
      while (1) {
        if (path_i > N) {
          printf("!?\n");
          exit(1);
        }
        for (i=path_i-1; i>=0; i--) {
          if (up == path[i]) {
            work_i = path_i - i;
            for (j=0; j<work_i; j++) work[j] = path[j+i];
            qsort(work, work_i, sizeof(int), cmp);
            for (p=Root; p!=0; p=p->next) {
              if (p->mem[0] == work[0]) {
                goto SKIP;
              }
            }
            p = (struct cycle*)malloc(sizeof(struct cycle));
            p->next = Root;
            Root = p;
            p->nu = work_i;
            p->mem = (int*)malloc(sizeof(int)*p->nu);
            for (j=0; j<p->nu; j++) p->mem[j] = work[j];
            goto SKIP;
          }
        }
        path[path_i] = up;
        path_i++;
        v[up-1] = 1;
        up = S[up-1];
      }
      SKIP:
      ;
    }
  }

  int *ldwn = (int*)malloc(sizeof(int)*N);
  for (i=0; i<N; i++) ldwn[i] = 1;
  int min;
  int min_i;
  for (p=Root; p!=0; p=p->next) {
    min = ~(1 << 31);
    for (i=0; i<p->nu; i++) {
      if (L[p->mem[i]-1] < min) {
        min = L[p->mem[i]-1];
        min_i = p->mem[i]-1;
      }
    }
    ldwn[min_i] = 0;
  }
  float total = 0;
  for (i=0; i<N; i++) {
    if (ldwn[i] == 1) {
      total += L[i];
    }
  }
  total /= 2;
  for (i=0; i<N; i++) {
    if (ldwn[i] == 0) {
      total += L[i];
    }
  }
  printf("%.1f\n", total);
  return 0;
}
0