题解:AT_abc463_e [ABC463E] Roads and Gates
分析情况
依据题目的意思,我们有两种方式从
-
如果存在边
e_i 连接u 与v 两点,我们可以以题目已给出的边权T_i 的代价,从u 移动到v 。 -
对于任意的两点
u 与v ,可以以X_u + X_v + Y 的代价直接移动。
对于方式
考虑虚点
由上图可观察到,对于新建的边的边权中重复出现了
但是此时还缺少
我们可以验证这个思路:从
具体实现
首先我们正常建图,然后我们创立一个虚点
特别注意,Dijkstra 中一定要包括虚点在内!同时,一定要注意数据类型和精度!
代码实现
#include <iostream>
#include <vector>
#include <queue>
const int MAXN = 2e5 + 5;
const long double INF = 4e18L;
struct Edge
{
int v;
long double w;
};
struct Node
{
int u;
long double dis;
bool operator>(const Node &a) const
{
return dis > a.dis;
}
};
int n, m;
long double Y, X[MAXN], dis[MAXN];
bool vis[MAXN];
std::vector<Edge> e[MAXN];
std::priority_queue<Node, std::vector<Node>, std::greater<Node>> pq;
void Dijkstra(int s)
{
for (int i = 1; i <= n + 1; i++) // 注意这里包括了虚点
{
dis[i] = INF;
vis[i] = false;
}
dis[s] = 0;
pq.push({s, 0});
while (!pq.empty())
{
int u = pq.top().u;
pq.pop();
if (vis[u])
continue;
vis[u] = true;
for (auto &edge : e[u])
{
int v = edge.v;
long double w = edge.w;
if (!vis[v] && dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
pq.push({v, dis[v]});
}
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> m >> Y;
for (int i = 1; i <= m; i++)
{
int u, v;
long double w;
std::cin >> u >> v >> w;
e[u].push_back({v, w});
e[v].push_back({u, w});
}
for (int i = 1; i <= n; i++)
std::cin >> X[i];
for (int i = 1; i <= n; i++)
{
e[i].push_back({n + 1, X[i] + Y / 2.0});
e[n + 1].push_back({i, X[i] + Y / 2.0});
}
Dijkstra(1);
for (int i = 2; i <= n; i++)
std::cout << (long long)dis[i] << (i == n ? '\n' : ' ');
return 0;
}