結果

問題 No.107 モンスター
ユーザー cielciel
提出日時 2015-10-24 05:27:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 1,157 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 49,064 KB
実行使用メモリ 5,332 KB
最終ジャッジ日時 2023-08-22 23:14:57
合計ジャッジ時間 1,569 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,004 KB
testcase_01 AC 2 ms
5,012 KB
testcase_02 AC 2 ms
5,052 KB
testcase_03 AC 2 ms
5,028 KB
testcase_04 AC 2 ms
5,004 KB
testcase_05 AC 2 ms
5,012 KB
testcase_06 AC 2 ms
5,060 KB
testcase_07 AC 2 ms
5,028 KB
testcase_08 AC 2 ms
5,072 KB
testcase_09 AC 2 ms
5,064 KB
testcase_10 AC 2 ms
5,052 KB
testcase_11 AC 2 ms
5,064 KB
testcase_12 AC 2 ms
5,004 KB
testcase_13 AC 3 ms
5,120 KB
testcase_14 AC 4 ms
5,180 KB
testcase_15 AC 4 ms
5,128 KB
testcase_16 AC 2 ms
5,052 KB
testcase_17 AC 3 ms
5,048 KB
testcase_18 AC 6 ms
5,308 KB
testcase_19 AC 7 ms
5,264 KB
testcase_20 AC 3 ms
5,140 KB
testcase_21 AC 3 ms
5,064 KB
testcase_22 AC 5 ms
5,192 KB
testcase_23 AC 7 ms
5,332 KB
testcase_24 AC 6 ms
5,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <set>
#include <stack>
#include <algorithm>

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define INF 99999999
using namespace std;

typedef int Weight;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

const int M = 20;
Weight best[1<<M];
Weight total[1<<M];
Weight shortestHamiltonPath(const Array &w) {
	int n=w.size();
	int N = 1 << n;
	REP(S,N) best[S] = total[S] = 0;
	best[0] = total[0] = 100;
	REP(S,N) REP(j,n) if (!(S&(1<<j)) && best[S]>0) {
		if (w[j]>0) {
			Weight hp=min(best[S]+w[j],total[S]);
			if (best[S|(1<<j)] < hp) {
				best[S|(1<<j)] = hp;
				total[S|(1<<j)] = total[S];
			}
		} else {
			Weight hp=best[S]+w[j];
			if (hp<=0) continue;
			if (total[S|(1<<j)] < total[S]+100 || (total[S|(1<<j)] == total[S]+100 && best[S|(1<<j)] < hp)) {
				best[S|(1<<j)] = hp;
				total[S|(1<<j)] = total[S]+100;
			}
		}
	}
	return best[N-1];
}

int main(){
	int N;
	scanf("%d",&N);
	Array v(N);
	for(int i=0;i<N;i++)scanf("%d",&v[i]);
	printf("%d\n",shortestHamiltonPath(v));
}
0