zoukankan      html  css  js  c++  java
  • Escape Time II 简单的深搜dfs()

    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), mt (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 abc, indicating that there is a way between room a and room b and it will take c (1 ≤ c ≤t) 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
    ***********************************************************************************************************************************************************
    dfs()
    ***********************************************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 #include<queue>
     7 bool vis[1001][1001];
     8 int jw[1001];
     9 int cost[1001][1001];
    10 int maxn,s,e,n,m,t;
    11 void dfs(int st,int jt,int cos)
    12 {
    13     if(st==e&&jt>maxn)
    14         maxn=jt;
    15     for(int it=0;it<n;it++)
    16     {
    17         if(!vis[st][it]&&st!=it&&cost[st][it]&&cos+cost[st][it]<=t)
    18         {
    19             int val=jw[it];
    20             jw[it]=0;
    21             vis[st][it]=true;
    22             dfs(it,jt+val,cos+cost[st][it]);
    23             jw[it]=val;
    24             vis[st][it]=false;
    25         }
    26     }
    27     return;
    28 }
    29 int main()
    30 {
    31     int i,j,k,a,b,w;
    32     while(scanf("%d %d %d",&n,&m,&t)!=EOF)
    33     {
    34         scanf("%d %d",&s,&e);
    35         memset(vis,false,sizeof(vis));
    36         memset(cost,0,sizeof(cost));
    37         for(i=0;i<n;i++)
    38         {
    39             scanf("%d",&jw[i]);
    40         }
    41         for(i=0;i<m;i++)
    42         {
    43             scanf("%d %d %d",&a,&b,&w);
    44             cost[a][b]=cost[b][a]=w;
    45         }
    46         maxn=0;
    47         int val=jw[s];
    48         jw[s]=0;
    49         dfs(s,val,0);
    50         printf("%d
    ",maxn);
    51     }
    52     return 0;
    53 }
    View Code
  • 相关阅读:
    (原创)C++ 同步队列
    (原创)用C++11的std::async代替线程的创建
    C语言宏应用-------#define STR(X) #X
    c++中数据表如何转成业务实体--map和结构体的相互转换
    线程池的陷阱
    java中map和对象互转工具类的实现示例
    3.python元类编程
    人生苦短,我用python(目录)
    11.多线程、多进程和线程池编程
    10.python3实用编程技巧进阶(五)
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3440458.html
Copyright © 2011-2022 走看看