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$为 ...
随机推荐
- 254 shades of grey
254 shades of grey Description: Why would we want to stop to only 50 shades of grey? Let's see to ho ...
- Factorial
Factorial 计算阶乘 In mathematics, the factorial of a non-negative integer n, denoted by n!, is the pro ...
- linux bash脚本把A和B文件中有相同ID的B文件的内容输出到文件C
bash脚本把A和B文件中有相同ID的B文件的内容输出到文件C. Aid文件:ID001.1ID032.1ID090.10 Bfilt文件:XX XX XXX ID001.1 XXX999999999 ...
- 转: 解决MSYS2下的中文乱码问题
解决方案 新建/usr/bin/win: 12 #!/bin/bash$@ |iconv -f gbk -t utf-8 新建/etc/profile.d/alias.sh: 12345678 ali ...
- poj 1850 code(组合数学)
题目:http://poj.org/problem?id=1850 题意:按给定的规则给字母编号. 一个很简单的题目,但是却做了好久.................................. ...
- 西南科技大学第十届ACM程序设计竞赛题解
A.德州扑克 B. 我恨11(1089) 问题描述 11是一个孤独的数字,小明十分讨厌这个数字,因此如果哪个数字中出现了11或者该数字是11的倍数,他同样讨厌这个数字.现在问题来了,在闭区间[L,R] ...
- iOS application: how to clear notifications?
http://stackoverflow.com/questions/8682051/ios-application-how-to-clear-notifications up vote105down ...
- 【转】Eclipse配置Struts2问题:ClassNotFoundException: org...dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
我的解决方案 一开始,我是依照某本教材,配置了User Libraries(名为struts-2.2.3, 可供多个项目多次使用), 然后直接把struts-2.2.3引入过来(这个包不会真正的放在项 ...
- 从ramdisk根文件系统启动Linux成功
这几天参考国嵌的实验手册和网上的资料完成了u-boot定制.内核定制.ramdisk根文件系统的制作,并成功.趁热打铁,总结一下.本文引用了很多网络上的文章,就不一一注明了.感谢各大侠的帮助,如有雷同 ...
- Entity Framework中查看生成的SQL语句
Entity Framework 4.0 中是这样的,高版本的跟这个有些差异,不太一样,貌似已经到7了 using (Entities entities = new Entities()) { var ...