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了其实

对于每个点,你都暴力出,经过这个点的时间,然后处理出来就好了

然后就贪心搞一搞……

代码:

#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/贪心的更多相关文章

  1. 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心

    题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...

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

  3. 【NOIP2015】斗地主 题解(DFS+贪心)

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的AAA到KKK加上大小王的共545454张牌来进行的扑克牌游戏.在斗地主中,牌的大小关 系根据牌的数码表示如下: ...

  4. Aizu 0033 Ball(dfs,贪心)

    日文题面...题意:是把一连串的有编号的球往左或者往右边放.问能不能两边都升序. 记录左边和右边最上面的球编号大小,没有就-1,dfs往能放的上面放. #include<bits/stdc++. ...

  5. NOIP2015斗地主[DFS 贪心]

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  6. NOIP2010引水入城[BFS DFS 贪心]

    题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...

  7. [ZJOI2007]时态同步(dfs+贪心)

    小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3.进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点,都存在且仅 ...

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

  9. [CSP-S模拟测试]:虎(DFS+贪心)

    题目传送门(内部题15) 输入格式 第一行一个整数$n$,代表点数接下来$n-1$行,每行三个数$x,y,z$,代表点$i$与$x$之间有一条边,若$y$为$0$代表初始为白色,否则为黑色,若$z$为 ...

随机推荐

  1. 十个免费的Web压力测试工具

    两天,jnj在本站发布了<如何在低速率网络中测试 Web 应用>,那是测试网络不好的情况.而下面是十个免费的可以用来进行Web的负载/压力测试的工具,这样,你就可以知道你的服务器以及你的W ...

  2. HDU 1533 Going Home (最小费用流)

    题意: 一个矩阵n*m,其中有k个房子和k个人,k个人分别必须走到任意一个房子中(匹配),但是权值就是长度(非欧拉距离),求匹配完的权之和. 思路: 建图方法是,首先将k个人和k个房子分别抽出来到集合 ...

  3. 【转】protobuf2.5.0在<delete [] elements_;>crash的问题。

    背景 项目中使用protobuf作为网络传输协议,最开始在项目中直接使用源代码编译,在真机上测试一直是正常的,直到某天开始在 CPU是64 bit的设备上发现protobuf导致crash了,于是就开 ...

  4. StrutsPrepareAndExecuteFilter的作用

    FilterDispatcher是早期struts2的过滤器,后期的都用StrutsPrepareAndExecuteFilter了,如 2.1.6.2.1.8.StrutsPrepareAndExe ...

  5. C#日期时间格式化

    日期转化一为了达到不同的显示效果有时,我们需要对时间进行转化,默认格式为:2007-01-03 14:33:34 ,要转化为其他格式,要用到DateTime.ToString的方法(String, I ...

  6. 如何使用UDP进行跨网段广播

    广播域首先我们来了解一下广播域的概念.广播域是网络中能接收任一台主机发出的广播帧的所有主机集合.也就是说,如果广播域内的其中一台主机发出一个广播帧,同一广播域内所有的其它主机都可以收到该广播帧.广播域 ...

  7. mkimage的-a 和 –c参数和内核引导

    目录 一.mkimage工具简介二.-a参数与-e参数和内核引导的关系三.实例测试 3.1 -a参数与-e参数相同,可以将内核下载到SDRAM的任何地址,然后从这启动 3.2 -a参数与-e参数不同, ...

  8. java 多线程下载

    import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTool { /** * 把一 ...

  9. android xml文件

    一.布局文件:在layout目录下,使用比较广泛: 我们可以为应用定义两套或多套布局,例如:可以新建目录layout_land(代表手机横屏布局),layout_port(代表手机竖屏布局),系统会根 ...

  10. 【译】 AWK教程指南 4通过文本内容和对比选择指定的记录

    Pattern { Action }为awk中最主要的语法.若某Pattern的值为真则执行它后面的 Action. awk中常使用"关系表达式" (Relational Expr ...