WA#3#4#6#7#8#9QAQ为啥?
查看原帖
WA#3#4#6#7#8#9QAQ为啥?
877893
Daydreamers楼主2023/11/9 18:58
#include<bits/stdc++.h>
#define fr(r,a,b) for(ll r=a;r<=b;r++)
#define fl(l,a,b) for(ll l=a;l>=b;l--)
#define ull unsigned long long
#define ll long long
#define met(a,x) memset((a),(x),sizeof((a)));
using namespace std;
const int N=1e5+5;
const int inf=0x7f7f7f7f;
const ll oo=9e18;
int n,m,k,s,t,a,b,c;
int tot=0,head[1000005],dis[1000005][15];
bool vis[1000005][15];
struct qnode{
	int s,val,use;
	bool operator<(const qnode &a)const{
		return val>a.val;
	} 
};
struct node {
	int to,nxt,val;
};
node e[100005];
void add(int u,int v,int d)
{
	tot++;
	e[tot].to=v;
	e[tot].val=d;
	e[tot].nxt=head[u];
	head[u]=tot;
}
void dijkstra()
{
	met(dis,0x7f7f7f7f);
	dis[s][0]=0;
	priority_queue<qnode> q;
	qnode tmp={s,0,0};
	q.push(tmp);
	vis[s][0]=1;
	int now,s;
	
	while(!q.empty()){
		tmp=q.top();
		q.pop();
		s=tmp.s;
		now=tmp.use;
		vis[s][now]=false;
		for(int i=head[s];i;i=e[i].nxt)
		{
			int y=e[i].to;
			if(now<k&&dis[y][now+1]>dis[s][now]+e[i].val)
			{
				dis[y][now+1]=dis[s][now];
				if(!vis[y][now+1]){
					q.push((qnode){y,dis[y][now+1],now+1});
					vis[y][now+1]=1;
				}
			}
			if(dis[y][now]>dis[s][now]+e[i].val){
				dis[y][now]=dis[s][now]+e[i].val;
				if(!vis[y][now]){
					q.push((qnode){y,dis[y][now],now});
					vis[y][now]=1;
				}
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m>>k;
	cin>>s>>t;
	s++;t++;
	fr(i,1,m){
		cin>>a>>b>>c;
		a++;b++;
		add(a,b,c);
		add(b,a,c);
	}
	dijkstra();
	int ans=inf;
	fr(i,0,k){
		ans=min(ans,dis[t][i]);
	}
	cout<<ans<<endl;
	return 0;
}
2023/11/9 18:58
加载中...