集美大学“第15届蓝桥杯大赛(软件类)“校内选拔赛 H卯酉东海道

dijk + spfa思想 然后你需要存一下每个点 * l种颜色,你开个数组存一下

st[i][j] 为到达i点且到达以后是j颜色的最小距离是否已经确定了

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e5+10;
struct Edge{
	ll to,col,w;
	bool operator<(const Edge&W)const{
		return w>W.w;
	}
};
int n,m,l,base;
bool st[N][70];
vector<Edge>g[N];
ll ans = 1e15;
ll dist[N];

void spfa(int t){
	
	memset(st,0,sizeof st);
	
	priority_queue<Edge>q;
	q.push({1,t,0});
	for(int i=1;i<=n;i++)dist[i] = 1e15;
	dist[1] = 0;
	
	while(q.size()){
		auto t = q.top();
		q.pop();
		int to = t.to,col = t.col;
		if(st[to][col])continue;
		st[to][col] = true;
		
		
		for(auto&ts:g[to]){
			int tos = ts.to,cols = ts.col,ws = ts.w;
			if(dist[tos]>dist[to]+(cols==col?1:base)*ws){
				dist[tos] = dist[to] + (cols==col?1:base)*ws;
				q.push({tos,cols,dist[tos]});
			}
		}
	}
	ans = min(ans,dist[n]);
	
}

void solve()
{
	cin>>n>>m>>l>>base;
	for(int i=1;i<=m;i++){
		int a,b,c,d;cin>>a>>b>>c>>d;
		g[a].push_back({b,c,d});
	}
	
	for(int i=1;i<=l;i++)spfa(i);
	
	if(ans==1e15)cout<<-1<<"\n";
	else cout<<ans<<"\n";
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;_ = 1;
	while(_--)solve();
}

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值