牛客假日团队赛54-L Cow routing

Cow routing
题目描述

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. 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 only a single route. Please help her decide what is the minimum cost she must pay.

输入描述:

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 a single route Bessie can use to travel
from city A to city B. If there is no such route, output -1.

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

Although there is a cheaper two-route solution (use route 2 to travel
from city 1 to city 3, then route 1 to travel from city 3 to city 2),
Bessie is only allowed to use a single route, so she must use route 3
at a cost of 8.

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

题解: 直接遍历所有的航线,如果该航线满足牛妹搭载的条件,即首先途径A城然后再途径B城,这时我们就可以更新牛妹从A到B的花销了,继续再找,如果发现有一条新的航线的花销比之前那条航线的花销要少,则更新之前的花销,直至所有的航线都遍历完。

** c++ AC 代码 **

#include<bits/stdc++.h>
using namespace std;

int a,b,routes;
int ans = -1;

int main()
{
	scanf("%d%d%d",&a,&b,&routes);
	while(routes--)
	{
		int cost,number;
		scanf("%d%d",&cost,&number);
		int s = 0, e = 0;	// s 代表牛妹开始的城市,e 代表牛妹要抵达的终点
		for(int i=0;i<number;i++)
		{
			int airplane;
			scanf("%d",&airplane);
			if(!s && airplane==a)
			{
				s = 1;
				continue;
			}
			if(s && !e && airplane==b)
			{
				e = 1;
				continue;
			}
		}
		if(e)
		{
			if(ans==-1 || ans >cost)
				ans = cost;
		}
	}
	printf("%d\n",ans);
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值