結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー
提出日時 2020-12-17 16:51:58
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 646 ms / 2,000 ms
コード長 1,544 bytes
コンパイル時間 925 ms
コンパイル使用メモリ 104,012 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-21 08:08:41
合計ジャッジ時間 6,724 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

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