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 log

    http://git-scm.com/book/zh/v2 https://backlogtool.com/git-guide/tw/contents/     http://gitbook.liuh ...

  2. The only legal comparisons are between two numbers, two strings, or two dates.

    The only legal comparisons are between two numbers, two strings, or two dates. Left  hand operand is ...

  3. [Unity3d]小地图的制作

    继续今天的学习心得,unity中小地图的制作,实现了小地图中红色小箭头代表场景中的主角,然后人物方向的转变,小地图中箭头也随之改变方向. 效果图     右上角就是小地图,上面有个红色小箭头就是代表主 ...

  4. usaco /the second wave

    bzoj4582:简单递推题. #include<cstdio> #include<cstring> #include<iostream> #include< ...

  5. 算法的时间复杂度(大O表示法)

    定义:如果一个问题的规模是n,解这一问题的某一算法所需要的时间为T(n),它是n的某一函数 T(n)称为这一算法的“时间复杂性”. 当输入量n逐渐加大时,时间复杂性的极限情形称为算法的“渐近时间复杂性 ...

  6. 【转】C,C++中使用可变参数

    可变参数即表示参数个数可以变化,可多可少,也表示参数的类型也可以变化,可以是 int,double还可以是char*,类,结构体等等.可变参数是实现printf(),sprintf()等函数的关键之处 ...

  7. 选择select框跳出信息

    <html > <body > <select type="select" name=s1 onChange=alert("你选择了&quo ...

  8. span文字在左背景图片在右

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  9. POJ 3107-Godfather(树形dp)

    题意: 有n个节点的树,删除一个点,得到的最大联通分支最小,求这样点的集合 分析: dp[i]表示删除i所得最大联通分支,遍历一遍节点即可,该题用vector会超时 #include <map& ...

  10. tomcat 容器中的UML架构图