K是1; 更新道路3->4使得从3到4的时间由100减少到0. 最新最短路经是1->3->4,总用时为1单位. N<=10000
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pi (4*atan(1.0))
const int N=1e5+10,M=4e6+10,inf=1e9+10;
const ll INF=1e18+10;
struct is
{
int v,w;
int next;
}edge[N*4];
int head[N*4],si;
int flag[N][23];
int d[N][23];
struct point
{
int pos,dis;
point(int a,int b){pos=b;dis=a;}
friend bool operator <(point x,point y)
{
return x.dis>y.dis;
}
};
void add(int u,int v,int w)
{
si++;
edge[si].v=v;
edge[si].w=w;
edge[si].next=head[u];
head[u]=si;
}
void dij(int n,int k)
{
priority_queue<point>q;
point a(0,1);
q.push(a);
while(!q.empty())
{
point v=q.top();
q.pop();
int x=(v.pos)%(n+1);
int y=(v.pos)/(n+1);
if(flag[x][y])continue;
flag[x][y]=1;
for(int i=head[x];i;i=edge[i].next)
{
int vv=edge[i].v;
if(d[vv][y]>d[x][y]+edge[i].w)
{
d[vv][y]=d[x][y]+edge[i].w;
q.push(point(d[vv][y],y*(n+1)+vv));
}
if(y==k)continue;
if(d[vv][y+1]>d[x][y])
{
d[vv][y+1]=d[x][y];
q.push(point(d[vv][y+1],(y+1)*(n+1)+vv));
}
}
}
printf("%d\n",d[n][k]);
}
void init()
{
si=0;
memset(head,0,sizeof(head));
memset(flag,0,sizeof(flag));
for(int i=1;i<N;i++)
for(int t=0;t<21;t++)
d[i][t]=inf;
d[1][0]=0;
}
int main()
{
int x,y,z,i,t;
while(~scanf("%d%d%d",&x,&y,&z))
{
init();
for(i=0;i<y;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
dij(x,z);
}
return 0;
}