This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <queue>
#pragma GCC optimize("Ofast")
#define ll long long
#define eps 1e-7
#define all(x) ((x).begin()),((x).end())
#define usecppio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
using pii = pair<int, int>;
const ll INF = 987654321987654321;
const int MX = 2625;
struct Edge {
int src, dst, w;
bool operator<(const Edge &p) const {
return w > p.w;
}
};
int fuel[MX];
ll cost[MX * MX];
vector <Edge> G[MX];
void dijkstra(ll dist[], int start, vector<Edge> graph[])
{
fill(dist,dist+MX*MX,INF);
dist[start*2501+fuel[start]] = 0;
priority_queue<Edge> pq;
pq.push({0, start,0});
while(!pq.empty()) {
Edge x = pq.top();
pq.pop();
//printf("Process edge %lld -> %lld (len=%lld)\n",x.src, x.dst, x.w);
for (auto ed : graph[x.dst]) {
bool flag = false;
int max_j = min(2500, fuel[ed.src]);
for (int j = 1; j <= max_j; j++) {
int fcost = min(fuel[ed.dst], j);
int u = ed.src*2501+j;
int v = ed.dst*2501+fcost;
int w = ed.w * j;
if (dist[v] > dist[u] + w) {
flag = true;
dist[v] = dist[u] + w;
}
}
if (flag) pq.push(ed);
}
}
}
int32_t main() {
usecppio
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> fuel[i];
for (int i = 0; i < m; i++) {
int u, v, w; cin >> u >> v >> w;
G[u].push_back({u, v, w});
G[v].push_back({v, u, w});
}
dijkstra(cost, 1, G);
ll ans = INF;
for (int j = 1; j <= 2500; j++) ans = min(ans, cost[n*2501+j]);
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |