题目描述:

由于自己“想得太多”,导致自己读了半天题才理解了题意。我还以为索敌值会随着每一次到达战略点而减小,结果题意是索敌值是固定了的,并不会改变。

如下是我对题目中第一个案例的分析:

每个圆圈代表一个战略点的编号,边上的值代表每两个战略点之间所需索敌值。

                  

开始时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的更多相关文章

  1. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  2. hdu 2199 Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. hdu 2199:Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. 二分搜索 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.注意会爆 ...

  5. hdu 1075 二分搜索

    还是写一下,二分搜索好了 这道题开数组比较坑... 二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环 先上AC代码 #include<iostream> #incl ...

  6. K Best(最大化平均数)_二分搜索

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

  7. HDU 2852 KiKi's K-Number(树状数组+二分搜索)

    题意:给出三种操作 0 e:将e放入容器中 1 e:将e从容器中删除,若不存在,则输出No Elment! 2 a k:搜索容器中比a大的第k个数,若不存在,则输出Not Find! 思路:树状数组+ ...

  8. nyoj914Yougth的最大化(二分搜索 + 贪心)

    Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗 ...

  9. 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 ...

随机推荐

  1. JQuery中的$.getScript()、$.getJson()和$.ajax()方法

    $.getScript() 有时候,在页面初次加载时就取得所需的全部JavaScript文件是完全没有必要的.虽然可以在需要哪个JavaScript文件时,动态地创建<script>标签, ...

  2. 跟踪OceanLotus的新下载程序KerrDown

    攻击的方法 两种方法将KerrDown下载器传递给目标.一个是使用带有恶意宏的Microsoft Office文档,另一个是包含带有DLL side-loading合法程序的RAR存档 .对于RAR存 ...

  3. Difference between plt.draw() and plt.show() in matplotlib

    Difference between plt.draw() and plt.show() in matplotlib down voteaccepted plt.show() will display ...

  4. Reverse Words in a String I & Reverse Words in a String II

    Reverse Words in a String I Given an input string, reverse the string word by word. For example,Give ...

  5. Two Sum I & II & III & IV

    Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...

  6. pytorch官网上两个例程

    caffe用起来太笨重了,最近转到pytorch,用起来实在不要太方便,上手也非常快,这里贴一下pytorch官网上的两个小例程,掌握一下它的用法: 例程一:利用nn  这个module构建网络,实现 ...

  7. Python3学习笔记07-List

    Python有6个序列的内置类型,但最常见的是列表和元 序列都可以进行的操作包括索引,切片,加,乘,检查成员. 此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法. 创建一个列表, ...

  8. C# 实现UDP打洞通信(一)

    最近研究了一下网络打洞的相关技术,TCP的方式据说可行性不高,各种困难,因此决定采用UDP(UDP是什么就不解释了)的方式. 原理: 我们都知道局域网内的主机想要访问外网的服务器是比较容易的,比如浏览 ...

  9. MPI 在Windows10 上安装,使用VS2013编译生成可执行程序

    原文地址:http://www.cnblogs.com/leijin0211/p/6851789.html 参考博客: http://www.cnblogs.com/shixiangwan/p/662 ...

  10. eclipse删除多余的工作空间

    window->preferences->startup and shutdown->workspaces如图: