結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー hirokazu1020
提出日時 2020-12-17 00:42:11
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,596 bytes
コンパイル時間 2,809 ms
コンパイル使用メモリ 111,344 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-20 05:37:25
合計ジャッジ時間 6,923 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41 WA * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<unordered_map>
#include<random>
#include<array>
#include<cassert>
using namespace std;
#define INF ((1<<30)-1)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()






int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int t;
	int n, m;
	cin >> t >> n >> m;
	vector<vector<pair<int, int>>> edge(n, vector<pair<int, int>>());
	rep(_, m) {
		int u, v, w;
		cin >> u >> v >> w;
		u--; v--;
		edge[u].emplace_back(v, w);
		if (t == 0)edge[v].emplace_back(u, w);
	}
	rep(i, n) {
		sort(all(edge[i]));
	}

	long long ans = 1LL << 60;
	rep(i, n) {
		vector<long long> d(n, 1LL << 60);
		d[i] = 0;
		priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
		pq.emplace(0, i);

		while (!pq.empty()) {
			auto pa = pq.top();
			pq.pop();
			if (d[pa.second] < pa.first)continue;

			for (auto e : edge[pa.second]) {
				int v = e.first;
				int w = e.second;
				if (pa.first + w < d[v]) {
					d[v] = pa.first + w;
					pq.emplace(d[v], v);
				}
			}
		}
		rep(j, n) {
			for (auto e: edge[j]) {
				if (e.first == i && d[j] != e.second) {
					ans = min(ans, d[j] + e.second);
				}
			}
		}
	}

	if (ans == (1LL << 60))ans = -1;
	cout << ans << endl;


	return 0;
}
0