补题向 | Magical Girl Haze(dijkstra堆优化+分层图)

Magical Girl Haze

There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici​. Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances become 00. Now she wants to go to City NN, please help her calculate the minimum distance.

Input

The first line has one integer T(1 \le T\le 5)T(1≤T≤5), then following TTcases.

For each test case, the first line has three integers N, MN,M and KK.

Then the following MM lines each line has three integers, describe a road, U_i, V_i, C_iUi​,Vi​,Ci​. There might be multiple edges between uu and vv.

It is guaranteed that N \le 100000, M \le 200000, K \le 10N≤100000,M≤200000,K≤10,
0 \le C_i \le 1e90≤Ci​≤1e9. There is at least one path between City 11 and City NN.

Output

For each test case, print the minimum distance.

样例输入复制

1
5 6 1
1 2 2
1 3 4
2 4 3
3 4 1
3 5 6
4 5 2

样例输出复制

3

题目来源

ACM-ICPC 2018 南京赛区网络预赛

为了方便理解,稍微转换一下题意

n个城市,m条有向路,每条路距离为c,最多可以把k条路距离变为0,求从1到n的最小距离

将图的点分为k+1层,也就是说,在每个点,可以通过某一条路到达另一个点,距离w,也可以直接坐电梯到下一层的另一个点,距离视为0,由于只有k层,所以最多只能把k条路距离变为0(坐k次电梯,按我的理解,也可以不坐电梯,所以最后在每一层的终点找出最短距离)

图建好后用dijkstra堆优化找出最短路径

#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<fstream>
#include<math.h>
#include<stack>
#include<queue>
#include<bitset>
#include<utility>
#include<set>
#include<map>
#define INF 0x7f7f7f7f
using namespace std;
typedef long long ll;
const int N=3000000;
int t;
int n,m,k;
struct P{
	ll i;
	ll dist;
	friend bool operator<(P a,P b){
		return a.dist>b.dist;
	}
};
struct Edge{
	ll y;
	ll w;
	ll nt;
};
Edge e[N<<1];
int head[N];
ll dist[N];
int cnt;
void add(ll x,ll y,ll w){
	e[cnt].y=y;
 	e[cnt].w=w;
 	e[cnt].nt=head[x];
 	head[x]=cnt;
 	cnt++;
}
void dij(int x){
	priority_queue<P> q;
	memset(dist,INF,sizeof(dist));
	dist[x]=0;
	P s;
	s.i=x;
	s.dist=dist[0];
	q.push(s);
	while(!q.empty()){
		P old=q.top();
		q.pop();
		int a=old.i;
		for(int i=head[a];i!=-1;i=e[i].nt){
			int b=e[i].y;
			if(dist[b]>dist[a]+e[i].w){
				dist[b]=dist[a]+e[i].w;
				P now;
				now.i=b;
				now.dist=dist[b];
				q.push(now);
			}
		}
	}
}
int main(){
 
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d",&n,&m,&k);
		memset(head,-1,sizeof(head));
		if(k>=m){
			printf("0\n");
			continue;
		}
		ll x,y,w;
		cnt=0;
		for(int i=1;i<=m;i++){
			scanf("%lld%lld%lld",&x,&y,&w);
			for(int j=0;j<=k;j++){
				add(x+j*n,y+j*n,w);
				if(j!=k){
					add(x+j*n,y+(j+1)*n,0);
				}
			}
		}
		dij(1);
		ll ans=INF;
		for(int i=0;i<=k;i++){
			ans=min(ans,dist[n+i*n]);
		}
		printf("%lld\n",ans);
	}
	
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

打懵圈了,不知道为什么最后做着做着理解成要把前k步变为0,还一直在找错~%…,# *'☆&℃$︿★? (@_@;)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bekote

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值