二分搜索-HihoCoder1139
题目描述:


由于自己“想得太多”,导致自己读了半天题才理解了题意。我还以为索敌值会随着每一次到达战略点而减小,结果题意是索敌值是固定了的,并不会改变。
如下是我对题目中第一个案例的分析:
每个圆圈代表一个战略点的编号,边上的值代表每两个战略点之间所需索敌值。

开始时Nettle在1,他只需要3个索敌值就能到达5了,如图:从战略点1到达战略点5只需要 3 个索敌值,因此答案 3 满足条件,再从战略点2到战略点5只需要 2 个索敌值,答案3大于2,因此答案 3 满足条件。所以,Nettle需要的最少索敌值为3,经1---->2---->5路线,可以消灭boss。
代码实现:
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXN = 1e6;
const int INF = +;
int N,M,K,T;
struct edge{
int from;//源点
int to;//目标点
int val;//锁敌值
}; struct node{
int to;//目标点
int val;//锁敌值
};
edge E[MAXN*];///所有的边
edge now[MAXN*];///满足k>=val的边
vector<node> G[MAXN];///满足k>=val的边对应的点
bool used[MAXN];
typedef pair<int,int> P;
queue<P> q;
int cmp(edge a, edge b){
return a.val<b.val;//从小到大排序
} bool bfs(int num,int k){///广搜从1到BOSS点的路径
while(!q.empty()){
q.pop();
}
q.push(P(num,k));
used[num]=true;
while(!q.empty()){
P top = q.front();
q.pop();
if(top.first==T){
return true;
}
for(int i=;i<G[top.first].size();i++){
if(!used[G[top.first][i].to]){
used[G[top.first][i].to]=true;
if(top.second>0){///保证索敌值始终大于零,一直广搜总会找到一个合适的索敌值
q.push(P(G[top.first][i].to,top.second-));///每次执行后使索敌值-1
}
}
}
}
return false;
} bool C(int x){
int cnt=;
memset(used,,sizeof(used));
for(int i=;i<MAXN;i++){
G[i].clear();///满足val>=k的边对应的点
}
///满足锁敌值为x的边存在now数组里面
for(int i=;i<*M;i++){
if(E[i].val<=x){///如果这条边的索敌值比答案x的索敌值小,就放入now数组里面
now[cnt++]=E[i];
}
}
for(int i=;i<cnt;i++){
G[now[i].from].push_back((node){now[i].to,now[i].val});
}
if(bfs(,K)){///广搜从1到BOSS点的路径
return true;
}
return false;
} void solve(){
int lb=,ub=INF;
while(ub-lb>){
int mid=(ub+lb)/;
///满足锁敌值为x,且能够找到1点到BOSS点的路径
if(C(mid)){
ub=mid;
}else{
lb=mid;
}
}
printf("%d\n",ub); } int main(){
while(~scanf("%d%d%d%d",&N,&M,&K,&T)){
for(int i=;i<M;i++){
int a,b,val;
scanf("%d%d%d",&a,&b,&val);
E[i].from=a;E[i].to=b;E[i].val=val;///记录每条边的起始和所需索敌值
E[i+M].from=b;E[i+M].to=a;E[i+M].val=val;///将其存为有向图
}
sort(E,E+*M,cmp);
solve();
}
return ;
}
二分搜索-HihoCoder1139的更多相关文章
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- hdu 2199 Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2199:Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 二分搜索 UVALive 6076 Yukari's Birthday (12长春K)
题目传送门 题意:问使得sum (k^i) = n || n -1 (1 <= i <= r) 的min (r*k)组合的r和k 分析:r的最大不会超过40,枚举r,二分搜索k.注意会爆 ...
- hdu 1075 二分搜索
还是写一下,二分搜索好了 这道题开数组比较坑... 二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环 先上AC代码 #include<iostream> #incl ...
- K Best(最大化平均数)_二分搜索
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
- HDU 2852 KiKi's K-Number(树状数组+二分搜索)
题意:给出三种操作 0 e:将e放入容器中 1 e:将e从容器中删除,若不存在,则输出No Elment! 2 a k:搜索容器中比a大的第k个数,若不存在,则输出Not Find! 思路:树状数组+ ...
- nyoj914Yougth的最大化(二分搜索 + 贪心)
Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗 ...
- poj 2976 Dropping tests (二分搜索之最大化平均值之01分数规划)
Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...
随机推荐
- Error while executing topic command : Replication factor: 2 larger than available brokers: 0.
[root@hdp1 /mnt/software/maxwell-1.19.4]#kafka-topics.sh --zookeeper hdp1,hdp2,hdp3:2181 --create -- ...
- day3 进入指定目录:cd
想进入指定目录使用cd cd 目录名 进入指定目录 进入系统根目录:cd / 回退命令:cd .. 或者 cd ../ 或者 cd ..// 进入当前用户的主目录:cd 或者 cd ~ 跳转指定目录: ...
- 解决Centos下yum无法更新
问题: http://mirrors.cloud.aliyuncs.com/epel/6/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - ...
- SpringBoot整合Jdbc
(1).添加相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...
- IP分片丢失重传 - Sacrifice的日志 - 网易博客
尽管IP分片看起来是是透明的,但有一点让人不想使用它:即使只丢失一片数据也要重传整个数据报.为什么会发生这种情况呢? 因为IP层本身没有超时重传的机制--由更高层来负责超时和重传(TC ...
- 『转载』hadoop 1.X到2.X的变化
表1新旧hadoop脚本/变量/位置变化表 改变项 原框架中 新框架中(Yarn) 备注 配置文件位置 ${hadoop_home_dir}/conf ${hadoop_home_dir}/etc/h ...
- jenkins cobertura单元测试
1.1 Maven 工程 pom.xml 修改 1.2 Build添加插件目标 此时构建项目,会在项目 targer/site/cobertura 目录中生成 html 与 xml ...
- 使用命令行登陆数据库配置文件修改 解决ora12528
下面是问题解决: ORA-12528: TNS:listener: all appropriate instances are blocking new connections 1:修改listene ...
- discuz3.4:在Centos6.5中安装过程
参考文章:https://www.cnblogs.com/hehongbin/articles/5741270.html https://www.cnblogs.com/mitang/p/552454 ...
- Stanford CS231n - Convolutional Neural Networks for Visual Recognition
网易云课堂上有汉化的视频:http://study.163.com/course/courseLearn.htm?courseId=1003223001#/learn/video?lessonId=1 ...