• zoj 3620 Escape Time II dfs


    题目链接:

    题目

    Escape Time II
    Time Limit: 20 Sec
    Memory Limit: 256 MB

    问题描述

    There is a fire in LTR ’ s home again. The fire can destroy all the things in t seconds, so LTR has to escape in t seconds. But there are some jewels in LTR ’ s rooms, LTR love jewels very much so he wants to take his jewels as many as possible before he goes to the exit. Assume that the ith room has ji jewels. At the beginning LTR is in room s, and the exit is in room e.

    Your job is to find a way that LTR can go to the exit in time and take his jewels as many as possible.

    输入

    There are multiple test cases.
    For each test case:
    The 1st line contains 3 integers n (2 ≤ n ≤ 10), m, t (1 ≤ t ≤ 1000000) indicating the number of rooms, the number of edges between rooms and the escape time.
    The 2nd line contains 2 integers s and e, indicating the starting room and the exit.
    The 3rd line contains n integers, the ith interger ji (1 ≤ ji ≤ 1000000) indicating the number of jewels in the ith room.
    The next m lines, every line contains 3 integers a, b, c, indicating that there is a way between room a and room b and it will take c (1 ≤ c ≤ t) seconds.

    输出

    For each test cases, you should print one line contains one integer the maximum number of jewels that LTR can take. If LTR can not reach the exit in time then output 0 instead.

    样例

    input
    3 3 5
    0 2
    10 10 10
    0 1 1
    0 2 2
    1 2 3
    5 7 9
    0 3
    10 20 20 30 20
    0 1 2
    1 3 5
    0 3 3
    2 3 2
    1 2 5
    1 4 4
    3 4 2

    output
    30
    80

    题意

    给你一个无向图,问在规定时间内从起点走到终点能带走的最多珠宝。

    题解

    n才10,直接暴搜。

    代码

    zoj崩了,代码还没提交,先放着吧orz

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 22;
    const int INF = 0x3f3f3f3f;
    
    int G[maxn][maxn];
    int vis[maxn],val[maxn];
    int n, m, k,st,ed;
    int ans;
    
    void dfs(int u,int d,int cnt) {
    	if (d > k) return;
    	if (u == ed) ans = max(ans, cnt);
    	for (int i = 0; i < n; i++) {
    		if (i == u) continue;
    		int t = val[i]; val[i] = 0;
    		dfs(i, d + G[u][i],cnt+t);
    		val[i] = t;
    	}
    }
    
    void init() {
    	memset(vis, 0, sizeof(vis));
    	memset(G, INF, sizeof(G));
    }
    
    int main() {
    	while (scanf("%d%d%d", &n, &m, &k) == 3 && n) {
    		init();
    		scanf("%d%d", &st, &ed);
    		for (int i = 0; i < n; i++) scanf("%d", &val[i]);
    		while (m--) {
    			int u, v,w;
    			scanf("%d%d%d", &u, &v, &w);
    			G[u][v] = G[v][u] = min(G[u][v], w);
    		}
    		ans = 0;
    		vis[st] = 1;
    		int t = val[st]; val[st] = 0;
    		dfs(st,0,t);
    		printf("%d
    ", ans);
    	}
    	return 0;
    }
  • 相关阅读:
    javascript数据结构与算法---二叉树(查找最小值、最大值、给定值)
    javascript数据结构与算法---检索算法(二分查找法、计算重复次数)
    javascript数据结构与算法---检索算法(顺序查找、最大最小值、自组织查询)
    javascript数据结构与算法--高级排序算法(快速排序法,希尔排序法)
    ThinkPHP5中Session的使用
    能让你少写1000行代码的20个正则表达式
    composer 安装
    thinkphp5.0 实现图片验证效果且能点击图片刷新图片
    thinkphp5.0 实现单文件上传功能
    thinkphp5.0 输入变量
  • 原文地址:https://www.cnblogs.com/fenice/p/5634156.html
Copyright © 2020-2023  润新知