UVa10662 - The Wedding(枚举法)

Background

Felix and Leti are going to get married soon, everybody ispreparing their gifts, but they have a big problem: the money,their budget is not very high. They would like to get a goodrestaurant, to "sleep" their first night in a nicehotel and to spend a marvellous honey-moon travelling around theworld.

The best way to get the cheapest price is getting anall-included package, that is, we have to contract the travel,the restaurant and the hotel all together.

Is that possible?

TheProblem

We have to find the cheapest travel agency-restaurant-hotelcombination.The problem is that not all the combinations areallowed.

TheInput

Each test case has the following format:

  • The first line consists of three integers T,R,H indicating the number of travel agencies, restaurants and hotels, respectively. Assume that T < 20, R < 20 and H < 20. Travel agencies, restaurants and hotels are numbered: 0, 1, 2, ...
  • The next T+R+H lines are divided into three blocks:
    • The first block has T rows and R+1 columns. The first column are the travel agencies' offer prices for the world-tour. In the rest of columns, cell (i,j) is 0 if the travel agency(i) can be combined with the restaurant(j) and 1 if not.
    • The second block has R rows and H+1 columns. The first column are the restaurants' offer prices. In the rest of columns, cell (i,j) is 0 if the restaurant(i) can be combined with the hotel(j) and 1 if not.
    • The third block has H rows and T+1 columns. The first column are the hotels' offer prices. In the rest of columns, cell (i,j) is 0 if the hotel(i) can be combined with the travel-agency(j) and 1 if not.
  • The input ends with an empty line.

TheOutput

For each test case the output should consist of a single linewith the number of the travel agency (T),restaurant (R) and hotel (H), and the cheapest total price (P). This values should be output in theformat: T R H:P

If there is not any combination, the output should be a linewith the text: Don't get married!

If more than one possibility exists, you can output any ofthem.

SampleInput

2 2 2
12 0 0
1 1 1
34 0 0
3 1 1
21 1 0
2 1 0
2 2 2
12 0 0
1 0 0
34 0 0
3 0 0
21 0 0
2 0 0
5 5 6
970 0 1 1 1 0 
856 0 0 0 0 0 
1290 1 0 0 1 0 
1361 0 0 1 0 0 
82 0 0 0 0 1 
1182 0 0 0 1 1 0 
450 0 1 1 0 0 1 
895 0 0 1 0 1 1 
1865 0 1 0 0 1 1 
183 1 1 1 1 1 0 
252 1 1 1 0 1 
1813 1 0 0 1 1 
1429 0 0 1 0 0 
1522 1 1 1 0 0 
1762 0 0 1 0 1 
1946 0 1 0 0 0 

SampleOutput

Don't get married!
1 1 1:6
4 1 3:2054
题意:给出旅行社、饭馆和宾馆的个数,求出花费最少的选择

思路:找出旅行社->饭馆+饭馆->宾馆+宾馆->旅行社值最小情况,用枚举法

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 25;
const int INF = 0x3f3f3f3f;

int travels, restaurants, hotels;

int t[MAXN][MAXN], r[MAXN][MAXN], h[MAXN][MAXN];

bool input()
{
	if (scanf("%d%d%d", &travels, &restaurants, &hotels) != 3) return false;
	
	for (int i = 0; i < travels; i++) {
		int tmp;
		scanf("%d", &tmp);
		for (int j = 0; j < restaurants; j++) {
			int combi;
			scanf("%d", &combi);
			if (combi == 0) t[i][j] = tmp;
			else t[i][j] = INF;
		}
	}
	
	for (int i = 0; i < restaurants; i++) {
		int tmp;
		scanf("%d", &tmp);
		for (int j = 0; j < hotels; j++) {
			int combi;
			scanf("%d", &combi);
			if (combi == 0) r[i][j] = tmp;
			else r[i][j] = INF;
		}
	}
	
	for (int i = 0; i < hotels; i++) {
		int tmp;
		scanf("%d", &tmp);
		for (int j = 0; j < travels; j++) {
			int combi;
			scanf("%d", &combi);
			if (combi == 0) h[i][j] = tmp;
			else h[i][j] = INF;
		}
	}
	
	return true;
}

void solve()
{
	int ans = INF;
	int x, y, z;
	
	for (int i = 0; i < travels; i++) {
		for (int j = 0; j < hotels; j++) {
			for (int k = 0; k < restaurants; k++) {
				if (t[i][k] != INF && r[k][j] != INF && h[j][i] != INF) {
					if (t[i][k] + r[k][j] + h[j][i] < ans) {
						x = i, y = k, z = j;
						ans = t[i][k] + r[k][j] + h[j][i];
					}
				}
			}
		}
	}
	
	if (ans == INF) {
		printf("Don't get married!\n");
	} else {
		printf("%d %d %d:%d\n", x, y, z, ans);
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("d:\\OJ\\uva_in.txt", "r", stdin);
	#endif
	
	while (input()) {
		solve();
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kgduu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值