zoj 3620 Escape Time II dfs

Escape Time II

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3620

Description

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.

Input

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 ≤ ct) seconds.

Output

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.

Sample 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

Sample Output

30
80

HINT

题意

给你个无向边带权图,让你在时间内从s走到e,拿的糖果最多,然后让你输出,拿了多少个糖果

题解:

数据范围太小了,直接暴力搜索就好了……

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************
struct node
{
    int x,y;
};
int n,m,t;
int s,en;
int jew[15];
int vis[20][20];
int ans;
int g[20][20];
void init()
{
    ans=0;
    memset(jew,0,sizeof(jew));
    memset(g,0,sizeof(g));
    memset(vis,0,sizeof(vis));
}
void dfs(int po,int tt,int num)
{
    if(po==en&&tt<=t)
        ans=max(ans,num);
    for(int i=0;i<n;i++)
    {
        if(!vis[po][i]&&g[po][i]&&tt+g[po][i]<=t)
        {
            int tmp=jew[i];
            vis[po][i]=1;
            jew[i]=0;
            dfs(i,tt+g[po][i],num+tmp);
            jew[i]=tmp;
            vis[po][i]=0;
        }
    }

}
int main()
{
    //freopen("test.txt","r",stdin);
    while(scanf("%d%d%d",&n,&m,&t)!=EOF)
    {
        init();
        s=read(),en=read();
        for(int i=0;i<n;i++)
            jew[i]=read();
        for(int i=1;i<=m;i++)
        {
            int a=read(),b=read(),c=read();
            g[a][b]=c,g[b][a]=c;
        }
        int tmp=jew[s];
        jew[s]=0;
        dfs(s,0,tmp);
        printf("%d\n",ans);
    }
}
时间: 2024-10-18 10:49:41

zoj 3620 Escape Time II dfs的相关文章

zoj 3620 Escape Time II

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4744 Escape Time II Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 second

zoj 3332 Strange Country II

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3332 Description You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 to n. The unique way to trav

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

ZOJ 3631 Watashi&#39;s BG DFS

J - Watashi's BG Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3631 Appoint description: Description Watashi is the couch of ZJU-ICPC Team and he is very kind hearted. In ZJU-ICPC summer trainin

ZOJ 3644 Kitty&#39;s Game dfs,记忆化搜索,map映射 难度:2

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的因数,所以不会形成环,只需从1直接走,走到n即可. 但是如果这样的话时空复杂度就都是nk,明显不满足题意,而这个时候我们可以想到,每个状态都必然是k的约数,(点数不是k的约数的节点不在路上,可以无视),而约数的个数也就k^0.5个,可以直接用map映射,这样时空复杂度都是n*k^0.5,可以解出答案

POJ 3620 Avoid The Lakes(DFS)

题目链接:http://poj.org/problem?id=3620 DFS基础题~ #include<cstdio> #include<iostream> #include<sstream> #include<cstdlib> #include<cstring> #include<string> #include<climits> #include<cmath> #include<algorithm&

ZOJ 1002 Fire Net(dfs)

嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一个4*4的地图给每一个点排序,如下图: 0  1  2  3 4  5  6  7 8  9  10  11 12 13 14 15 设一个点为第k个点,那么它的坐标为(k/n,k%n),根据这个进行dfs,当k == n * n是退出dfs.如果k < n *n,就继续dfs,判断是否能放下,即要

CodeForces 264A Escape from Stones dfs

题目链接:点击打开链接 题意:开始有一个区间[0,1] 每次操作在中间填i,然后选择坐半段或者右半段(给出选择的方案,然后从左到右输出填写的i) (i=1 2 3···) #include <cstdio> char s[1000005]; void dfs(int x){ if(s[x] == 0)return ; if(s[x] == 'l') { dfs(x+1); printf("%d\n", x+1); } else { printf("%d\n&quo