zoj 3620 Escape Time II dfs

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4744

题意:

给你个无向边带权图,让你在时间内从s走到e,拿的珠宝最多,然后让你输出,拿了多少个珠宝 (有环)

题解:

爆搜 看注释

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
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;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5+10;

int n,m,t,s,e,jew[20],vis[20][20];
vector<pair<int,int> > G[20];
int ans;

void dfs(int x,int sum,int k){
    if(x==e && sum<=t){
        ans = max(ans,k);
        // return ;   !!!! WA了半辈子 就算到终点了 有时间 就还可以走 再回来!!!
    }

    for(int i=0; i<(int)G[x].size(); i++){
        int v = G[x][i].first, w = G[x][i].second;
        // cout << x << "->" << v << " need " << w << endl;
        if(!vis[x][v] && (w+sum)<=t){
            vis[x][v] = 1;  // 判边是否走过,从x走到v 走两次是没有意义的,但是可以从v->x 在有时间的情况下拿到在v初的珠宝
            int tmp = jew[v];
            jew[v] = 0; // 把走过的节点置为0,因为可能有环,再走一次这个点就不能算贡献了
            // cout << k << " ==\n";
            dfs(v,w+sum,k+tmp);
            jew[v] = tmp;
            vis[x][v] = 0;
        }
    }
}

int main(){
    while(scanf("%d%d%d",&n,&m,&t)!=EOF){
        ans = 0;
        for(int i=0; i<=n; i++) G[i].clear();
        MS(vis); MS(jew);
        s=read(),e=read();
        for(int i=0; i<n; i++)
            jew[i] = read();
        for(int i=0; i<m; i++){
            int u=read(),v=read(),w=read();
            G[u].PB(MP(v,w));
            G[v].PB(MP(u,w));
        }
        int tmp = jew[s]; jew[s] = 0;
        dfs(s,0,tmp);
        cout << ans << endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值