分析:
因为有等级限制,所以枚举等级范围,再用最短路判断最小金币即可
可以根据酋长的等级来缩小枚举的范围
#include <cstring>
#include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e9
#define maxn 50001
int n , m ;
struct node
{
int dec;
int nxt;
int val;
};
int cnt;
node data[maxn];
int state[maxn];
int head[maxn];
int d[maxn];
int vis[maxn];
void addege(int u,int v,int w)
{
data[cnt].dec = v;
data[cnt].val = w;
data[cnt].nxt = head[u];
head[u] = cnt++;
}
void spfa(int x)
{
memset(vis,0,sizeof(vis));
for(int i = 0 ; i < maxn ; ++i)d[i] = INF;
queue<int>Q;
Q.push(0);
vis[0] = 1;
d[0] = 0;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = 0;
for(int i = head[u] ; ~i ; i = data[i].nxt)
{
int v = data[i].dec;
if( state[v] > n + x || state[v] < x )continue;
if( d[v] > d[u] + data[i].val )
{
d[v] = d[u] + data[i].val;
if(!vis[v])
{
vis[v] = 1;
Q.push(v);
}
}
}
}
}
int main()
{
cin >> n >> m;
cnt = 0;
for(int i = 0 ; i < maxn ; ++i)head[i] = -1;
for(int i = 1 ; i <= m ; ++i)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
state[i] = b;
addege( 0 , i , a );
while( c-- )
{
int x,y;
scanf("%d %d",&x,&y);
addege( x , i , y );
}
}
int ans = INF ;
for(int i = state[1] - n ; i <= state[1] ; ++i){
spfa(i);
ans = min(ans,d[1]);
}
cout << ans << endl;
}