分层图的dijkstra裸题,昨天T了感觉是分层push的时候复杂度过高要加一个层,不想改,结果今天改完对了...哭了
#include <iostream>
#include <cstdio>
#include <cstring>
#include<algorithm>
#include<sstream>
#include<queue>
#include<set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn = 10003;
int n,m,k;
int s,t;
int dis[maxn][12];
struct EDGE
{
int to,dis,ceng;
EDGE()
{
to = dis = ceng = 0;
}
EDGE(int a,int b,int c)
{
to = a;
dis = b;
ceng = c;
}
bool operator<(const EDGE &a)const
{
return dis>a.dis;
}
};
priority_queue<EDGE> que;
vector<EDGE> vec[maxn];
void dijkstra()
{
memset(dis,INF,sizeof(dis));
EDGE ed;
dis[t][0] = 0;
EDGE par;
que.push(EDGE(t,0,0));
while(!que.empty())
{
par = que.top();
que.pop();
int p = par.to;
int len = vec[p].size();
for(int i=0; i<len; i++)
{
ed = vec[p][i];
if(dis[p][par.ceng] + ed.dis < dis[ed.to][par.ceng])
{
dis[ed.to][par.ceng] = dis[p][par.ceng] + ed.dis;
que.push(EDGE(ed.to,dis[ed.to][par.ceng],par.ceng));
}
if(par.ceng != k && dis[ed.to][par.ceng+1] > dis[p][par.ceng])
{
dis[ed.to][par.ceng+1] = dis[p][par.ceng];
que.push(EDGE(ed.to,dis[ed.to][par.ceng+1],par.ceng+1));
}
}
}
}
int main()
{
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int a,b,c;
cin>>n>>m>>k;
cin>>s>>t;
for(int i=0; i<m; i++)
{
cin>>a>>b>>c;
vec[a].push_back(EDGE(b,c,0));
vec[b].push_back(EDGE(a,c,0));
}
dijkstra();
int zuixiao = INF;
for(int i=0; i<=k; i++)
{
zuixiao = min(zuixiao,dis[s][i]);
}
cout<<zuixiao<<'\n';
return 0;
}