题目链接

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+剪枝)的更多相关文章

  1. LA 6450 social advertising(dfs剪枝)

    6450 Social AdvertisingYou have decided to start up a new social networking company. Other existing ...

  2. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  4. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  5. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  6. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  7. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

随机推荐

  1. HDU-4952 Number Transformation

    http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/ ...

  2. mac下的改装人生——关于ssd

    这两天研究了很多关于ssd的东西,想想还是写下来把,毕竟花了这么多时间进去. 先说一下我自己的电脑把.前几天,因为嫌我的电脑是在是太卡了,准备来次升级,然后先买了个8g的内存装上,发现的确是没有死机的 ...

  3. [转]Ubuntu上的包管理:dpkg,apt和aptitude

    一直以来对于ubuntu的包管理的概念就是apt-get,偶尔手动装个包就是dpkg -i,现在觉得是要系统地了解一下这几个包管理的命令. 原文转自: http://zhouliang.pro/201 ...

  4. hdu 4432 Sum of divisors(十进制转其他进制)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4432 代码: #include<cstdio> #include<cstring&g ...

  5. poj 1050 To the Max (简单dp)

    题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...

  6. Skype的故事:几乎所有风投都想投 犯罪分子洗钱必备

    Skype的故事:几乎所有风投都想投 犯罪分子洗钱必备 转载自: http://news.chinaventure.com.cn/11/7/1381032922.shtml 今年是 Skype 网络电 ...

  7. 【Android - 进阶】之图片三级缓存的原理及实现

    在Android开发中,如果图片过多,而我们又没有对图片进行有效的缓存,就很容易导致OOM(Out Of Memory)错误.因此,图片的缓存是非常重要的,尤其是对图片非常多的应用.现在很多框架都做了 ...

  8. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  9. tcp ip参数详解

    http://www.cnblogs.com/digdeep/p/4869010.html 1. TCP/IP模型 我们一般知道OSI的网络参考模型是分为7层:“应表会传网数物”——应用层,表示层,会 ...

  10. 理解 Linux 网络栈(1):Linux 网络协议栈简单总结 图

    http://www.cnblogs.com/sammyliu/p/5225623.html