第 35 次 CSP 认证 D 题,求助各位,只过了三十分
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;
const int N = 10010, M = N << 1;
int n, m;
LL xx[N], yy[N];
LL x[N], y[N], r[N], t[M];
LL e[M], h[M], ne[M], w[M], idx;
int dist[M];
bool st[M];
//vector<LL> fx[N];
//int fx[N];
void add(LL a, LL b, LL c)
{
e[idx] = b; w[idx] = c; ne[idx] = h[a]; h[a] = idx ++;
}
LL get_dist(LL x1, LL y1, LL x2, LL y2)
{
return max(abs(x1 - x2), abs(y1 - y2));
}
int dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII> > heap; // 堆优化
heap.push({0, 1});
while (heap.size())
{
auto t = heap.top();
heap.pop();
int distance = t.first, ver = t.second;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
if (dist[j] > w[i] + dist[ver])
{
dist[j] = w[i] + dist[ver];
heap.push({dist[j], j});
}
}
}
if(dist[n] == 0x3f3f3f3f) return -1;
else return dist[n];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i ++) cin >> xx[i] >> yy[i];
for (int i = 1; i <= m; i ++) cin >> x[i] >> y[i] >> r[i] >> t[i];
for (int j = 1; j <= m; j ++)
{
for (int i = 1; i <= n; i ++)
{
if (get_dist(xx[i], yy[i], x[j], y[j]) <= r[j])
{
add(i, j + n, t[j]); add(j + n, i, 0);
}
}
}
LL res = -1;
res = dijkstra();
if (res == -1) cout << "Nan" << endl;
else cout << res << endl;
return 0;
}