题意:给你一些N个点,M条边,走每条边要花费金钱,然后给出其中必须访问的点,在这些点可以打工,但是需要先拿到证书,只可以打一次,也可以选择不打工之直接经过它。一个人从1号点出发,给出初始金钱,问你能不能访问所以的点,并且获得所以证书。

题解:目标是那些一定要访问的点,怎么到达的我们不关心,但是我们关心花费最少的路径,而且到达那个点后是一定要打工的,如果只是经过,那么在求花费最少的路径的时候已经考虑过了。

因此先用Folyd求出各个点直接的最短路径,由于N很小,又只要求出一个解,所以直接dfs暴搜就行了。

M很大,可能有重边。注意处理。

当时一看,图论题,没仔细想就跳过了。下面的代码加了输入挂,因为第一遍写T了,以为是输入问题,所以加上了。改了我好几个小时没过,重写一遍就过了。

#define HDU
#ifndef HDU
#include<bits/stdc++.h>
#else
//pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
//#include<iostream>
#endif // HDU
#define mins(s,x) if((x)<(s)) s = x
#define maxs(s,x) if((x)>(s)) s = x
using namespace std;
typedef long double ld;
typedef long long ll; const int maxn = ;
const int maxh = ;
const int INF = 0x3f3f3f3f; int N,M,Mon,H;
int dis[maxn][maxn];
inline void scan_d(int &ret)
{
char c;ret=;
while((c=getchar())<''||c>'');
while(c>=''&&c<='') ret=ret*+(c-''),c=getchar();
} struct City
{
int cost,earn,id;
void input(){
scanf("%d%d%d",&id,&earn,&cost);
}
}city[maxh]; bool vis[maxh]; bool dfs(int u,int money,int cnt)
{
if( cnt == H && money >= dis[u][] )return true;
for(int i = ; i < H; i++) if(!vis[i]){
int v = city[i].id;
if( money >= dis[u][v]+city[i].cost ){
vis[i] = ;
if(dfs(v,money-dis[u][v]-city[i].cost+city[i].earn,cnt+)) return true;
vis[i] = ;
}
}
return false;
} void init()
{
memset(vis,false,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
for(int i = ; i <= N; i++)
dis[i][i]= ;
} //#define local int main()
{
#ifdef local
freopen("in.txt","r",stdin);
// freopen("myout.txt","w",stdout);
#endif // local
int T;
scan_d(T);
//scanf("%d",&T);
while(T--){
scan_d(N);scan_d(M);scan_d(Mon);
//scanf("%d%d%d",&N,&M,&Mon);
init();
for(int i = ; i < M; i++){
int u,v,w;
scan_d(u);scan_d(v);scan_d(w);
//scanf("%d%d%d",&u,&v,&w);
//if(u == v) continue;
if(dis[u][v]>w) {
dis[u][v] = w;
dis[v][u] = dis[u][v];
} } for(int k = ; k <= N; k++)
for(int i = ; i <= N; i++){
if(dis[i][k]<INF)
for(int j = ; j <= N; j++) {
mins(dis[i][j],dis[i][k]+dis[k][j]);
}
} scanf("%d",&H);
for(int i = ; i < H; i++){
city[i].input();
} printf("%s\n",dfs(,Mon,)?"YES":"NO");
} return ;
}

HDU 4284 Travel (Folyd预处理+dfs暴搜)的更多相关文章

  1. HDU 4277 USACO ORZ(DFS暴搜+set去重)

    原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...

  2. hdu 4277 USACO ORZ (dfs暴搜+hash)

    题目大意:有N个木棒,相互组合拼接,能组成多少种不同的三角形. 思路:假设c>=b>=a 然后枚举C,在C的dfs里嵌套枚举B的DFS. #include <iostream> ...

  3. HDU 4284 Travel

    据说是TSP经典问题...可以用状态压缩做.但是看到数据量,就厚着脸皮上搜索了...先floyd预处理每对点间的最小消费,然后只考虑要去的城市就可以了,这样的话城市数最多16个...当时就暴搜了... ...

  4. Usaco 2.3 Zero Sums(回溯DFS)--暴搜

    Zero SumConsider the sequence of digits from 1 through N (where N=9) in increasing order: 1 2 3 ... ...

  5. hdu4848 DFS 暴搜+ 强剪枝

    题意:       给你一个图,然后问你从1出发遍历所有的点的距离和是多少,这里的距离和是每一个点到1的距离的总和,不是选择一条遍历所有点的路径的总长度,时间限制是 8000ms. 思路:       ...

  6. [HDU 5135] Little Zu Chongzhi's Triangles (dfs暴搜)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135 题目大意:给你n条边,选出若干条边,组成若干个三角形,使得面积和最大.输出最大的面积和. 先将边 ...

  7. hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. HDU - 4284 Travel(floyd+状压dp)

    Travel PP loves travel. Her dream is to travel around country A which consists of N cities and M roa ...

  9. hdu 4284 Travel(floyd + TSP)

    虽然题中有n<=100个点,但实际上你必须走过的点只有H<=15个.而且经过任意点但不消耗C[i]跟D[i]可以为无限次,所以可以floyd预处理出H个点的最短路,之后剩下的...就成了裸 ...

随机推荐

  1. C# 中的迭代器 yield关键字 提高性能和可读性

    展示一个例子 IList<string> FindBobs(IEnumerable<string> names) { var bobs = new List<string ...

  2. HDU - 5015 233 Matrix(杨辉三角/前缀+矩阵快速幂)

    233 Matrix In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23 ...

  3. framework资源文件读取

    1.在framework里面读framwork自己的资源文件 这是framework内部的资源,跟其他都没有关系.但是framework不能单独存在,必须要放在某个“主程序”中才能起作用.bundle ...

  4. NHibernate 打不开工厂有可能是这几个原因

    1. 属性必须虚拟化. 2.属性必须要有Id 字段 3.数据库必须要是创建好的数据库.

  5. codeforces 352D - Jeff and Furik【期望dp】

    首先恋人操作过一轮之后逆序对不会变多,所以设f[i]为把i个逆序对消掉的期望次数,f[i]=0.5f[i-2]+0.5f[i]+2,化简然后递推即可 #include<iostream> ...

  6. ICPC 2016 China Final J. Mr.Panda and TubeMaster【最大费用最大流】

    有一种限制下界强制选的,但是也可以不用 把每个格点拆成两个,一个连s一个连t,对于不是必选的连中间连流量1费用0边表示不选,然后黑白染色,黑点连横着白点连竖着,边权就是这条水管的权值,然后跑最大费用最 ...

  7. acwing 3 完全背包

    习题地址 https://www.acwing.com/problem/content/description/3/ 题目描述有 N 种物品和一个容量是 V 的背包,每种物品都有无限件可用. 第 i ...

  8. jsf+ejb

    jsf+ejb 示例 http://docs.jboss.org/jbossas/docs/Installation_And_Getting_Started_Guide/5/html/Sample_J ...

  9. 黑马MyBatis入门day1

    package com.itheima.domain; /* CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username ...

  10. block size大小

    1.用tune2fs查看block size大小: 1 2 tune2fs -l /dev/sda1 |grep "Block size" Block size: 1024 2.用 ...