思路:最短路问题 以每个人为点,替换的费用为边建图 求酋长到每个人的最小费用,但有一个等级的限制
在任何一条路径上,任何两点的等级之差都必须在限制之内。这里需要重构图,枚举每一个等级范围,在此条件下新建一个图 从题意中我们知道,最后所有的最短路都会汇集在1号点,也就是说1号点是所有最短路都存在的点,这个条件很重要,这样我们就可以依照1号点来给定区间,比如1号点等级为stp,那么也就是说在所有最短路的这些点都必须满足在[lev-i,lev+M-i]这个区间里面。如果在这个区间内出现的两个点的他们之间的等级差不会超过了M值。
//196K
0MS
#include <stdio.h>
#include <string.h>
#define VM 110
#define EM 10005
const int inf = 0x3f3f3f3f;
struct E
{
int to,w,nxt;
}orig[EM],edge[EM]; //orig是源图,edge是新图
struct point
{
int cost,stp;
}node[VM];
//每种物品的花费和等级
int head1[VM],head[VM],e1,e,M,N,ans;
void addedge1 (int cu,int cv,int cw)
{
orig[e1].to = cv;
orig[e1].w = cw;
orig[e1].nxt = head1[cu];
head1[cu] = e1 ++;
}
void addedge (int cu,int cv,int cw)
{
edge[e].to = cv;
edge[e].w = cw;
edge[e].nxt = head[cu];
head[cu] = e ++;
}
int min (int a,int b)
{
return a > b ? b : a;
}
void Build (int a,int b) //重构图 等级在[a,b]内的边
{
int u,v;
for (u = 1;u <= N;u ++)
{
if (node[u].stp <= b&&node[u].stp >= a)
for (int i = head1[u];i != -1;i = orig[i].nxt)
{
v = orig[i].to;
if (node[v].stp <= b&&node[v].stp >= a)
addedge(u,v,orig[i].w);
}
}
}
void Spfa ()
//spfa求最短路
{
int dist[VM],vis[VM],que[VM+10];
int i,u,v,front = 0,rear = 0;
memset (dist,0x3f,sizeof(dist));
memset (vis,0,sizeof(vis));
dist[1] = 0,vis[1] = 1;
que[rear++] = 1;
while (front != rear)
{
u = que[front ++];
front = front % VM;
vis[u] = 0;
for (i = head[u];i != -1;i = edge[i].nxt)
{
v = edge[i].to;
if (dist[v] > dist[u] + edge[i].w)
{
dist[v] = dist[u] + edge[i].w;
if (!vis[v])
{
que[rear++] = v;
rear = rear % VM;
vis[v] = 1;
}
}
}
}
for (i = 1;i <= N;i ++)
//ans取所有中的最小
ans = min (ans,dist[i]+node[i].cost);//得加上i这个物品本身所需的费用
}
int main ()
{
int u,v,w,m;
while (~scanf ("%d%d",&M,&N))
{
memset (head1,-1,sizeof(head1));
e1 = 0;
for (u = 1;u <= N;u ++)
{
scanf ("%d%d%d",&node[u].cost,&node[u].stp,&m);
while (m --)
{
scanf ("%d%d",&v,&w);
addedge1 (u,v,w);
}
}
ans = inf;
for (int i = 0;i <= M;i ++) //枚举
{
memset (head,-1,sizeof(head));
e = 0;
Build (node[1].stp-i,M+node[1].stp - i);
Spfa();
}
printf ("%d\n",ans);
}
return 0;
}
//196K
#include <stdio.h>
#include <string.h>
#define VM 110
#define EM 10005
const int inf = 0x3f3f3f3f;
struct E
{
}orig[EM],edge[EM];
struct point
{
}node[VM];
int head1[VM],head[VM],e1,e,M,N,ans;
void addedge1 (int cu,int cv,int cw)
{
}
void addedge (int cu,int cv,int cw)
{
}
int min (int a,int b)
{
}
void Build (int a,int b) //重构图 等级在[a,b]内的边
{
}
void Spfa ()
{
}
int main ()
{
}