一直输出极大值,并且是负的,求调,玄关
查看原帖
一直输出极大值,并且是负的,求调,玄关
530570
Firrel_qaq楼主2024/1/26 17:24
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
int n,m,k,s,mjbg;
struct edge{
	int to,nxt,w;
}mp[5000005];
struct que{
	int dis,id;
	bool operator <(const que &x)const{
        return x.dis < dis;
    }
};
int head[5000005];
int tot;
int white[5000005],vis[5000005];
void add(int u,int v,int w){
	mp[++tot].to = v;
	mp[tot].nxt = head[u];
	mp[tot].w = w;
	head[u] = tot;
	return ;
}
int dijkstra(){
	priority_queue<que> q;
	q.push({0,s});
	while(!q.empty()){
		int t = q.top().id;
		q.pop();
		if(white[t]) continue;
		white[t] = 1;
		for(int i = head[t];i;i = mp[i].nxt){
			vis[mp[i].to] = min(vis[t] + mp[i].w,vis[mp[i].to]);
			if(!white[mp[i].to]) q.push({vis[mp[i].to],mp[i].to});
		}
	}
	return vis[mjbg];
}
int main(){
	scanf("%d%d%d",&n,&m,&k);
	scanf("%d%d",&s,&mjbg);
	for(int i = 0;i <= n;i++) vis[i] = 2147483645;
	for(int i = 1;i <= m;i++){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		add(u,v,w);
		add(v,u,w);
		for(int j = 1;j <= k;j++) add(j * n + u,j * n + v,w);
	}
	for(int i = 0;i < n;i++){
		for(int j = head[i];j;j = mp[j].nxt){
			for(int cnm = 2;cnm <= k;cnm++){
				add((cnm - 1) * n + i,cnm * n + mp[j].to,0);
				add(cnm * n + mp[j].to,(cnm - 1) * n + i,0);
			}
		}
	}
	int nmb = dijkstra();
	printf("%d",nmb);
	return 0;
}

2024/1/26 17:24
加载中...