hdu 1062(DFS||dijkstra)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 44870 | Accepted: 13259 |
Description
为了方便起见,我们把所有的物品从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 0
Sample Output
5250
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int INF = ;
const int N = ;
int m,n;
int MIN;
int graph[N][N];
struct Node{
int v,level;
}node[];
bool vis[N]; void dfs(int u,int ans,int level1,int level2){
vis[u] = true;
MIN = min(ans+node[u].v,MIN);
for(int i=;i<=n;i++){
if(!vis[i]&&graph[u][i]<INF){
if(abs(node[i].level-level1)>m) continue;
if(abs(node[i].level-level2)>m) continue;
dfs(i,ans+graph[u][i],min(node[i].level,level1),max(level2,node[i].level));
vis[i] = false;
}
}
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
graph[i][j] = INF;
if(i==j) graph[i][j] = ;
}
}
int LEVEL;
for(int i=;i<=n;i++){
int num;
scanf("%d%d%d",&node[i].v,&node[i].level,&num);
for(int j=;j<=num;j++){
int a,b;
scanf("%d%d",&a,&b);
graph[i][a] = min(b,graph[i][a]);
}
}
LEVEL = node[].level;
memset(vis,,sizeof(vis));
MIN = ;
dfs(,,LEVEL,LEVEL);
printf("%d\n",MIN);
}
return ;
}
还有一种方法就是枚举每一个点作为最大level进行dijkstra.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int INF = ;
const int N = ;
int m,n;
int MIN;
int graph[N][N];
struct Node{
int v,level;
}node[];
bool vis[N];
int low[N];
int dijkstra(){
int pos=;
for(int i=;i<=n;i++){
low[i] = node[i].v;
}
for(int i=;i<n;i++){
int MIN = INF;
for(int j=;j<=n;j++){
if(!vis[j]&&MIN>low[j]){
pos = j;
MIN = low[j];
}
}
vis[pos] = true;
for(int j=;j<=n;j++){
if(!vis[j]&&low[j]>low[pos]+graph[pos][j]){
low[j] = low[pos]+graph[pos][j];
}
}
}
return low[];
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
graph[i][j] = INF;
if(i==j) graph[i][j] = ;
}
}
for(int i=;i<=n;i++){
int num;
scanf("%d%d%d",&node[i].v,&node[i].level,&num);
graph[][i] = node[i].v;
for(int j=;j<=num;j++){
int a,b;
scanf("%d%d",&a,&b);
graph[a][i] = min(b,graph[a][i]);
}
}
int LEVEL;
MIN = INF;
for(int i=;i<=n;i++){
LEVEL = node[i].level;///设当前值的level 最大
memset(vis,,sizeof(vis));
for(int j=;j<=n;j++){
if(node[j].level>LEVEL||LEVEL-node[j].level>m) vis[j] = true;
}
MIN = min(MIN,dijkstra());
}
printf("%d\n",MIN);
}
return ;
}
hdu 1062(DFS||dijkstra)的更多相关文章
- HDU 5143 DFS
分别给出1,2,3,4 a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- hdu 1142(DFS+dijkstra)
#include<iostream> #include<cstdio> #include<cmath> #include<map> #include&l ...
- ACM: HDU 1869 六度分离-Dijkstra算法
HDU 1869六度分离 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descri ...
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- HDU 1062 Text Reverse(水题,字符串处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 解题报告:注意一行的末尾可能是空格,还有记得getchar()吃回车符. #include< ...
- hdu 4751(dfs染色)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...
- HDU 1045 (DFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...
- HDU 1241 (DFS搜索+染色)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...
随机推荐
- 201621044079《Java程序设计》第1周学习总结
1. 本周学习总结 首先要认识到java这门课程的重要性 了解java语言的发展历史 以及java的特点(*跨平台) 了解JDK JRE JVM的含义以及关系 JVM 是实现平台无关性的关键 学会独立 ...
- 安装elasticsearch-1.7.1及中文IK和近义词配置
安装elasticsearch及中文IK和近义词配置 https://www.cnblogs.com/yjf512/p/4789239.html 安装elasticsearch及中文IK和近义词配置 ...
- Charles 抓包发现自动跳转为https 问题梳理
今天遇到个有点意思的问题.特此记录. 业务场景: 做了一个页面,但是对外是挂载在京东主站上.如:www.jd.com/yifu/123456.html. 现场情况: 在本地/测试环境/预发环境中,每次 ...
- cdh版本的hadoop安装及配置(伪分布式模式) MapReduce配置 yarn配置
安装hadoop需要jdk依赖,我这里是用jdk8 jdk版本:jdk1.8.0_151 hadoop版本:hadoop-2.5.0-cdh5.3.6 hadoop下载地址:链接:https://pa ...
- 【BZOJ 4500 矩阵】
Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 390 Solved: 217[Submit][Status][Discuss] Description ...
- springMvc--请求的跳转和传值
springMvc--请求的跳转和传值 目录 forword跳转页面的三种方式 1.使用serlvet 2.使用Model对象 3.使用ModelAndView redirect跳转到页面 使用ser ...
- Spring源码解析-基于注解依赖注入
在spring2.5版本提供了注解的依赖注入功能,可以减少对xml配置. 主要使用的是 AnnotationConfigApplicationContext: 一个注解配置上下文 AutowiredA ...
- HDU 1556 线段树/树状数组/区间更新姿势 三种方法处理
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Codeforces 931.D Peculiar apple-tree
D. Peculiar apple-tree time limit per test 1 second memory limit per test 256 megabytes input standa ...
- wget下载HTTPS链接
wget -c -O master.zip --no-check-certificate https://github.com/mitsuhiko/flask/archive/master.zip # ...