Poj OpenJudge 百练 1062 昂贵的聘礼
1.Link:
http://poj.org/problem?id=1062
http://bailian.openjudge.cn/practice/1062/
2.Content:
昂贵的聘礼
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37631 Accepted: 10872 Description
年轻的探险家来到了一个印第安部落里。在那里他和酋长的女儿相爱了,于是便向酋长去求亲。酋长要他用10000个金币作为聘礼才答应把女儿嫁给他。探险家拿不出这么多金币,便请求酋长降低要求。酋长说:"嗯,如果你能够替我弄到大祭司的皮袄,我可以只要8000金币。如果你能够弄来他的水晶球,那么只要5000金币就行了。"探险家就跑到大祭司那里,向他要求皮袄或水晶球,大祭司要他用金币来换,或者替他弄来其他的东西,他可以降低价格。探险家于是又跑到其他地方,其他人也提出了类似的要求,或者直接用金币换,或者找到其他东西就可以降低价格。不过探险家没必要用多样东西去换一样东西,因为不会得到更低的价格。探险家现在很需要你的帮忙,让他用最少的金币娶到自己的心上人。另外他要告诉你的是,在这个部落里,等级观念十分森严。地位差距超过一定限制的两个人之间不会进行任何形式的直接接触,包括交易。他是一个外来人,所以可以不受这些限制。但是如果他和某个地位较低的人进行了交易,地位较高的的人不会再和他交易,他们认为这样等于是间接接触,反过来也一样。因此你需要在考虑所有的情况以后给他提供一个最好的方案。
为了方便起见,我们把所有的物品从1开始进行编号,酋长的允诺也看作一个物品,并且编号总是1。每个物品都有对应的价格P,主人的地位等级L,以及一系列的替代品Ti和该替代品所对应的"优惠"Vi。如果两人地位等级差距超过了M,就不能"间接交易"。你必须根据这些数据来计算出探险家最少需要多少金币才能娶到酋长的女儿。Input
输入第一行是两个整数M,N(1 <= N <= 100),依次表示地位等级差距限制和物品的总数。接下来按照编号从小到大依次给出了N个物品的描述。每个物品的描述开头是三个非负整数P、L、X(X < N),依次表示该物品的价格、主人的地位等级和替代品总数。接下来X行每行包括两个整数T和V,分别表示替代品的编号和"优惠价格"。Output
输出最少需要的金币数。Sample Input
1 4 10000 3 2 2 8000 3 5000 1000 2 1 4 200 3000 2 1 4 200 50 2 0Sample Output
5250Source
3.Method:
dijkstra算法的应用,这题还增加了等级的考虑,我使用的枚举,然后运用算法前直接排除掉等级不符合要求的
4.Code:
#include <iostream>
#include <cstring>
#include <limits>
#include <cstdlib>
#include <cmath>
using namespace std;
int intMax = numeric_limits<int>::max();
int main()
{
//freopen("D://input.txt","r",stdin);
int i,j, k, ii;
int m, n;
cin >> m >> n;
//cout << m << " " << n << endl;
int **arr_map = new int*[n];
; i < n; ++i)
{
arr_map[i] = new int[n];
; j < n; ++j) arr_map[i][j] = intMax;
}
int *arr_p = new int[n];
int *arr_L = new int[n];
int p,L,x;
; i < n; ++i)
{
cin >> p >> L >> x;
arr_p[i] = p;
arr_L[i] = L;
int t,v;
; j < x; ++j)
{
cin >> t >> v;
arr_map[t - ][i] = v;
}
}
//for(j = 0; j < n; ++j)
//{
// for(k = 0; k < n; ++k) cout << arr_map[j][k] << " ";
// cout << endl;
//}
//cout << endl;
];
int *arr_d = new int[n];
bool *arr_m = new bool[n];
int **arr_map2 = new int*[n];
; i < n; ++i) arr_map2[i] = new int[n];
; i < n; ++i)
{
; ii <= m; ++ii)
{
int l_bettom = arr_L[i] - m + ii;
int l_top = arr_L[i] + ii;
; j < n; ++j) memcpy(arr_map2[j], arr_map[j], sizeof(int) * n);
; j < n; ++j)
{
/*if(abs(arr_L[j] - arr_L[i]) > m)
{
for(k = 0; k < n; ++k)
{
arr_map2[k][j] = intMax;
arr_map2[j][k] = intMax;
}
}*/
if(arr_L[j] < l_bettom || arr_L[j] > l_top)
{
; k < n; ++k)
{
arr_map2[k][j] = intMax;
arr_map2[j][k] = intMax;
}
}
}
//for(j = 0; j < n; ++j)
//{
// for(k = 0; k < n; ++k) cout << arr_map2[j][k] << " ";
// cout << endl;
//}
//cout << endl;
memcpy(arr_d, arr_map2[i], sizeof(int) * n);
memset(arr_m, ,sizeof(bool) * n);
arr_d[i] = ;
arr_m[i] = true;
; j < n; ++j)
{
int min_d = intMax;
int idx_d;
; k < n; ++k)
{
if(!arr_m[k] && min_d > arr_d[k])
{
min_d = arr_d[k];
idx_d = k;
}
}
if(min_d == intMax) break;
arr_m[idx_d] = true;
; k < n; ++k)
{
if(!arr_m[k] && arr_map2[idx_d][k] != intMax && min_d + arr_map2[idx_d][k] < arr_d[k])
{
arr_d[k] = min_d + arr_map2[idx_d][k];
}
}
//for(k = 0; k < n; ++k) cout << arr_d[k] << " ";
//cout << endl;
}
] != intMax && min_p > arr_d[] + arr_p[i]) min_p = arr_d[] + arr_p[i];
}
}
cout << min_p << endl;
; i < n; ++i) delete [] arr_map2[i];
delete [] arr_map2;
delete [] arr_m;
delete [] arr_d;
delete [] arr_L;
delete [] arr_p;
; i < n; ++i) delete [] arr_map[i];
delete [] arr_map;
;
}
5.Reference:
Poj OpenJudge 百练 1062 昂贵的聘礼的更多相关文章
- Poj OpenJudge 百练 1860 Currency Exchang
1.Link: http://poj.org/problem?id=1860 http://bailian.openjudge.cn/practice/1860 2.Content: Currency ...
- Poj OpenJudge 百练 2602 Superlong sums
1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlo ...
- Poj OpenJudge 百练 2389 Bull Math
1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Ma ...
- Poj OpenJudge 百练 1573 Robot Motion
1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...
- Poj OpenJudge 百练 2632 Crashing Robots
1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashin ...
- Poj OpenJudge 百练 Bailian 1008 Maya Calendar
1.Link: http://poj.org/problem?id=1008 http://bailian.openjudge.cn/practice/1008/ 2.content: Maya Ca ...
- poj 1062 昂贵的聘礼 (dijkstra最短路)
题目链接:http://poj.org/problem?id=1062 昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
- 最短路(Dijkstra) POJ 1062 昂贵的聘礼
题目传送门 /* 最短路:Dijkstra算法,首先依照等级差距枚举“删除”某些点,即used,然后分别从该点出发生成最短路 更新每个点的最短路的最小值 注意:国王的等级不一定是最高的:) */ #i ...
- POJ 1062 昂贵的聘礼(图论,最短路径)
POJ 1062 昂贵的聘礼(图论,最短路径) Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女 ...
随机推荐
- open和fopen的区别
open和fopen的区别: 1.缓冲文件系统缓冲文件系统的特点是:在内存开辟一个“缓冲区”,为程序中的每一个文件使用,当执行读文件的操作时,从磁盘文件将数据先读入内存“缓冲区”, 装满后再从内存“缓 ...
- 工作随笔记 点击除div自身之外的地方,关闭自己
<div id="showSelectOptions" style="width:100px;height:100px;background-color:red;b ...
- BootStrap2学习日记20---定制缩略图
先看看效果: 代码: <ul class="thumbnails"> <li class="span3"> <div class= ...
- 每天一道面试题(2):实现strncpy
目录 0. 为何要写strncpy? 1. 源码及测试结果 2. 面试注意事项 3. 小结
- C++模拟Java“内部”类
代码思路来自Thinking in C++ 10.13.1内部类方法.类似多重继承,但是类型转换是单向的:Outer支持“向上”转型,但是不能“向下”转型回Outer. #include <io ...
- [C.Sharp] Color 颜色转换
URL:http://www.cnblogs.com/taven/archive/2009/09/26/1574329.html 方法1: 引用命名空间:using System.Drawing; 1 ...
- 【Android 界面效果33】二级listview列表
今天来实现以下大众点评客户端的横向listview二级列表,先看一下样式. 这种横向的listview二级列表在手机软件上还不太常见,但是使用过平板的都应该知道,在平板上市比较常见的.可能是因为平板屏 ...
- spring--mybatis事务总结
spring--事务原理.mybatis--MapperScannerConfigurer 和 mybatis--MapperProxy事务,最近想把spring mybatis中的事务和mapper ...
- codeforces Good Bye 2013 379D New Year Letter
题目链接:http://codeforces.com/problemset/problem/379/D [题目大意] 告诉你初始字符串S1.S2的长度和递推次数k, 使用类似斐波纳契数列的字符串合并的 ...
- codevs3732==洛谷 解方程P2312 解方程
P2312 解方程 195通过 1.6K提交 题目提供者该用户不存在 标签数论(数学相关)高精2014NOIp提高组 难度提高+/省选- 提交该题 讨论 题解 记录 题目描述 已知多项式方程: a ...