結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー 夜
提出日時 2020-12-17 16:51:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 631 ms / 2,000 ms
コード長 1,544 bytes
コンパイル時間 1,393 ms
コンパイル使用メモリ 104,096 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 06:59:57
合計ジャッジ時間 7,359 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 176 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 111 ms
4,348 KB
testcase_12 AC 11 ms
4,348 KB
testcase_13 AC 45 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 13 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 61 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 182 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 70 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 13 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 81 ms
4,348 KB
testcase_34 AC 37 ms
4,348 KB
testcase_35 AC 9 ms
4,348 KB
testcase_36 AC 5 ms
4,348 KB
testcase_37 AC 3 ms
4,348 KB
testcase_38 AC 17 ms
4,348 KB
testcase_39 AC 4 ms
4,348 KB
testcase_40 AC 1 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 5 ms
4,348 KB
testcase_44 AC 4 ms
4,348 KB
testcase_45 AC 139 ms
4,348 KB
testcase_46 AC 10 ms
4,348 KB
testcase_47 AC 32 ms
4,348 KB
testcase_48 AC 67 ms
4,348 KB
testcase_49 AC 8 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 13 ms
4,348 KB
testcase_53 AC 7 ms
4,348 KB
testcase_54 AC 434 ms
4,348 KB
testcase_55 AC 426 ms
4,348 KB
testcase_56 AC 423 ms
4,348 KB
testcase_57 AC 609 ms
4,348 KB
testcase_58 AC 631 ms
4,348 KB
testcase_59 AC 622 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
vector<vector<pair<int,long long>>> vec(2020);
int u[2020], v[2020];
long long w[2020];
long long mi[2020];
int main() {
	bool t;
	cin >> t;
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> u[i] >> v[i] >> w[i];
		if (!t) {
			vec[v[i]].emplace_back(make_pair(u[i], w[i]));
		}
		vec[u[i]].emplace_back(make_pair(v[i], w[i]));
	}
	long long ans = 10000000000000007;
	for (int i = 0; i < m; i++) {
		priority_queue<pair<long long, int>,vector<pair<long long,int>>,greater<pair<long long,int>>> que;
		for (int j = 1; j <= n; j++) {
			mi[j] = 10000000000000007;
		}
		que.push(make_pair(0, v[i]));
		mi[v[i]] = 0;
		while (!que.empty()) {
			long long x = que.top().first;
			int y = que.top().second;
			que.pop();
			for (int j = 0; j < vec[y].size(); j++) {
				if (y == v[i] && vec[y][j].first == u[i]) {
					continue;
				}
				if (y == u[i] && vec[y][j].first == v[i]) {
					continue;
				}
				if (mi[vec[y][j].first] > x + vec[y][j].second) {
					mi[vec[y][j].first] = x + vec[y][j].second;
					que.push(make_pair(x + vec[y][j].second, vec[y][j].first));
				}
			}
		}
		if (ans > w[i] + mi[u[i]]) {
			ans = w[i] + mi[u[i]];
		}
	}
	if (ans == 10000000000000007) {
		cout << -1 << endl;
		return 0;
	}
	cout << ans << endl;
}
0