POJ - 1062 昂贵的聘礼 Dijkstra
思路:构造最短路模型,抽象出来一个源点,这个源点到第i个点的费用就是price[i],然后就能抽象出图来,终点是1.
任意两个人之间都有等级限制,就枚举所有最低等级限制,然后将不再区间[min_lev, min_lev+m]区间的点都删除,就能进行Dijkstra算法了。
AC代码
#include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <utility> #include <string> #include <iostream> #include <map> #include <set> #include <vector> #include <queue> #include <stack> using namespace std; #define eps 1e-10 #define inf 0x3f3f3f3f #define PI pair<int, int> typedef long long LL; const int maxn = 100+5; int price[maxn], lev[maxn]; int cost[maxn][maxn], vis[maxn], d[maxn]; int n, m; int Dijkstra() { memset(d, inf, sizeof(d)); d[0] = 0; for(int i = 0; i <= n; ++i) { int x = 0, tmp = inf; for(int y = 0; y <= n; ++y) if(!vis[y] && d[y] <= tmp) tmp = d[x=y]; if(vis[x]) continue; vis[x] = 1; for(int y = 0; y <= n; ++y) if(!vis[y]) d[y] = min(d[y], d[x] + cost[x][y]); } return d[1]; } int main() { while(scanf("%d%d", &m, &n) == 2) { int r, x, c; memset(cost, inf, sizeof(cost)); for(int i = 1; i <= n; ++i) { scanf("%d%d%d", &price[i], &lev[i], &r); cost[0][i] = price[i]; //源点0到各点的费用 for(int j = 0; j < r; ++j) { scanf("%d%d", &x, &c); cost[x][i] = min(cost[x][i], c); } } int ans = inf; for(int i = 1; i <= n; ++i) { int min_lev = lev[i]; //最低等级 memset(vis, 0, sizeof(vis)); for(int j = 1; j <= n; ++j) { if(lev[j] < min_lev || lev[j] - min_lev > m) vis[j] = 1; } int dis = Dijkstra(); ans = min(ans, dis); } printf("%d\n", ans); } return 0; }
如有不当之处欢迎指出!
POJ - 1062 昂贵的聘礼 Dijkstra的更多相关文章
- 最短路(Dijkstra) POJ 1062 昂贵的聘礼
题目传送门 /* 最短路:Dijkstra算法,首先依照等级差距枚举“删除”某些点,即used,然后分别从该点出发生成最短路 更新每个点的最短路的最小值 注意:国王的等级不一定是最高的:) */ #i ...
- POJ 1062 昂贵的聘礼(图论,最短路径)
POJ 1062 昂贵的聘礼(图论,最短路径) Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女 ...
- poj 1062 昂贵的聘礼 (dijkstra最短路)
题目链接:http://poj.org/problem?id=1062 昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
- POJ 1062 昂贵的聘礼(带限制条件的dijkstra)
题目网址:http://poj.org/problem?id=1062 题目: 昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- POJ - 1062 昂贵的聘礼(最短路Dijkstra)
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u SubmitStatus Descr ...
- POJ 1062 昂贵的聘礼
C - 昂贵的聘礼 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit St ...
- 最短路POJ 1062 昂贵的聘礼
C - 昂贵的聘礼 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ -1062 昂贵的聘礼(前向星 && SPFA)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/30299671 题目链接:id=1062&qu ...
- POJ 1062 昂贵的聘礼 (最短路)
昂贵的聘礼 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/M Description 年轻的探险家来到了一个印第安部落里.在那里 ...
随机推荐
- 壮美大山包-2017中国大山包国际超百公里ITRA积分赛赛记
2017年4月份用时28个多小时跑完江南100,付出的代价是双脚多达8个以上的水泡.所以接下来的4月29日的环汶川越野赛果断的从60公里换到了30公里组,慢悠悠的完成了. 虽然在赛道上一度想再也不参加 ...
- Using $this when not in object context in
错误信息:$this引用没有上下文 原因:在PHP5中,static声明的静态方法里不可以使用$this 需要使用self来引用当前类中的方法或是变量. 引用的方法里不可以带$this(示例代码中为g ...
- js给页面添加滚动事件并判断滚动方向
<script> var scrollFunc = function (e) { var direct = 0; e = e || window.event; if (e.wheelDel ...
- openvpn服务端与客户端网段互通
http://www.softown.cn/post/140.html OpenVPN安装.配置教程 http://www.softown.cn/post/137.html openvpn的serve ...
- Ajax发送数据
- 选择结构if、switch
选择结构if.switch 一.if三种形式 if if-else 3.if -else if -else 二.switch 针对某个表达式的值做出判断,成为决定执行代码块 switch 语句特点: ...
- 理解javascript模块化(转)
模块化是一个通用的编程最佳实践.程序的模块化使我们可以更方便地使用别人的代码,想要什么功能,就加载什么模块,从而提高代码的利用效率,增加开发速度. 模块就像积木,有了它,我们可以搭出各种各种功能样式的 ...
- vim批量注释
vim批量注释 法一.在vim中 :20,30 s/^/#/g 20-30行 用 # 注释掉.(python是用#注释地---) :20,30 s/^#//g 20-30行 取消注释 法二. 1.多行 ...
- 我是如何让minio client上传速度提高几十倍的
minio java client 使用okhttp作为底层的http实现,在产品包里面局域网上传文件的速度一直只有400~800KB/s,经过一天排查发现是-Djava.compile=none禁用 ...
- 高通HAL层之Sensor HAL
高通的HAL层其实分为两种,一种是直接从kernel这边报数据上来的,由sensor HAL层来监听,另一种是走ADSP的模式,HAL层是通过qmi的形式进行监听的: 走ADSP架构的可以看下面的博客 ...