結果

問題 No.788 トラックの移動
ユーザー okok
提出日時 2019-02-09 00:08:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 484 ms / 2,000 ms
コード長 2,582 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 86,240 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 17:29:26
合計ジャッジ時間 4,174 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define EPS (1e-5)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX 3000

typedef long long ll;
typedef pair<ll,ll> P;

int N, M, L;
int t[MAX];

struct edge{ll to, cost;};

vector<edge> G[MAX];
ll d[MAX], V, dl[MAX];
int temp, temp2;

void dijkstra(ll s){
	priority_queue<P,vector<P>,greater<P>> q;
	fill(d,d+V, INF);
	
	d[s] = 0;
	q.push(P(0,s));
	
	while(!q.empty()){
		P p = q.top();q.pop();
		ll v = p.second;
		
		// cout<<p.first<<" ()() "<<p.second<<endl;
		// cout<<d[v]<<" < "<<p.first<<endl;
		
		if(d[v] < p.first) continue;
		
		for(edge e : G[v]){
			// cout<<v<<" e.to "<<e.to<<endl;
			if(d[e.to] > d[v] + e.cost){
				d[e.to] = d[v] + e.cost;
				
				q.push(P(d[e.to],e.to));
			}
		}
	}
	
	
}

void dijkstraL(){
	priority_queue<P,vector<P>,greater<P>> q;
	fill(dl,dl+V, INF);
	
	ll s = L;
	
	dl[s] = 0;
	q.push(P(0,s));
	
	while(!q.empty()){
		P p = q.top();q.pop();
		ll v = p.second;
		
		if(dl[v] < p.first) continue;
		
		for(edge e : G[v]){
			if(dl[e.to] > dl[v] + e.cost){
				dl[e.to] = dl[v] + e.cost;
				q.push(P(dl[e.to],e.to));
			}
		}
	}
	
	
}

void bfs(int s){
	queue<int> Q;
	int used[MAX] = {};
	
	Q.push(s);
	
	while(!Q.empty()){
		int v = Q.front(); Q.pop();
		
		
		for(edge e: G[v]){
			if(used[e.to]) continue;
			temp += d[e.to]*t[e.to]*2;
			// cout<<temp<<" [][] "<<t[e.to]<<" "<<e.to<<endl;
			if(t[e.to]){
				temp2 = min(temp2,-dl[s]-d[e.to]+dl[e.to]);
				// cout<<s<<" "<<e.to<<" "<<dl[s]<<" "<<d[e.to]<<" "<<dl[e.to]<<endl;
			}
			used[e.to] = true;
			Q.push(e.to);
		}
	}
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int ans = INF;
	int a, b, c, count = 0;
	edge e;
	
	
	cin>>N>>M>>L;
	
	L--;
	
	V = N;
	
	for(int i = 0; i < N; i++){
		cin>>t[i];
		if(t[i]) count++;
	}
	
	
	
	for(int i = 0; i < M; i++){
		cin>>a>>b>>c;
		a--, b--;
		e.to = b, e.cost = c;
		G[a].push_back(e);
		e.to = a;
		G[b].push_back(e);
	}
	
	if(count <= 1){
		cout<<0<<endl;
		return 0;
	}
	
	dijkstraL();
	
	for(int i = 0; i < N; i++){
		// cout<<"<><> "<<i<<endl;
		temp = 0;
		temp2 = 0;
		dijkstra(i);
		bfs(i);
		// cout<<i<<" "<<temp<<" "<<temp2<<" "<<dl[i]<<endl;
		// cout<<temp+dl[i]+temp2<<" ans"<<endl;
		ans = min(temp+dl[i]+temp2,ans);
	}
	
	
	cout<<ans<<endl;
	
	return 0;
}
0