之前有分层图的题没做,比赛遇到挂了,补一补
这东西貌似迪杰斯特拉跑的比SPFA快很多
例:BZOJ1579
分成0~k层,表示用了多少次更新,每条边分一条连向下一层的边权值为0,然后向这两层转移
code:
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 11000;
const int maxm = 510000;
const int maxk = 25;
void down(int &x,int y){if(y<x)x=y;}
struct edge
{
int y,c,nex;
edge(){}
edge(int a1,int a2,int a3){y=a1;c=a2;nex=a3;}
}a[maxm<<1];int len,fir[maxn];
void ins(int x,int y,int c){a[++len]=edge(y,c,fir[x]);fir[x]=len;}
int d[maxk][maxn];
int n,m,K,st,ed;
struct node
{
int x,k,c;
node(){}
node(int a1,int a2,int a3){x=a1;k=a2;c=a3;}
};
priority_queue<node>q;
bool operator <(node x,node y){return x.c>y.c;}
void solve()
{
memset(d,63,sizeof d);d[0][st]=0;
q.push(node(st,0,0));
while(!q.empty())
{
node tmp=q.top();q.pop();
int x=tmp.x;
if(tmp.c>d[tmp.k][x])continue;
for(int k=fir[x];k;k=a[k].nex)
{
int y=a[k].y;
if(tmp.k<K&&d[tmp.k+1][y]>tmp.c)
{
d[tmp.k+1][y]=tmp.c;
q.push(node(y,tmp.k+1,tmp.c));
}
if(d[tmp.k][y]>tmp.c+a[k].c)
{
d[tmp.k][y]=tmp.c+a[k].c;
q.push(node(y,tmp.k,tmp.c+a[k].c));
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&K);
for(int i=1;i<=m;i++)
{
int x,y,c;scanf("%d%d%d",&x,&y,&c);
ins(x,y,c);ins(y,x,c);
}
st=1;ed=n;
solve();
int ret=INT_MAX;
for(int i=1;i<=K;i++)down(ret,d[K][ed]);
printf("%d\n",ret);
return 0;
}