版权声明:虽然本蒟蒻很菜,但各位dalao转载请注明出处谢谢。 https://blog.csdn.net/xuxiayang/article/details/86657053
给定任意时刻从任意点出发的代价,求从第1个点出发,第 个时刻到达第 个点的最小代价
经典的分层图最短路(自认为)
虽然 不知道有木有不卡,反正 +堆优化也不会打QWQ
#include<queue>
#include<cctype>
#include<cstdio>
#include<cstring>
#define id(i,j) ((i-1)*m+(j))
using namespace std;int n,m,l[20501],tot,t,x,y,z[20501],dis[20501],path[20501];
struct node{int next,to,w;}e[2005001];
bool vis[20501];
inline void add(register int u,register int v,register int w){e[++tot]=(node){l[u],v,w};l[u]=tot;return;}
typedef long long LL;
inline char Getchar()
{
static char buf[1000000],*p1=buf+1000000,*pend=buf+1000000;
if(p1==pend)
{
p1=buf; pend=buf+fread(buf,1,1000000,stdin);
if (pend==p1) return -1;
}
return *p1++;
}
inline LL read()
{
char c;int d=1;LL f=0;
while(c=Getchar(),!isdigit(c))if(c==45)d=-1;f=(f<<3)+(f<<1)+c-48;
while(c=Getchar(),isdigit(c)) f=(f<<3)+(f<<1)+c-48;
return d*f;
}
inline void spfa()
{
memset(dis,0x3f3f3f3f,sizeof(dis));
queue<int>q;dis[1]=0;vis[1]=true;q.push(1);
while(q.size())
{
int x=q.front();q.pop();vis[x]=false;
for(register int i=l[x];i;i=e[i].next)
{
int y=e[i].to,w=e[i].w;
if(dis[y]>dis[x]+w)
{
dis[y]=dis[x]+w;
path[y]=x;
if(!vis[y]) vis[y]=true,q.push(y);
}
}
}
return;
}
signed main()
{
freopen("lines.in","r",stdin);
freopen("lines.out","w",stdout);
n=read();m=read();
for(register int i=1;i<=n;i++)
for(register int k=1;k<=n;k++)
{
if(i==k) continue;
t=read();
for(int j=1;j<=m;j++)
{
if(j<=t) z[j]=read();else z[j]=z[(j-1)%t+1];
if(!z[j]) z[j]=0x3f3f3f3f;
add(id(i,j),id(k,j+1),z[j]);
}
}
spfa();
printf("%d",dis[id(n,m+1)]);
}