二维dijkstra,
d
i
s
[
i
]
[
j
]
dis[i][j]
dis[i][j] 代表从起点出发,经过
j
j
j 长度的路程,到达
i
i
i 点的最小价值。写法就和dijkstra模板差不多了。
对于
p
r
i
o
r
i
t
y
q
u
e
u
e
priority_queue
priorityqueue 则重载一个结构体放进去,维护
C
O
2
CO_2
CO2 的排放、路程的长度已经当前节点。
struct node
{
int c, d, u;
bool operator < (const node &a) const
{
if(c == a.c) return d > a.d; // '>' 是最小值在上面,不要搞反
return c > a.c;
}
};
priority_queue<node>q;
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define int long long
const LL Max = 0x3f3f3f3f3f3f3f3f;
const LL Min = -0x3f3f3f3f3f3f3f3f;
const int N = 1e3 + 5;
typedef pair<int, int> PII;
int T, n, m, mxd, ans = Max;
int x[N], y[N], w[N];
int co[N][N];
bool st[N][N];
vector<PII> v[N];
struct node
{
int c, d, u;
bool operator < (const node &a) const
{
if(c == a.c) return d > a.d;
return c > a.c;
}
};
priority_queue<node>q;
int get(int i, int j)
{
double d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
int x = d;
if (x == d)
return x;
return x + 1;
}
void dijkstra()
{
co[n][0] = 0;
q.push({0, 0, n});
while(q.size())
{
int t1 = q.top().c, t2 = q.top().d, t3 = q.top().u;
// cout << t1 << " " << t2 << " " << t3 << '\n';
q.pop();
if(st[t3][t2]) continue;
st[t3][t2] = true;
for(int i = 0; i < v[t3].size(); i ++)
{
int j = v[t3][i].first, mj = v[t3][i].second;
int dd = get(t3, j), ww = dd * w[mj];
if(dd + t2 > mxd) continue;
if(co[j][dd + t2] > co[t3][t2] + ww)
{
co[j][dd + t2] = co[t3][t2] + ww;
q.push({co[j][dd + t2], dd + t2, j});
}
}
}
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int s1, s2, t1, t2;
cin >> s1 >> s2 >> t1 >> t2;
cin >> mxd;
cin >> w[0];
cin >> m;
for (int i = 1; i <= m; i++)
{
cin >> w[i];
}
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> x[i] >> y[i];
int k;
cin >> k;
while (k--)
{
int j, mj;
cin >> j >> mj;
v[i].push_back({j, mj});
v[j].push_back({i, mj});
}
v[n].push_back({i, 0});
v[i].push_back({n + 1, 0});
}
v[n].push_back({n+1, 0});
x[n] = s1, y[n] = s2, x[n + 1] = t1, y[n + 1] = t2;
if(get(n, n+1) > mxd)
{
cout << "-1\n";
return 0;
}
memset(co, 0x3f, sizeof co);
dijkstra();
for(int i = 0; i <= mxd; i ++)
{
ans = min(ans, co[n+1][i]);
}
cout << ans << '\n';
return 0;
}