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. git rev-list

    git-rev-list - Lists commit objects in reverse chronological order 按照时间顺序倒序排列的commit Update: If all ...

  2. nf_contrack_netlink.c

    /* Connection tracking via netlink socket. Allows for user space * protocol helpers and general trou ...

  3. 关于pragma pack的用法(一)

    一个很重要的参数#pragma pack(n) 数据边界对齐方式:以如下结构为例: struct {                    char a;                    WOR ...

  4. BZOJ3509: [CodeChef] COUNTARI

    3509: [CodeChef] COUNTARI Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 85[Submit][St ...

  5. I.MX6 gpio-keys driver hacking

    /**************************************************************************** * I.MX6 gpio-keys driv ...

  6. I.MX6 android 移除shutdown功能

    /************************************************************************ * I.MX6 android 移除shutdown ...

  7. Warning: Name is nonexistent or not a directory

    Every time I start up MATLAB, I receive this message: Warning: Name is nonexistent or not a director ...

  8. Javascript 知识点简介

    如何在HTML中引入JS? 所有重定向的HTML标签内都可以嵌入javascript代码. 浮点数不要用 == 来进行判断 var num=0;    for(var i=0;i<10;i++) ...

  9. 《深入Java虚拟机学习笔记》- 第14章 浮点运算

    <深入Java虚拟机学习笔记>- 第13章 浮点运算

  10. 【Python】使用python的tornado配合html页面示例

    背景:java写的非标加密算法,测试时执行java工程进行解密测试,很不方便. 目的:想写个web页面,使得任何测试人员都可以在输入加密串时得到解密后字段,方便日志查询及字段核对.(额,算法部分就不写 ...