結果

問題 No.17 2つの地点に泊まりたい
ユーザー FF256grhyFF256grhy
提出日時 2016-09-03 22:25:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,337 ms / 5,000 ms
コード長 1,757 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 24,704 KB
実行使用メモリ 12,724 KB
最終ジャッジ日時 2024-04-23 02:16:40
合計ジャッジ時間 7,279 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 0 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 23 ms
6,944 KB
testcase_04 AC 19 ms
6,940 KB
testcase_05 AC 485 ms
8,568 KB
testcase_06 AC 192 ms
6,944 KB
testcase_07 AC 115 ms
6,944 KB
testcase_08 AC 1,337 ms
11,772 KB
testcase_09 AC 1,279 ms
12,724 KB
testcase_10 AC 229 ms
8,196 KB
testcase_11 AC 548 ms
9,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 0 ms
6,940 KB
testcase_16 AC 0 ms
6,944 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 12 ms
6,944 KB
testcase_19 AC 4 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 0 ms
6,944 KB
testcase_22 AC 24 ms
6,944 KB
testcase_23 AC 467 ms
6,944 KB
testcase_24 AC 434 ms
8,028 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 1,153 ms
11,264 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:21:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   21 |         scanf("%d", &n);
      |         ~~~~~^~~~~~~~~~
main.cpp:22:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   22 |         INC0(i, n) { scanf("%d", &s[i]); }
      |                      ~~~~~^~~~~~~~~~~~~
main.cpp:23:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   23 |         scanf("%d", &m);
      |         ~~~~~^~~~~~~~~~
main.cpp:24:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   24 |         INC0(i, m) { scanf("%d%d%d", &a[i], &b[i], &c[i]); }
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <cstdio>

#define FOR(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define REV(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define INC0(i, n) FOR(i, 0, n)
#define INC1(i, n) FOR(i, 1, (n) + 1)
#define DEC0(i, n) REV(i, 0, n)
#define DEC1(i, n) REV(i, 1, (n) + 1)

typedef long long   signed int LL;
typedef long long unsigned int LU;


int n, s[50], m, a[1225], b[1225], c[1225];
struct DP { int v, s1, s2; };
DP queue[50 * 50 * 50 * 50]; // テキトー
int get, set;
int dp[50][50][50];

int main() {
	scanf("%d", &n);
	INC0(i, n) { scanf("%d", &s[i]); }
	scanf("%d", &m);
	INC0(i, m) { scanf("%d%d%d", &a[i], &b[i], &c[i]); }
	
	dp[0][0][0] = 1; // +1
	
	DP temp = {0, 0, 0};
	queue[set++] = temp;
	while(get < set) {
		DP q = queue[get++];
		int v = q.v;
		INC0(i, m) {
			if(a[i] == v || b[i] == v) {
				int w;
				if(a[i] == v) { w = b[i]; }
				else          { w = a[i]; }
				
				int  x = dp[v][q.s1][q.s2] + c[i];
				int& y = dp[w][q.s1][q.s2];
				if(y == 0 || x < y) {
					y = x;
					DP temp = {w, q.s1, q.s2};
					queue[set++] = temp;
				}
				
				if(v != 0 && v != n - 1 && q.s1 == 0) {
					int  x = dp[v][0][0] + s[v] + c[i];
					int& y = dp[w][v][0];
					if(y == 0 || x < y) {
						y = x;
						DP temp = {w, v, 0};
						queue[set++] = temp;
					}
				}
				
				if(v != 0 && v != n - 1 && v != q.s1 && q.s1 != 0 && q.s2 == 0) {
					int  x = dp[v][q.s1][0] + s[v] + c[i];
					int& y = dp[w][q.s1][v];
					if(y == 0 || x < y) {
						y = x;
						DP temp = {w, q.s1, v};
						queue[set++] = temp;
					}
				}
			}
		}
	}
	
	int min = 6000000;
	
	FOR(i, 1, n - 1) {
	FOR(j, 1, n - 1) {
		int x = dp[n - 1][i][j];
		if(i != j && x != 0 && x < min) { min = x; }
	}
	}
	
	printf("%d\n", min - 1); // -1
	
	return 0;
}
0