思路:
最短路。枚举路径中节点的“等级”,合法才可以进行松弛操作。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <functional> 5 #include <queue> 6 using namespace std; 7 8 typedef pair<int, int> PP; 9 const int MAXN = 105, INF = 0x3f3f3f3f; 10 11 int M, N, P[MAXN], L[MAXN], X, dist[MAXN]; 12 struct edge 13 { 14 int to, cost; 15 }; 16 vector<edge> G[MAXN]; 17 18 int Dijkstra(int s, int l, int r) 19 { 20 fill(dist + 1, dist + N + 1, INF); 21 dist[s] = 0; 22 priority_queue<PP, vector<PP>, greater<PP> > q; 23 q.push(PP(0, s)); 24 while (!q.empty()) 25 { 26 PP tmp = q.top(); q.pop(); 27 int id = tmp.second; 28 if (tmp.first > dist[id]) continue; 29 for (unsigned int i = 0; i < G[id].size(); i++) 30 { 31 edge & e = G[id][i]; 32 if (dist[e.to] >= dist[id] + e.cost && L[e.to] >= l && L[e.to] <= r) 33 { 34 dist[e.to] = dist[id] + e.cost; 35 q.push(PP(dist[e.to], e.to)); 36 } 37 } 38 } 39 int minn = INF; 40 for (int i = 1; i <= N; i++) 41 minn = min(minn, dist[i] + P[i]); 42 return minn; 43 } 44 45 int main() 46 { 47 cin >> M >> N; 48 for (int i = 1; i <= N; i++) 49 { 50 cin >> P[i] >> L[i] >> X; 51 int id, cost; 52 for (int j = 0; j < X; j++) 53 { 54 cin >> id >> cost; 55 G[i].push_back(edge{id, cost}); 56 } 57 } 58 int l = L[1], ans = INF; 59 for (int i = max(l - M, 0); i <= l; i++) 60 { 61 int p = i, q = i + M; 62 ans = min(ans, Dijkstra(1, p, q)); 63 } 64 cout << ans << endl; 65 return 0; 66 }