ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze(分层最短路)

参考大佬博客 :https://blog.csdn.net/godjing007/article/details/97683174

题意大致如下 给你一个图你可以将k条边的权值变成0,求最短路

ac代码如下

#include <bits/stdc++.h>
 
typedef long long ll;
typedef long long ld;
 
using namespace std;
 
const ll maxn = 1e5 + 7;

ll n,m,k;
struct node
{
	ll x,step,num;
	node(ll xx,ll ste,ll nu)
	{
		x = xx;
		step = ste;
		num = nu;
	}
};

bool operator < (const node a,const node b)
{
	return a.step > b.step; 
}
vector<pair<ll,ll> > mp[maxn];
ll dis[maxn][11];
void dij()
{
	priority_queue<node> q;
	q.push(node(1,0,0));
	memset(dis,0x3f,sizeof(dis));
	dis[1][0] = 0;
	bool flag[maxn][11] = {0};
	while(!q.empty())
	{
		node te = q.top();
		q.pop();
		if(flag[te.x][te.num])continue;
		flag[te.x][te.num] = 1;
		for(int i = 0; i < mp[te.x].size(); i ++)
		{
			ll to = mp[te.x][i].first;
			ll val = mp[te.x][i].second;
			if(dis[to][te.num] > te.step + val && !flag[to][te.num])
			{
				dis[to][te.num] = te.step + val;
				q.push(node(to,dis[to][te.num],te.num));
			}
			if(te.num + 1 <= k)
			{
				if(dis[to][te.num + 1] > te.step && !flag[to][te.num])
				{
					dis[to][te.num + 1] = te.step;
					q.push(node(to,dis[to][te.num + 1],te.num + 1));
				}
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	
	ll t;
	cin >> t;
	while(t--)
	{
		cin >> n >> m >> k;
		for(int i = 1; i <= n; i ++)
		{
			mp[i].clear();
		}
		for(int i = 1; i <= m; i ++)
		{
			ll x,y,z;
			cin >> x >> y >> z;
			mp[x].push_back(make_pair(y,z));
		}
		
		dij();
		
		ll mi = 1e18;
		for(int i = 0; i <= k; i ++)
		{
			mi = min(dis[n][i],mi); 
		}
		cout << mi << endl;
	} 
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值