結果

問題 No.788 トラックの移動
ユーザー furafura
提出日時 2020-06-09 02:23:36
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 1,518 bytes
コンパイル時間 2,181 ms
コンパイル使用メモリ 211,920 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-21 17:35:38
合計ジャッジ時間 5,007 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 367 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 85 ms
4,380 KB
testcase_05 AC 356 ms
4,376 KB
testcase_06 AC 367 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 59 ms
4,376 KB
testcase_16 AC 384 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

template<class T> struct edge{
	int to;
	T wt;
	edge(int to,const T& wt):to(to),wt(wt){}
};
template<class T> using weighted_graph=vector<vector<edge<T>>>;

template<class T>
void add_undirected_edge(weighted_graph<T>& G,int u,int v,const T& wt){
	G[u].emplace_back(v,wt);
	G[v].emplace_back(u,wt);
}

template<class T>
void add_directed_edge(weighted_graph<T>& G,int u,int v,const T& wt){
	G[u].emplace_back(v,wt);
}

template<class T>
vector<T> Dijkstra(const weighted_graph<T>& G,int s){
	constexpr T INF=numeric_limits<T>::max();
	int n=G.size();
	vector<T> d(n,INF); d[s]=0;
	priority_queue<pair<T,int>> Q; Q.emplace(0,s);
	while(!Q.empty()){
		T d0=-Q.top().first;
		int u=Q.top().second; Q.pop();
		if(d0>d[u]) continue;
		for(const auto& e:G[u]){
			int v=e.to;
			if(d[v]>d[u]+e.wt) d[v]=d[u]+e.wt, Q.emplace(-d[v],v);
		}
	}
	return d;
}

const long long INF=1LL<<61;

int main(){
	int n,m,s; scanf("%d%d%d",&n,&m,&s), s--;
	vector<int> num(n);
	rep(u,n) scanf("%d",&num[u]);
	weighted_graph<lint> G(n);
	rep(i,m){
		int u,v,c; scanf("%d%d%d",&u,&v,&c); u--; v--;
		add_undirected_edge<lint>(G,u,v,c);
	}

	if(count(num.begin(),num.end(),0)==n-1) return puts("0"),0;

	auto d0=Dijkstra(G,s);

	lint ans=INF;
	rep(c,n){
		auto d=Dijkstra(G,c);
		lint sum=0;
		rep(u,n) sum+=2*num[u]*d[u];
		rep(u,n) if(num[u]>0) {
			ans=min(ans,d0[u]+d[u]+sum-2*d[u]);
		}
	}
	printf("%lld\n",ans);

	return 0;
}
0