結果
| 問題 |
No.788 トラックの移動
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-02-09 00:08:28 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 450 ms / 2,000 ms |
| コード長 | 2,582 bytes |
| コンパイル時間 | 930 ms |
| コンパイル使用メモリ | 87,180 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-12-15 07:02:06 |
| 合計ジャッジ時間 | 4,195 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 14 |
ソースコード
#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;
}