Aizu 2302 On or Off dfs/贪心
On or Off
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93265#problem/C
Description
Saving electricity is very important!
You are in the office represented as R \times C grid that consists of walls and rooms. It is guaranteed that, for any pair of rooms in the office, there exists exactly one route between the two rooms. It takes 1 unit of time for you to move to the next room (that is, the grid adjacent to the current room). Rooms are so dark that you need to switch on a light when you enter a room. When you leave the room, you can either leave the light on, or of course you can switch off the light. Each room keeps consuming electric power while the light is on.
Today you have a lot of tasks across the office. Tasks are given as a list of coordinates, and they need to be done in the specified order. To save electricity, you want to finish all the tasks with the minimal amount of electric power.
The problem is not so easy though, because you will consume electricity not only when light is on, but also when you switch on/off the light. Luckily, you know the cost of power consumption per unit time and also the cost to switch on/off the light for all the rooms in the office. Besides, you are so smart that you don't need any time to do the tasks themselves. So please figure out the optimal strategy to minimize the amount of electric power consumed.
After you finished all the tasks, please DO NOT leave the light on at any room. It's obviously wasting!
Input
The first line of the input contains three positive integers R (0 \lt R \leq 50), C (0 \lt C \leq 50) and M (2 \leq M \leq 1000). The followingR lines, which contain C characters each, describe the layout of the office. '.' describes a room and '#' describes a wall.
This is followed by three matrices with R rows, C columns each. Every elements of the matrices are positive integers. The (r, c) element in the first matrix describes the power consumption per unit of time for the room at the coordinate (r, c). The (r, c) element in the second matrix and the third matrix describe the cost to turn on the light and the cost to turn off the light, respectively, in the room at the coordinate (r, c).
Each of the last M lines contains two positive integers, which describe the coodinates of the room for you to do the task.
Note that you cannot do the i-th task if any of the j-th task (0 \leq j \leq i) is left undone.
Output
Print one integer that describes the minimal amount of electric power consumed when you finished all the tasks.
Sample Input
1 3 2
...
1 1 1
1 2 1
1 1 1
0 0
0 2
Sample Output
7
HINT
题意
给你一个r*c的矩阵,然后再告诉你每一个格子,开灯的花费,关灯的花费,灯每开一秒钟需要的花费
然后让你按着顺序去完成任务,问你最小花费是多少
题解:
首先,题意中,保证从一个任务到另外一个任务只有一条路,所以就是一个树形dp了其实
对于每个点,你都暴力出,经过这个点的时间,然后处理出来就好了
然后就贪心搞一搞……
@)1%KBO0HM418$J94$1R.jpg)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = + ;
int mat[maxn][maxn],n,m,q;
int on[maxn][maxn];
int off[maxn][maxn];
int t[maxn][maxn];
int ptr[maxn][maxn];
vector<int>PPP[maxn][maxn]; char s[maxn][maxn]; struct node
{
int x,y;
};
node mis[];
vector<node> Q; vector<node> P; int Flag=;
int dx[]={,-,,};
int dy[]={,,,-};
int vis[maxn][maxn];
void dfs(int x,int y,int xx,int yy)
{
if(Flag)return;
if(x==xx&&y==yy)
{
int stary = ; for(int i=;i<P.size();i++)
Q.push_back(P[i]);
Flag=;
return;
}
for(int i=;i<;i++)
{
int xxx = x+dx[i];
int yyy = y+dy[i];
if(xxx<=||xxx>n)continue;
if(yyy<=||yyy>m)continue;
if(vis[xxx][yyy])continue;
if(s[xxx][yyy]=='#')continue;
node ttt;ttt.x=xxx;ttt.y=yyy;
P.push_back(ttt);
vis[xxx][yyy]=;
dfs(xxx,yyy,xx,yy);
P.pop_back();
}
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=n;i++)
scanf("%s",s[i]+);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&t[i][j]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&on[i][j]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&off[i][j]); for(int i=;i<=q;i++)
{
scanf("%d%d",&mis[i].x,&mis[i].y);
mis[i].x++;mis[i].y++;
}
Q.push_back(mis[]);
for(int i=;i<q;i++)
{
memset(vis,,sizeof(vis));
Flag=;
P.clear();
vis[mis[i].x][mis[i].y]=;
node k;k.x=mis[i].x,k.y=mis[i].y;
P.push_back(k);
dfs(mis[i].x,mis[i].y,mis[i+].x,mis[i+].y);
}
for(int i = ; i < Q.size() ; ++ i)
{
node cur = Q[i];
int x = cur.x , y = cur.y;
PPP[x][y].push_back(i);
}
int ans = ;
for(int i = ; i <= n ; ++ i)
for(int j = ; j <= m ; ++ j)
if(PPP[i][j].size() != )
{
ans += on[i][j]; ans += off[i][j];
for(int z = ; z < PPP[i][j].size() - ; ++ z)
{
int dis = PPP[i][j][z+] - PPP[i][j][z];
ans += min(dis * t[i][j] , off[i][j] + on[i][j]);
}
}
printf("%d\n",ans);
return ;
}
Aizu 2302 On or Off dfs/贪心的更多相关文章
- 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心
题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...
- ICPC Asia Nanning 2017 I. Rake It In (DFS+贪心 或 对抗搜索+Alpha-Beta剪枝)
题目链接:Rake It In 比赛链接:ICPC Asia Nanning 2017 Description The designers have come up with a new simple ...
- 【NOIP2015】斗地主 题解(DFS+贪心)
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的AAA到KKK加上大小王的共545454张牌来进行的扑克牌游戏.在斗地主中,牌的大小关 系根据牌的数码表示如下: ...
- Aizu 0033 Ball(dfs,贪心)
日文题面...题意:是把一连串的有编号的球往左或者往右边放.问能不能两边都升序. 记录左边和右边最上面的球编号大小,没有就-1,dfs往能放的上面放. #include<bits/stdc++. ...
- NOIP2015斗地主[DFS 贪心]
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- NOIP2010引水入城[BFS DFS 贪心]
题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...
- [ZJOI2007]时态同步(dfs+贪心)
小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3.进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点,都存在且仅 ...
- codeforces 680D D. Bear and Tower of Cubes(dfs+贪心)
题目链接: D. Bear and Tower of Cubes time limit per test 2 seconds memory limit per test 256 megabytes i ...
- [CSP-S模拟测试]:虎(DFS+贪心)
题目传送门(内部题15) 输入格式 第一行一个整数$n$,代表点数接下来$n-1$行,每行三个数$x,y,z$,代表点$i$与$x$之间有一条边,若$y$为$0$代表初始为白色,否则为黑色,若$z$为 ...
随机推荐
- poj 2635 The Embarrassed Cryptographer(数论)
题目:http://poj.org/problem?id=2635 高精度求模 同余模定理. 题意: 给定一个大数K,K是两个大素数的乘积的值.再给定一个int内的数L 问这两个大素数中最小的一个是 ...
- poj 3267 The Cow Lexicon(dp)
题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...
- php 对象调用方法
static union _zend_function *zend_std_get_method(zval **object_ptr, char *method_name, int method_le ...
- HNOI2004打鼹鼠(LIS)
大水题…… 不过通过这题我们应该养成一个好习惯:好好看清题…… 竟然没有看到时限 10sec…… var i,j,n,m,ans:longint; f,time,x,y:..] of longint; ...
- 在ASP.NET中各种跳转控制
在ASP.NET中各种跳转控制 分类: 我的资料2012-03-16 15:01 76人阅读 评论(0) 收藏 举报 asp.netjavascripturlmenu Respose.Write(&q ...
- 用slf4j+logback实现多功能日志解决方案 --- 转
大家都知道,slf4j是原来log4j的作者写的一个新的日志组件,意思是简单日志门面接口,可以跟其他日志组件配合使用,常用的配合是slf4j+logback,无论从功能上还是从性能上都较之log4j有 ...
- openvpn文本验证模式配置
证书模式要为每个客户端生成一个证书,虽说安全性较好,但是比较麻烦,可以配置成用户名/密码的验证模式,这样就比较方便了,我这里用最简单的文本验证模式. 一.安装openvpn 不多说了,之前有篇文章已经 ...
- ExtJs 5.0需要注意的问题
1.在网上查找到的一些例子当中,存在new Ext.grid.ColumnModel()这样的操作,在5.0当中这是不允许的,在5.0当中这个已经被设置为私有方法,不允许用户调用,在5.0中我们不需要 ...
- 问题:关于坛友的一个js轮播效果的实现
需求:点击向前按钮进行向前翻页,向后按钮进行向后翻页,点击中间蓝色小圆圈可以来回自由切换 我的大概思路:先默认显示一个div 然后在原位置在隐藏一个div 给按钮添加click事件,转到下一个时 ...
- bzoj 3131 [Sdoi2013]淘金(数位DP+优先队列)
Description 小Z在玩一个叫做<淘金者>的游戏.游戏的世界是一个二维坐标.X轴.Y轴坐标范围均为1..N.初始的时候,所有的整数坐标点上均有一块金子,共N*N块. 一阵风吹 ...