結果
| 問題 |
No.17 2つの地点に泊まりたい
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-03-10 03:32:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 171 ms / 5,000 ms |
| コード長 | 2,087 bytes |
| コンパイル時間 | 2,932 ms |
| コンパイル使用メモリ | 202,092 KB |
| 最終ジャッジ日時 | 2025-01-19 13:10:46 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int S[50];
int dist[50][50][50];
vector <pair<int,int>> adj[50];
int n,m,a,b,c;
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
for(int i=0;i<n;i++)
{
cin >> S[i];
}
cin >> m;
for(int i=0;i<m;i++)
{
cin >> a >> b >> c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
dist[i][j][k] = 1e9;
}
}
}
dist[0][0][0] = 0;
priority_queue <pair<int,pair<int,pair<int,int>>>,vector<pair<int,pair<int,pair<int,int>>>>,greater<pair<int,pair<int,pair<int,int>>>>> pque;
pque.push(make_pair(0,make_pair(0,make_pair(0,0))));
while(!pque.empty())
{
int now = pque.top().second.first;
int t1 = pque.top().second.second.first;
int t2 = pque.top().second.second.second;
if(dist[now][t1][t2] < pque.top().first)
{
pque.pop();
continue;
}
pque.pop();
//cout << now << ' ' << t1 << ' ' << t2 << '\n';
//cout << now << ' ' << cnt << ' ' << dist[now][cnt] << ' ' << S[now] << '\n';
for(auto it : adj[now])
{
int next = it.first;
int cost = it.second;
if(dist[next][t1][t2] > dist[now][t1][t2] + cost)
{
dist[next][t1][t2] = dist[now][t1][t2] + cost;
pque.push(make_pair(dist[next][t1][t2],make_pair(next,make_pair(t1,t2))));
}
if(now!=0 && now!=n-1)
{
if(t1==0)
{
if(dist[next][now][t2] > dist[now][t1][t2] + cost + S[now])
{
dist[next][now][t2] = dist[now][t1][t2] + cost + S[now];
pque.push(make_pair(dist[next][now][t2],make_pair(next,make_pair(now,t2))));
}
}
else if(t2==0)
{
if(t1!=now)
{
if(dist[next][t1][now] > dist[now][t1][t2] + cost + S[now])
{
dist[next][t1][now] = dist[now][t1][t2] + cost + S[now];
pque.push(make_pair(dist[next][t1][now],make_pair(next,make_pair(t1,now))));
}
}
}
}
}
}
int res = 1e9;
for(int i=1;i<n-1;i++)
{
for(int j=1;j<n-1;j++)
{
res = min(res,dist[n-1][i][j]);
}
}
cout << res << '\n';
return 0;
}