首先建图,起始点为0点,终止点为1,金币数为边权值,如果有依赖关系则加一条边。
然后枚举最短路上等级最高点,每次做最短路时需要满足level小于这个最高点,并且和其
previous点等级差不超过m,最短路我使用了spfa.
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <climits>
#define oo 0x7f7f7f7f
using namespace std;
const int MAXN = 110;
int m,n;
int map[MAXN][MAXN];
int level[MAXN];
int dis[MAXN];
bool visited[MAXN];
int ans;
void spfa()
{
for(int k=1;k<=n;k++)
{
int maxt = level[k];
memset(dis,127,sizeof(dis));
memset(visited,false,sizeof(visited));
dis[0] = 0;
queue <int> q;
q.push(0);
visited[0] = true;
while(!q.empty())
{
int u = q.front();q.pop();
visited[u] = false;
for(int i=1;i<=n;i++)
{
if(map[u][i]!=-1&&maxt-level[i]<=m&&level[i]<=maxt)
{
if(dis[i]>dis[u]+map[u][i])
{
dis[i] = dis[u] + map[u][i];
if(!visited[i])
{
q.push(i);
visited[i] = true;
}
}
}
}
}
if(dis[1]<oo&&dis[1]<ans)
{
ans = dis[1];
}
}
}
int main()
{
memset(map,255,sizeof(map));
int st = 0;
scanf("%d%d",&m,&n);
for(int i=1;i<=n;i++)
{
int num;
scanf("%d%d%d",&map[st][i],level+i,&num);
for(int j=0;j<num;j++)
{
int t,v;
scanf("%d%d",&v,&t);
map[v][i] = t;
}
}
ans = INT_MAX;
spfa();
printf("%d\n",ans);
return 0;
}
#include <cstdio>
#include <cstring>
#include <queue>
#include <climits>
#define oo 0x7f7f7f7f
using namespace std;
const int MAXN = 110;
int m,n;
int map[MAXN][MAXN];
int level[MAXN];
int dis[MAXN];
bool visited[MAXN];
int ans;
void spfa()
{
for(int k=1;k<=n;k++)
{
int maxt = level[k];
memset(dis,127,sizeof(dis));
memset(visited,false,sizeof(visited));
dis[0] = 0;
queue <int> q;
q.push(0);
visited[0] = true;
while(!q.empty())
{
int u = q.front();q.pop();
visited[u] = false;
for(int i=1;i<=n;i++)
{
if(map[u][i]!=-1&&maxt-level[i]<=m&&level[i]<=maxt)
{
if(dis[i]>dis[u]+map[u][i])
{
dis[i] = dis[u] + map[u][i];
if(!visited[i])
{
q.push(i);
visited[i] = true;
}
}
}
}
}
if(dis[1]<oo&&dis[1]<ans)
{
ans = dis[1];
}
}
}
int main()
{
memset(map,255,sizeof(map));
int st = 0;
scanf("%d%d",&m,&n);
for(int i=1;i<=n;i++)
{
int num;
scanf("%d%d%d",&map[st][i],level+i,&num);
for(int j=0;j<num;j++)
{
int t,v;
scanf("%d%d",&v,&t);
map[v][i] = t;
}
}
ans = INT_MAX;
spfa();
printf("%d\n",ans);
return 0;
}