CF1915G 题解
思路
这道题中,原本是最短路的板子题,但因为有 的变化,所以我们不能直接普通使用最短路。然后你会发现 和 都很小,只有 ,因此我们可以把 ,其中 表示在第 个点且当前速度为 时最小的距离。然后 的话肯定是贪心取,取一路过来速度最快的自行车就行。
代码
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int t, n, m, a[1005], x, y, z;
ll d[1005][1005];
vector <pii> v[1005];
struct node {
ll dis;
int u, minx;
bool operator < (const node& cmp) const {
return dis > cmp.dis;
}
} ;
priority_queue <node> q;
ll dijkstra () {
for (int i = 1; i <= n; ++ i)
memset (d[i], 0x3f, sizeof d[i]);
d[1][a[1]] = 0;
q.push ({0, 1, a[1]});
while (! q.empty ()) {
const node t = q.top ();
q.pop ();
const int u = t.u, s = t.minx;
if (t.dis > d[u][s])
continue ;
for (pii i : v[u]) {
const int v = i.first, x = min (s, a[v]);
if (t.dis + i.second * s < d[v][x])
q.push ({d[v][x] = t.dis + i.second * s, v, x});
}
}
ll minx = 1e18;
for (int i = 0; i < 1003; ++ i)
minx = min (minx, d[n][i]);
return minx;
}
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)
v[i].clear ();
while (m --) {
cin >> x >> y >> z;
v[x].push_back ({y, z});
v[y].push_back ({x, z});
}
for (int i = 1; i <= n; ++ i)
cin >> a[i];
cout << dijkstra () << '\n';
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端