昂贵的聘礼 POJ - 1062
题目链接:https://vjudge.net/problem/POJ-1062

如图,我们可以把交换的情况,抽象为一个有向图,
先抛去等级限制,那么就是一个最短路,从①出发,到达其他点的最短路中
最短的那个就是我们需要的答案了。
当然松弛条件变成了 dis[now] - pos[now].w + w(now->to) + pos[to].w < dis[to],
这里,如果我们走了一条边,那么原来的最后加进去的物品的价格一定会发生改变,那么我们把原来的物品价格减去,
改成交换后的价格,然后要加上我们买to的那个物品的价格,然后与dis[to]比较。
等级限制,就是说你在交换物品的路上,你交换物品的所有商人集合记为P,某人商人记为px,
∀pi , pj∈P, abs(pi.level - pj.level) <= L.那么我们需要记录你一路上商人的等级,当然,我们只需要考虑极端情况就好了,
记录一个最低等级,一个最高等级,如果这两个满足,那么其他商人都会满足。代码会有解释。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std; typedef long long LL;
#define inf 1e9
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--) const int N = ;
int head[N];
bool vis[N];
int dis[N];
int cnt;
int L,n; struct Pos{
int w;
int level;
}pos[N]; struct Edge{
int to;
int w;
int next;
}e[(int)1e7]; struct node{
int loc;//当前位置
int level_max;//之前的最高等级
int level_min;//最浅的最低等级
int dis;//距离
bool friend operator< (const node& a,const node& b){
return a.dis > b.dis;
}
}; priority_queue<node> que; void add(int u,int v,int w){
e[cnt].to = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
} //我们要交换的商人 与之前的最高和最低等级是否满足等级限制
inline bool check_loc(node& now,int to){
return abs(now.level_max - pos[to].level) <= L && abs(now.level_min - pos[to].level) <= L;
} void dijkstra(){ rep(i,,n) dis[i] = inf;
dis[] = pos[].w;
que.push(node{, pos[].level, pos[].level, dis[]}); node now;
int to,w;
while(!que.empty()){ now = que.top();
que.pop();
if(vis[now.loc]) continue;
vis[now.loc] = true; for(int o = head[now.loc]; ~o; o = e[o].next){
to = e[o].to;
w = e[o].w; if(dis[now.loc] - pos[now.loc].w + w + pos[to].w < dis[to] && check_loc(now,to)){
dis[to] = dis[now.loc] - pos[now.loc].w + w + pos[to].w;
//更新遇到的最高等级和最低等级
que.push(node{to, min(now.level_min, pos[to].level), max(now.level_max, pos[to].level),
dis[to]});
}
}
} int ans = inf;
rep(i,,n) ans = min(ans,dis[i]);
cout << ans << endl;
} int main(){ scanf("%d%d",&L,&n); rep(i,,n) head[i] = -;
cnt = ;
int tot,v,w;
rep(u,,n){
scanf("%d%d%d",&pos[u].w,&pos[u].level,&tot);
rep(j,,tot){
scanf("%d%d",&v,&w);
add(u,v,w);
}
} dijkstra(); getchar(); getchar();
return ;
}
昂贵的聘礼 POJ - 1062的更多相关文章
- (最短路 dijkstra)昂贵的聘礼 -- poj -- 1062
链接: http://poj.org/problem?id=1062 昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- 昂贵的聘礼 POJ - 1062(最短路)
年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:" ...
- 昂贵的聘礼 - poj 1062 (Dijkstra+枚举)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39976 Accepted: 11596 Description 年 ...
- POJ 1062 昂贵的聘礼
C - 昂贵的聘礼 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit St ...
- 最短路(Dijkstra) POJ 1062 昂贵的聘礼
题目传送门 /* 最短路:Dijkstra算法,首先依照等级差距枚举“删除”某些点,即used,然后分别从该点出发生成最短路 更新每个点的最短路的最小值 注意:国王的等级不一定是最高的:) */ #i ...
- Poj OpenJudge 百练 1062 昂贵的聘礼
1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 T ...
- 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 昂贵的聘礼
C - 昂贵的聘礼 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit St ...
随机推荐
- JS获取display为none的隐藏元素的宽度和高度的解决方案
有时候,我们一进入页面,就需要获取display为none元素的物理尺寸(宽高),或获取display为none元素的子元素的物理尺寸(宽高),本篇文章就如何解决以上问题给出自己的解决方案 <h ...
- [NOIP2016]换教室 题解(奇怪的三种状态)
2558. [NOIP2016]换教室 [题目描述] 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第i(1< ...
- 在阿里云上搭建Spring Initializr服务器。
参考的博客有: https://blog.csdn.net/chszs/article/details/51713174 https://segmentfault.com/a/119000001137 ...
- .NET多线程之调用上下文CallContext
命名空间:System.Runtime.Remoting.Messaging 类型完全限定名称:System.Runtime.Remoting.Messaging.CallContext 官方介绍:h ...
- 浅谈对static的理解
相信很多朋友在面试过程中都遇到过关于static的相关题目,接下来我们来分析一下static. static(静态的),用来修饰成员变量,成员方法,它随着类的加载而加载,使用static修饰的数据可以 ...
- Your project specifies TypeScriptToolsVersion 2.3, but a matching compiler ...... 出现这种警告解决方式
- fjnu2016-2017 低程 PROBLEM C 汪老司机
动态规划 方程 #include <iostream>#include <iomanip>#include <cmath>#include <algorith ...
- python UUID
UUID介绍 UUID是128位的全局唯一标识符,通常由32字节的字符串表示.它可以保证时间和空间的唯一性,也称为GUID,全称为:UUID ―― Universally Unique IDentif ...
- 一个完整的产品设计流程——家庭安全管家
不管是产品设计,还是前后端开发,始终都应该做出来才能够有很好的提高锻炼.书看得再多,如果不配合实际练习始终得不到实质性的进展. 接下来的案例是和几位学弟学妹一起做的,契机是参加一个用户体验设计比赛,从 ...
- Spring源码分析之环境搭建
写在最前面 最近突然心血来潮,想看看源码,看看大牛都怎么码代码,膜拜下.首选肯定是spring大法,于是说干就干,从GitHub上下载spring-framework源码编译拜读. 环境搭建 安装JD ...