HDU1690 Bus System 【floyd算法】

题目大意:
在这里插入图片描述
输入

2  //代表2个案例
1 2 3 4 1 3 5 7  案例1     代表l1---l4  c1 -- c4
4 2  4个站 2个问题 求2个路径
1
2
3
4
1 414 的最小花费
4 141的最小花费
1 2 3 4 1 3 5 7   /案例2
4 1
1
2
3
10
1 4

输出

Case 1:
The minimum cost between station 1 and station 4 is 3.
The minimum cost between station 4 and station 1 is 3.
Case 2:
Station 1 and station 4 are not attainable.
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const long long inf = 1e18;
const int N = 110;
typedef long long ll;
ll map[N][N];
ll x[N];
int n, m;
ll L1, L2, L3, L4, C1, C2, C3, C4;

void floyd(){
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			for(int k = 1; k <= n; k++)
				map[j][k] = min(map[j][k], map[j][i] + map[i][k]);
}
ll getm(ll dis){
	if(dis < 0) dis = -dis;
	if(dis > 0 && dis <= L1) return C1;
	if(dis > L1 && dis <= L2) return C2;
	if(dis > L2 && dis <= L3) return C3;
	if(dis > L3 && dis <= L4) return C4;
	return inf; 
}
int main(){
	int t ;
	scanf("%d", &t);
	int tt = 0;
	while(t--){
		
		scanf("%lld%lld%lld%lld%lld%lld%lld%lld",&L1,&L2,&L3,&L4,&C1,&C2,&C3,&C4);
		scanf("%d%d", &n, &m);
		memset(map, inf, sizeof map);
		for(int i = 1; i <= n; i++) scanf("%lld", &x[i]);
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				map[i][j] = map[j][i] = getm(x[i] - x[j]);
		floyd();
		cout << "Case " << ++tt << ":" << endl;	
		while(m--){
			int u, v;
			scanf("%d%d", &u, &v);
			if(map[u][v] != inf) printf("The minimum cost between station %d and station %d is %lld.\n",u,v,map[u][v]);
			else printf("Station %d and station %d are not attainable.\n",u,v);
		}	
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值