結果

問題 No.788 トラックの移動
ユーザー 👑 jupirojupiro
提出日時 2020-01-27 18:32:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 521 ms / 2,000 ms
コード長 2,311 bytes
コンパイル時間 1,232 ms
コンパイル使用メモリ 129,704 KB
実行使用メモリ 34,532 KB
最終ジャッジ日時 2023-08-21 17:34:05
合計ジャッジ時間 4,807 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 496 ms
34,528 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 110 ms
10,968 KB
testcase_05 AC 488 ms
34,532 KB
testcase_06 AC 499 ms
34,452 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 174 ms
34,512 KB
testcase_16 AC 521 ms
34,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

void dijkstra(ll start, vector<vector<pair<ll, ll>>>& graph, vector<ll>& dist) {
	dist[start] = 0;
	priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
	vector<bool> used(dist.size(), false);
	pq.push(make_pair(0, start));
	while (!pq.empty()) {
		ll d, node;
		tie(d, node) = pq.top();
		pq.pop();
		if (used[node]) continue;
		used[node] = true;
		for (pair<ll, ll> element : graph[node]) {
			ll new_d, new_node;
			tie(new_node, new_d) = element;
			new_d += d;
			if (new_d < dist[new_node]) {
				dist[new_node] = new_d;
				pq.push(make_pair(dist[new_node], new_node));
			}
		}
	}
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	ll N, M, L; scanf("%lld %lld %lld", &N, &M, &L); L--;
	ll cnt = 0;
	vector<ll> t(N); for (ll i = 0; i < N; i++) {
		scanf("%lld", &t[i]); if (t[i] > 0) cnt++;
	}
	if (cnt == 1) {
		puts("0");
		return 0;
	}
	vector<vector<pair<ll, ll>>> g(N);
	for (ll i = 0; i < M; i++) {
		ll a, b, c; scanf("%lld %lld %lld", &a, &b, &c);
		a--; b--;
		g[a].emplace_back(b, c);
		g[b].emplace_back(a, c);
	}
	vector<vector<ll>> d(N, vector<ll>(N, INF));
	for (ll i = 0; i < N; i++) {
		dijkstra(i, g, d[i]);
	}
	ll res = INF;
	for (ll i = 0; i < N; i++) {
		ll sum = 0;
		for (ll j = 0; j < N; j++) {
			sum += t[j] * d[i][j] * 2;
		}
		for (ll j = 0; j < N; j++) {
			if (t[j] == 0) continue;
			chmin(res, sum + d[L][j] + d[j][i] - d[i][j] * 2);
		}
		
	}
	cout << res << endl;
	return 0;
}
0