結果

問題 No.788 トラックの移動
ユーザー たぴちゃんたぴちゃん
提出日時 2019-02-08 22:59:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 439 ms / 2,000 ms
コード長 1,455 bytes
コンパイル時間 2,064 ms
コンパイル使用メモリ 207,928 KB
実行使用メモリ 34,796 KB
最終ジャッジ日時 2023-08-21 17:27:51
合計ジャッジ時間 5,094 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 407 ms
34,740 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 97 ms
19,404 KB
testcase_05 AC 394 ms
34,748 KB
testcase_06 AC 407 ms
34,736 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,504 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 116 ms
34,744 KB
testcase_16 AC 439 ms
34,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
const int INF = 1e18;

int n, m, l;
int t[2000];
vector<P> G[2000];
int dist[2000][2000];

struct Dijkstra{
	vector<int> d;
	Dijkstra(int V){
		d.resize(V, INF);
	}
	void calc(int s){
		d[s] = 0;
		priority_queue<P, vector<P>, greater<P> > q;
		q.push(P(d[s], s));
		while(!q.empty()){
			P p = q.top(); q.pop();
			int from = p.second;
			int cost = p.first;
			if(d[from] < cost) continue;
			for(auto e : G[from]){
				int next = e.first;
				int newCost = cost + e.second;
				if(d[next] > newCost){
					d[next] = newCost;
					q.push(P(newCost, next));
				}
			}
		}
	}
};

signed main(){
	cin >> n >> m >> l;
	l--;
	for(int i = 0; i < n; i++) cin >> t[i];
	for(int i = 0; i < m; i++){
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		G[a].push_back({b, c});
		G[b].push_back({a, c});
	}
	int cnt = 0;
	for(int i = 0; i < n; i++) if(t[i]) cnt++;
	if(cnt == 1){
		cout << 0 << endl;
		return 0;
	}
	for(int i = 0; i < n; i++){
		Dijkstra dk(n);
		dk.calc(i);
		for(int j = 0; j < n; j++){
			dist[i][j] = dk.d[j];
		}
	}
	int ans = INF;
	for(int i = 0; i < n; i++){
		int tmp = dist[l][i];
		for(int j = 0; j < n; j++){
			tmp += t[j] * dist[i][j] * 2;
		}
		int MAX = 0;
		for(int j = 0; j < n; j++){
			if(t[j] == 0) continue;
			MAX = max(MAX, dist[l][i] + dist[i][j] - dist[l][j]);
		}
		tmp -= MAX;
		ans = min(ans, tmp);
	}
	cout << ans << endl;
}
0