📚【模板】FAKE k短路

#include <stdio.h>
#include <queue>
#include <string.h>
const int V = 1024;
const int E = 4096;
int n, m;
int s, e, k;

struct EDGE {
	int t, next, w;
} edge[E];
int edge_tot, head[V];
void add_edge(int f,int t,int w) {
	edge[++edge_tot].next = head[f];
	edge[edge_tot].t = t;
	edge[edge_tot].w = w;
	head[f] = edge_tot;
}

struct REDGE {
	int t, next, w;
} redge[E];
int redge_tot, rhead[V];
void add_redge(int f,int t,int w) {
	redge[++redge_tot].next = rhead[f];
	redge[redge_tot].t = t;
	redge[redge_tot].w = w;
	rhead[f] = redge_tot;
}

int h[V];

struct NODE {
	int id, val;
	bool operator < (const NODE &object) const {
		return val+h[id] > object.val+h[object.id];
	}
};
std :: priority_queue<NODE> q;
struct RNODE {
	int id, val;
	bool operator < (const RNODE &object) const {
		return val+h[id] > object.val+h[object.id];
	}
};
std :: priority_queue<RNODE> rq;

bool ck[V];
int cnt[V];
int kth(int begin,int end,int th) {
	memset(h,0x3f,(n+1)*sizeof(int));
	rq.push(RNODE{end,0});
	while(!rq.empty()) {
		RNODE u = rq.top();
		rq.pop();
		if(ck[u.id]) continue;
		ck[u.id] = true;
		h[u.id] = u.val;
		for(int j = rhead[u.id];j;j = redge[j].next) 
			rq.push(RNODE{redge[j].t,u.val+redge[j].w});
	}
	q.push(NODE{begin,0});
	while(!q.empty()) {
		NODE u = q.top();
		q.pop();
		cnt[u.id]++;
		if(u.id == end&&cnt[u.id] == th) 
			return u.val;
		if(cnt[u.id] > th) continue;
		for(int j = head[u.id];j;j = edge[j].next) 
			q.push(NODE{edge[j].t,u.val+edge[j].w});
	}
	return -1;
}

signed main() {
	scanf("%d %d",&n,&m);
	scanf("%d %d %d",&s,&e,&k);
	for(int i = 1, a, b, c;i <= m;++i) {
		scanf("%d %d %d\n",&a,&b,&c);
		add_edge(a,b,c);
		add_redge(b,a,c);
	}
	printf("%d\n",kth(s,e,k));
}
posted @ 2022-07-20 20:04  bikuhiku  阅读(19)  评论(0编辑  收藏  举报