zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    去掉DevExpress gridControl控件表头上的的默认英文字母
    【555】folium 更换底图
    github图片不显示问题解决
    厉害了,自己动手实现 LRU 缓存机制!
    一文搞懂 Netty 的整体流程,还有谁不会?
    PageHelper 分页一直有性能问题?
    left join 后用 on 还是 where,区别大了!
    不推荐别的了,IDEA 自带的数据库工具就很牛逼!
    王炸!!IDEA 2021.1 推出语音、视频功能,边写代码边聊天,我真的服了…
    别再面向 for 循环编程了,Spring 自带的观察者模式就很香!
  • 原文地址:https://www.cnblogs.com/fenice/p/5634156.html
Copyright © 2011-2022 走看看