#define ll long long #define I inline #define M 125001 #define K 10
usingnamespacestd;
int n , m , s , k;
structEdge{ int to; int last; ll dis; }edge[(M * K) << 1]; //每一层都要建m条边,并且题目中要求是双向边,因此,总边数为(m * k) * 2
int head[(M * K) << 1]; int edge_num;
I voidadd_edge(int from , int to , ll dis){ edge[++ edge_num] = Edge{to , head[from] , dis}; head[from] = edge_num; }
ll dis[(M * K) << 1]; bool vis[(M * K) << 1];
voidDijkstra(int s){ #define pairs pair<ll , ll >
priority_queue<pairs , vector<pairs > , greater<pairs > > Q; for(int i = 0; i <= M; i ++){ dis[i] = 0x3f3f3f3f; } dis[s] = 0; Q.push(make_pair(dis[s] , s)); while(!Q.empty()){ int v = Q.top().second; Q.pop(); if(vis[v]){ continue; } vis[v] = true; for(int i = head[v]; i ; i = edge[i].last){ int to = edge[i].to; if(dis[to] > dis[v] + edge[i].dis){ dis[to] = dis[v] + edge[i].dis; Q.push(make_pair(dis[to] , to)); } } } }
intmain(){ int u , v , w; int start , finish; cin >> n >> m >> k; cin >> start >> finish; for(int i = 0; i < m; i ++){ cin >> u >> v >> w; add_edge(u , v , w); add_edge(v , u , w); for(int j = 1; j <= k; j ++){ //对于每一层,分别建边 add_edge(u + (j - 1) * n , v + j * n , 0); //与下一层建立两条边权为0的边 add_edge(v + (j - 1) * n , u + j * n , 0); add_edge(u + j * n , v + j * n , w); //在本层中直接建边 add_edge(v + j * n , u + j * n , w); } } for(int i = 1; i <= k; i ++){ //最后在每一个结束点之间分别建一条边权为0的边,方便处理没用完的免费次数。 add_edge(finish + (i - 1) * n , finish + i * n , 0); } Dijkstra(start); cout << dis[finish + k * n] << "\n"; return0; }