CodeForces-1915G
Chthohepta · · 个人记录
题意
给定
住在城市
从
当 Slavic 骑上编号为
求 Slavic 骑车从城市
思路
dijkstra,分层图最短路板子。
设
代码
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
const int N = 1e3 + 2;
int t, n, m;
int u, v;
ll w, s[N];
struct E{
int v, w;
};
vector <E> e[N];
ll dis[N][N], ans;
struct dijkstra{
int u, dan_che;
ll hao_shi;
bool operator < (const dijkstra &x) const{
return hao_shi > x.hao_shi;
}
};
priority_queue <dijkstra> q;
void dij(){
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
dis[i][j] = 1e18;
}
}
dis[1][1] = 0;
q.push({1, 1, 0});
while(!q.empty()){
u = q.top().u;
int dc = q.top().dan_che;
q.pop();
for(int i = 0; i < e[u].size(); i ++){
v = e[u][i].v, w = e[u][i].w;
int ldc = (s[dc] < s[v] ? dc : v);
if(dis[v][ldc] > dis[u][dc] + s[dc] * w){
dis[v][ldc] = dis[u][dc] + s[dc] * w;
q.push({v, ldc, dis[v][ldc]});
}
}
}
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> t;
while(t --){
cin >> n >> m;
for(int i = 1; i <= n; i ++){
e[i].clear();
}
for(int i = 1; i <= m; i ++){
cin >> u >> v >> w;
e[u].push_back({v, w});
e[v].push_back({u, w});
}
for(int i = 1; i <= n; i ++){
cin >> s[i];
}
dij();
ans = 1e18;
for(int i = 1; i <= n; i ++){
ans = min(ans, dis[n][i]);
}
cout << ans << '\n';
}
return 0;
}