【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)
题目链接:
http://codeforces.com/gym/100526
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11665&courseid=0
题目大意:
一个微波炉,有N个按钮,每个按钮可以让时间加或减一个数,问达到M至少需要按几次按钮。(N<=16,0<=M<=3600)
如果无法达到M输出比M大的最小的值需要按的次数和这个值与M的差值。注意微波炉的时间满足0<=当前时间<=3600。如果超过则取边界。
题目思路:
【最短路】
初始状态d[0]=0之后按N个按钮所加的值往下扩展。每次扩展d+1.最后找d[M]和M之后第一个出现的可行解。
SPFA或BFS即可。
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 24
#define M 3604
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int t;
int b[N];
int q[M],d[M];
bool u[M];
void spfa()
{
int i,now,to,l=,r=;
mem(d,0x7f);
q[]=;d[]=;
while(l!=r)
{
now=q[l=(l+)%M];
u[now]=;
for(i=;i<=n;i++)
{
to=now+b[i];
to=max(to,);
to=min(to,);
if(d[to]>d[now])
{
d[to]=d[now]+;
if(!u[to])
{
u[to]=;
q[r=(r+)%M]=to;
}
}
}
}
for(i=t;i<=;i++)
if(d[i]!=MAX)break;
printf("%d %d\n",d[i],i-t);
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,way;
for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
// while(~scanf("%d",&n))
{
scanf("%d%d",&n,&t);
for(i=;i<=n;i++)
{
scanf("%d",&b[i]);
}
spfa();
}
return ;
}
/*
// //
*/
【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)的更多相关文章
- 【线段树】BAPC2014 E Excellent Engineers (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【扩展欧几里得】BAPC2014 I Interesting Integers (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【宽搜】BAPC2014 J Jury Jeopardy (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【模拟】BAPC2014 G Growling Gears (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【最大流】BAPC2014 A Avoiding the Apocalypse (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
随机推荐
- asp.net mvc 实现记忆返回的功能
大体思路是在当前跳转链接追加一个参数memoryguid,以guid为key把查询query保存在cookie里,跳转的时候带走这个guid,回来的时候还带着,这样我们就能根据这个guid从cooki ...
- jetty运行maven程序(修改及时生效,不需要重启jetty程序)
jetty:run -Djetty:port=9999
- equals函数的作用
1.equals函数在什么地方 在Object类中,写法与==一样,但是我们用的时候要重写这个equals方法 String类型中的equals是复写好的 2.equals函数的作用 ==号在比较两个 ...
- C# - Sql数据类型的对应关系
<Language From="SQL" To="C#"> <Type From="bigint" To="lo ...
- java 对于url地址的实体符号的处理
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 <dependency> <g ...
- 【POJ2761】【fhq treap】A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- socket 编程基础
一.Socket简介 Socket是进程通讯的一种方式,即调用这个网络库的一些API函数实现分布在不同主机的相关进程之间的数据交换. 几个定义: (1)IP地址:即依照TCP/IP协议分配给本地主机的 ...
- 子元素的margin-top影响父元素原因和解决办法
这个问题会出现在所有浏览器当中,原因是css2.1盒子模型中规定, In this specification, the expression collapsing margins means tha ...
- 关于javascript输出中文乱码的问题
今天找到一个引导效果.原来是用英文进行引导.但是我改了里面的英文为汉字就出现乱码的情况.英文提示是在js页面里面完成的.所以最后的解决办法 就是把js文件用记事本打开,然后把文件另存为utf-8的格式 ...
- js touch触屏原理分析
之前我们做过许多触屏的特效,那么,今天,我们来分析下js的触屏原理.事实上,大家百度一下js touch基本上可以找到这文章“指尖下的js ——多触式web前端开发之一:对于Touch的处理”,我想这 ...