思路:构造最短路模型,抽象出来一个源点,这个源点到第i个点的费用就是price[i],然后就能抽象出图来,终点是1.
任意两个人之间都有等级限制,就枚举所有最低等级限制,然后将不再区间[min_lev, min_lev+m]区间的点都删除,就能进行Dijkstra算法了。
AC代码
#include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <utility> #include <string> #include <iostream> #include <map> #include <set> #include <vector> #include <queue> #include <stack> using namespace std; #define eps 1e-10 #define inf 0x3f3f3f3f #define PI pair<int, int> typedef long long LL; const int maxn = 100+5; int price[maxn], lev[maxn]; int cost[maxn][maxn], vis[maxn], d[maxn]; int n, m; int Dijkstra() { memset(d, inf, sizeof(d)); d[0] = 0; for(int i = 0; i <= n; ++i) { int x = 0, tmp = inf; for(int y = 0; y <= n; ++y) if(!vis[y] && d[y] <= tmp) tmp = d[x=y]; if(vis[x]) continue; vis[x] = 1; for(int y = 0; y <= n; ++y) if(!vis[y]) d[y] = min(d[y], d[x] + cost[x][y]); } return d[1]; } int main() { while(scanf("%d%d", &m, &n) == 2) { int r, x, c; memset(cost, inf, sizeof(cost)); for(int i = 1; i <= n; ++i) { scanf("%d%d%d", &price[i], &lev[i], &r); cost[0][i] = price[i]; //源点0到各点的费用 for(int j = 0; j < r; ++j) { scanf("%d%d", &x, &c); cost[x][i] = min(cost[x][i], c); } } int ans = inf; for(int i = 1; i <= n; ++i) { int min_lev = lev[i]; //最低等级 memset(vis, 0, sizeof(vis)); for(int j = 1; j <= n; ++j) { if(lev[j] < min_lev || lev[j] - min_lev > m) vis[j] = 1; } int dis = Dijkstra(); ans = min(ans, dis); } printf("%d\n", ans); } return 0; }
如有不当之处欢迎指出!