牛客假日团队赛54-K Cow Routing II

Cow Routing II
题目描述

Tired of the cold winter weather on her farm, Bessie the cow plans to fly to a warmer destination for vacation. Unfortunately, she discovers that only one airline, Air Bovinia, is willing to sell tickets to cows, and that these tickets are somewhat complicated in structure.
Air Bovinia owns N planes (1 <= N <= 500), each of which flies on a specific “route” consisting of two or more cities. For example, one plane might fly on a route that starts at city 1, then flies to city 5, then flies to city 2, and then finally flies to city 8. No city appears multiple times in a route. If Bessie chooses to utilize a route, she can board at any city along the route and then disembark at any city later along the route. She does not need to board at the first city or disembark at the last city. Each route has a certain cost, which Bessie must pay if she uses any part of the route, irrespective of the number of cities she visits along the route, and Bessie may make use of a route only once (that is, she cannot use a route and then later use a different part of the same route).
Bessie would like to find the cheapest way to travel from her farm (in city A) to her tropical destination (city B). Since she does not want to be confused by a complicated itinerary, she wants to use at most two routes. Please help her decide what is the minimum cost she must pay.
[Note that the only difference between this problem and the preceding bronze problem is that here Bessie can use up to two routes, versus only one route in the preceding problem]

输入描述:

The first line of input contains A, B, and N, separated by spaces.
The next 2N lines describe the available routes, in two lines per
route. The first line contains the cost of using the route (an integer
in the range 1…1000), and the number of cities along the route (an
integer in the range 1…500). The second line contains a list of the
cities in order along the route. Each city is identified by an
integer in the range 1…10,000.

输出描述:

Output the minimum cost of an itinerary using at most two routes that
Bessie can use to travel from city A to city B. If there is no such
solution, output -1.

示例1
输入
1 2 3
3 3
3 2 1
4 4
2 1 4 3
8 5
4 1 7 8 2
输出
7
说明

Use route 2 to travel from city 1 to city 3, then route 1 to travel
from city 3 to city 2.

题意: 牛妹要从 A 城坐飞机到 B 城去,它嫌麻烦,只能接受一次转机,或者是坐那种直达的,从直达航班和转机航班中选出最经济实惠的一班航班。现给出输入,第一行输入分别是 A 城 、B 城、航空公司拥有的航线。牛妹可以选择任意一条航线在任意城坐飞机,但是它如果要到达B城,前提是,该航线途径得要先途经A城,并且再而途经B城,如果B城的出现的位置在A城前面那么牛妹就不能搭该班航线从A到B。接下来输入2*N行的数据,第一行第一个数字是表示该航线的花费,第二个数字表示该航线经过几个城市,第二行表示该航线途径城市的顺序。

题解: 开两个数组,一个记录从起点城市开始到它后面能够到达的城市的最小花销,另外一个记录从终点城市往前推(也就是从哪些城市能够直达终点)的最小花销,从这两个数组中选出组合最小的花销,再和直达航班的价格进行比较,哪个小就选用哪个。详细逻辑见代码。

c++ AC 代码

#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 10010;
int sx[MAX_N], ex[MAX_N], tmp[510]; // sx:从起点开始到达起点以后的城市的最小费用
									// ex:从终点倒着往前推能够到达的城市的最小花销
int main()
{
	int a, b, n, r = 9999;
	fill(sx, sx + MAX_N, 9999); // 将他们都初始化为一个较大的值
	fill(ex, ex + MAX_N, 9999);
	scanf("%d%d%d", &a, &b, &n);
	for (int i = 0; i < n; i++)
	{
		int cost, nums, x;
		scanf("%d%d", &cost, &nums);
		bool s = false;
		for (int j = 0; j < nums; j++)
		{
			scanf("%d", &x);
			tmp[j] = x;
			if (x == a) // 找到起点
				s = 1;
			if (s)						  // 找到起点后,将起点后的城都加到 sx 中,数组中存的值即最小花销
				sx[x] = min(sx[x], cost); // 将起点可达的城都加到sx中,并设置到这些城的花销
			if (s && x == b)
				r = min(r, cost);
		}
		s = 0;
		for (int j = nums - 1; j >= 0; j--) // 顺着终点往前推,将可达终点的城都加到ex中,并实时更新最小花销
		{
			if (tmp[j] == b) // 找到终点了,这时候就可以往前推了
				s = 1;
			if (s)
				ex[tmp[j]] = min(ex[tmp[j]], cost);
		}
	}

	for (int i = 0; i < 10010; i++)
		if (i != a && i != b && sx[i] != 9999 && ex[i] != 9999)
			r = min(r, sx[i] + ex[i]); // sx[i] + ex[i] 保证了最多只通过两个城
	if (r == 9999)
		r = -1;
	printf("%d\n", r);
	//system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值