結果
問題 | No.357 品物の並び替え (Middle) |
ユーザー |
![]() |
提出日時 | 2017-04-25 17:01:10 |
言語 | C90 (gcc 12.3.0) |
結果 |
AC
|
実行時間 | 4 ms / 5,000 ms |
コード長 | 889 bytes |
コンパイル時間 | 344 ms |
コンパイル使用メモリ | 22,144 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-13 08:50:50 |
合計ジャッジ時間 | 1,145 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 |
コンパイルメッセージ
main.c: In function ‘run’: main.c:28:3: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 28 | scanf("%d%d",&n,&m); | ^~~~~~~~~~~~~~~~~~~ main.c:34:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 34 | scanf("%d%d%d",&i1,&i2,&p); | ^~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include<stdio.h> #include<stdlib.h> #define POS(i,j) ((i)*n+(j)) #define MAX(a,b) ((a)>(b)?(a):(b)) int calc(int bit,int n,int *score,int *dp){ if(dp[bit]>=0) return dp[bit]; int res=0; int i; for(i=0;i<n;i++){ if((bit>>i)&0x01){ int j; int point=0; for(j=0;j<n;j++){ point+=((bit>>j)&0x01)*score[POS(i,j)]; } res=MAX(res,point+calc(bit^(1<<i),n,score,dp)); } } dp[bit]=res; return res; } void run(void){ int n,m; scanf("%d%d",&n,&m); int *score=(int *)calloc(n*n,sizeof(int)); int i; for(i=0;i<m;i++){ int i1,i2,p; scanf("%d%d%d",&i1,&i2,&p); score[POS(i1,i2)]=p; } int *dp=(int *)malloc(sizeof(int)*(1<<n)); for(i=0;i<(1<<n);i++){ dp[i]=-1; } dp[0]=0; int ans=calc((1<<n)-1,n,score,dp); printf("%d\n",ans); free(score); free(dp); return; } int main(void){ run(); return 0; }