昂贵的聘礼 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 ...
随机推荐
- Java编程思想:嵌套类
public class Test { public static void main(String[] args) { // Parcell11.test(); // ClassInterface. ...
- JavaScript作用域及预编译
几乎所有的编程语言都可以存储,访问,修改变量,那在JavaScript中这些变量放在那里?程序如何找到他们? js被归类于解释执行语言,但事实上他也是一门编译语言,因为他也要编译,但于传统的编译语言不 ...
- 《ElasticSearch6.x实战教程》之简单搜索、Java客户端(上)
第五章-简单搜索 众里寻他千百度 搜索是ES的核心,本节讲解一些基本的简单的搜索. 掌握ES搜索查询的RESTful的API犹如掌握关系型数据库的SQL语句,尽管Java客户端API为我们不需要我们去 ...
- 手把手教你破解文件密码、wifi密码、网页密码
手把手教你破解文件密码.wifi密码.网页密码 1.破解文件密码: 有时候我们在网上下载一个压缩包后,必须要关注或者支付一定费用才给你解压密码,实属比较恶心.在这里手把手叫你实现破解文件解压密码. 1 ...
- 浅谈单点登陆(SSO)
背景 在企业发展初期,企业使用的系统很少,通常一个或者两个,每个系统都有自己的登录模块,运营人员每天用自己的账号登录,很方便. 但随着企业的发展,用到的系统随之增多,运营人员在操作不同的系统时,需要多 ...
- Java基础之方法
方法 某段代码经常使用,可以使用大括号将这段代码包括起来,起个名字,以后就使用这个名字来代替这段代码. 定义格式: 修饰符 返回值类型 方法名(参数列表) { 方法体语句: return语句: } ...
- 【Java】Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 1099
详细信息如下: Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: ...
- 【iOS】UIAlertView 点击跳转事件
iOS 开发中,UIAlertView 经常用到.这里记录下曾用到的点击跳转事件. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@& ...
- 【iOS】iOS Error Domain=NSCocoaErrorDomain Code=3840 "未能完成操作。(“Cocoa”错误 3840。)"
昨天遇到的这个问题,详细信息: ----->类和方法__25+[Manager noticeRequest:]_block_invoke399----->错误信息Error Domain= ...
- poj 1455 Crazy tea party
这道题第一眼看去很难,其实不然,短短几行代码就搞定了. 说一下大概思路,如果是排成一排的n个人,如 1 2 3 4 5 6 7 8 我们要变成 8 7 6 5 4 3 2 1 需要交换 28次,找规律 ...