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;
    }
  • 相关阅读:
    assign()与create()的区别
    ES6对象扩展——部分新的方法和属性
    ES6对象扩展——扩展运算符
    rest operater剩余操作符
    深拷贝和浅拷贝
    for in和for of的简单区别
    查询ES6兼容的网站
    ES6扩展——对象的扩展(简洁表示法与属性名表达式)
    滚动条设置样式
    marquee横向无缝滚动无js
  • 原文地址:https://www.cnblogs.com/fenice/p/5634156.html
Copyright © 2011-2022 走看看