LA 6476 Outpost Navigation (DFS+剪枝)
Solution
DFS+剪枝
对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走.
记录经过每个点的最大弹药数,对dfs进行剪枝.
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
map<string, int> pos;
struct Edge {
int v, c, next;
} edge[];
int head[], cnt;
int vis[], amm[], aim[],sum[];
int cs, n, m, ans;
string s, t; void addedge (int u, int v, int c) {
edge[++cnt].v = v, edge[cnt].c = c;
edge[cnt].next = head[u];
head[u] = cnt;
}
void dfs (int x, int s, int k) {
if(k>=ans) return;
if (aim[x]) {
ans = min (ans, k);
return ;
}
if (!vis[x]) s += amm[x], vis[x] = ;
if(vis[x]&&sum[x]>=s) return;
sum[x]=max(sum[x],s);
for (int i = head[x]; i; i = edge[i].next) {
int v = edge[i].v, c = edge[i].c;
if (s >= c) dfs (v, s - c, k + c);
}
vis[x] = ;
}
int main() {
scanf ("%d", &cs);
while (cs--) {
pos.clear();
memset (head, , sizeof head);
memset(sum,,sizeof sum);
memset(vis,,sizeof vis);
cnt = , ans = 0x7fffffff;
scanf ("%d %d", &n, &m);
for (int i = ; i <= n; i++) {
cin >> s;
pos[s] = i;
cin >> amm[i] >> s;
if (s[] == 'y') aim[i] = ;
else aim[i] = ;
}
for (int i = , x; i <= m; i++) {
cin >> s >> t >> x;
int u = pos[s], v = pos[t];
addedge (u, v, x);
addedge (v, u, x);
}
dfs (, , );
if (ans != 0x7fffffff) printf ("%d\n", ans);
else puts ("No safe path");
}
}
LA 6476 Outpost Navigation (DFS+剪枝)的更多相关文章
- LA 6450 social advertising(dfs剪枝)
6450 Social AdvertisingYou have decided to start up a new social networking company. Other existing ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
随机推荐
- java 去掉字符串右侧空格
public static String rightTrim(String str) { String regex = "(.*\\S+)(\\s+$)"; Patte ...
- 如何实现View上添加标签
效果图: 利用 https://github.com/linger1216/labelview 类库来实现 具体代码 问度娘. {LabelView label = new LabelView(thi ...
- 使用Struts2开发Java Web应用程序(目录)
连接地址 http://blog.csdn.net/struts2/article/details/1721752
- Delphi 编写的Web Service
一编写服务程序 第一步:File----->New----->Other------>WebServices----->Soap Server Application选择I ...
- HDOJ/HDU 2561 第二小整数(水题~排序~)
Problem Description 求n个整数中倒数第二小的数. 每一个整数都独立看成一个数,比如,有三个数分别是1,1,3,那么,第二小的数就是1. Input 输入包含多组测试数据. 输入的第 ...
- 折腾iPhone的生活——5s使用的各种小技巧
电池: iPhone电池正常充电方法:随便充,想怎么充怎么充,想充就充,想停就停. 需要注意: 1.如果要闲置3个月以上,一定要冲到40%电以后再闲置 2.不要在-40~35度使用(应该是废话) 3. ...
- SQL数据类型介绍
在计算机中数据有两种特征:类型和长度.所谓数据类型就是以数据的表现方式和存储方式来划分的数据的种类. 在SQL Server 中每个变量.参数.表达式等都有数据类型.系统提供的数据类型分为几大类 ...
- 传统IO与NIO区别二
nio是new io的简称,从jdk1.4就被引入了.现在的jdk已经到了1.6了,可以说不是什么新东西了.但其中的一些思想值得我来研究.这两天,我研究了下其中的套接字部分,有一些心得,在此分享. ...
- Struts2配置文件讲解
解决在断网环境下,配置文件无提示的问题我们可以看到Struts.xml在断网的情况下,前面有一个叹号,这时,我们按alt+/ 没有提示,这是因为” http://struts.apache.org/d ...
- Myeclipse2013 SVN安装方法
1. 打开Help下的Install from Site 2. 弹出窗口,如下图: 3. 点击Add标签,如图: 在对话框Name输入Svn, URL中输入:http://subclipse.tigr ...