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个金币作为聘礼才答应把女 ...
随机推荐
- 剑指 offer set 8 树的子结构
总结 1. 解法分为两步, 一是定位树的头结点, 二是两棵树作比较 2. 两个数作比较, 包括比较两棵树是否相等, 或者两个数是否镜像, 算法的框架类似 bool comp(root1, root2)
- 工作随笔记 点击除div自身之外的地方,关闭自己
<div id="showSelectOptions" style="width:100px;height:100px;background-color:red;b ...
- .NET 托管堆和垃圾回收
托管堆基础 简述:每个程序都要使用这样或那样的资源,包括文件.内存缓冲区.屏幕空间.网络连接.....事实上,在面向对象的环境中,每个类型都代表可供程序使用的一种资源.要使用这些资源,必须为代表 ...
- Socket 之 c#实现Socket网络编程
一.命名空间: 在网络环境下,最有用的两个命名空间是System.Net和 System.Net.Sockets. 1.System.Net:通常与较高程的操作有关,例如download或upload ...
- Java面向对象设计题2
有感于很多新人都不知道怎么学习软件开发,个人感觉还是因为练习做的太少,软件开发知识想看懂太难了,必须是边读资料边动手练习.莫说是新人,Java老人研究新技术的时候也是边读资料边练习.因此整理和编排了一 ...
- java 转义字符
\t 相当于tab,缩进\n 回车\r 换行\b 换成 一个黑点
- 40个GitHub上最受欢迎的iOS开源项目
40个GitHub上最受欢迎的iOS开源项目(一) http://www.weste.net/2013/8-1/92975.html 40个GitHub上最受欢迎的iOS开源项目(二) http:// ...
- mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)
1:添加控制器类文件HomeController.cs其代码如下: using System; using System.Collections.Generic; using System.Linq; ...
- android studio1.3安装终于成功
本人机器是win7 32位旗舰版,4G内存.以前使用eclipse adt bundle开发Android程序感觉非常方便,但随着google对andriod studio支持力度加大,转向studi ...
- Zend Studio 上 安装使用Aptana插件(html,css,js代码提示功能) .
最近装了zend studio 9.0 用了段时间发现写html,css,js代码没提示,要开dreamwaver(对js代码提示也不好).就网上搜索了下,发现了Aptana插件,装上用了下,感觉不错 ...