题目链接:

  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)的更多相关文章

  1. 【线段树】BAPC2014 E Excellent Engineers (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  2. 【扩展欧几里得】BAPC2014 I Interesting Integers (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  3. 【宽搜】BAPC2014 J Jury Jeopardy (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  4. 【模拟】BAPC2014 G Growling Gears (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  5. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  6. 【最大流】BAPC2014 A Avoiding the Apocalypse (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  7. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  8. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  9. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

随机推荐

  1. python s12 day2

    python s12 day2   入门知识拾遗 http://www.cnblogs.com/wupeiqi/articles/4906230.html 基本数据类型 注:查看对象相关成员 var, ...

  2. Weex 初始

    1.一旦数据和模板绑定,数据的变化会立即体现在前台的变化 <template> <container> <text style="font-size: {{si ...

  3. 认识k_BackingField【转】

    事情从Json的序列化和反序列化说起. 在C#2.0的项目中,以前经常使用Json.Net实现序列化和反序列化.后来从c#3.0中开始使用新增的DataContractJsonSerializer进行 ...

  4. angularjs kindEditor 中content获得不到值

    angularjs kindEditor 中content获得不到值 需要修改下angular-kindeditor.js angular-kindeditor.js if (KindEditor) ...

  5. 序列化- 使用BinaryFormatter进行序列化

    可以使用属性(Attribute)将类的元素标为可序列化的(Serializable)和不可被序列化的(NonSerialized)..NET中有两个类实现了IFormatter借口的类中的Seria ...

  6. Missing artifact com.sun:tools:jar:1.5.0

    http://java-suddy.iteye.com/blog/1821871(解决了我的问题)

  7. VisualStudio2013内置SQLServer入门

    最近做项目老大要求用到sqlserver,但是这项目的数据库只是本地演示用并不复杂,于是决定试试VisualStudio2013内置的SQLServer.对于这个东西的了解并没有多少,然后项目初学习的 ...

  8. Using GUID to generate the unique file name in C#

    GUID, the abbreviation of "Global Unique Identifier", is a unique reference number used as ...

  9. Java hashCode 和 equals()

    1 Object中定义的hashCode() public int hashCode() Returns a hash code value for the object. This method i ...

  10. OpenCV中Mat的详解

    每次碰到Mat都得反复查具体的用法,网上的基础讲解不多,难得看到一篇,赶快转来收藏~ 原文地址:http://www.opencvchina.com/thread-1039-1-1.html 目标 我 ...