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 ...
随机推荐
- [转]dwr3框架学习笔记--简介及原理简介
1.DWR简介 DWR(直接web远程访问),DWR是一个Java库,使服务器上的Java和JavaScript的浏览器进行交互和相互调用尽可能简单. DWR 是一个可以允许你去创建 AJAX WEB ...
- 关于debian配置的问题汇总
debian的apache多域名配置: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-ho ...
- 代码托管平台(Git)
1,可以说GitHub的出现完全颠覆了以往大家对代码托管网站的认识.GitHub不但是一个代码托管网站,更是一个程序员的SNS社区.GitHub真正 迷人的是它的创新能力与Geek精神,这些都是无法模 ...
- springMVC js等文件找不到解决方法
<mvc:resources mapping="/javascript/**" location="/static_resources/javascript/&qu ...
- Storm ui 显示异常
今天安装storm集群的时候,各个进程也都起来,却发现Storm ui界面下无法观察Storm集群的状态 有很多地方处理不当都会造成这种现象: 1.storm.yaml配置不当 2.防火墙的问题 3. ...
- php数据缓存到文件类设计
// 自定义缓存类 class Cache_Filesystem { // 缓存写保存 function set ($key, $data, $ttl) { //打开文件为读/写模式 $h = fop ...
- JDBC连接数据库的过程
以连接MySQL为例: (1)加载MySQL数据库连接的驱动程序.到MySQL官网下载该驱动程序jar包,然后把包复制到WEB-INF/lib目录下,则JDBC会调用Class.forName()方法 ...
- IntellIJ IDEA 配置 Git,顺带解决Git Push rejected问题
1.下载便携版本git https://git-scm.com/download/win 弹出的下载取消,重新选择 2.解压自压缩文件. 3.配置IDEA 4.测试 5.配置终端环境shell为bas ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- JavaScript渐变效果的实现
鼠标移上去透明度渐渐增加,鼠标移出,透明度渐渐减小. 关键代码: view source print? 1 var speed = 0; 2 if(target>obj.alpha){ 3 ...