hdu 6201 dfs



1
4
10 88 0 30
1 2 40
1 3 2
3 4 10
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#define fi first
#define se second
using namespace std;
const int maxn=(int)1e6 +10;
int n,ans;
int cost[maxn];
int val[maxn];
vector<pair<int,int> >e[maxn];
#define MIN(x,y)  if(x>y)x=y;
#define MAX(x,y)  if(x<y)x=y;
bool vis[maxn];
int dfs(int st,int tot,int edge){ //第一次更新各点的lowestcost 叶子结点的lowestcost可能不是最佳的
    MIN(cost[st],tot);vis[st]=true;
    for(int i=0;i<e[st].size();i++){
        int v=e[st][i].fi,w=e[st][i].se;
        if(vis[v])continue;
        int mincost=dfs(v,cost[st]+w,w);
        MIN(cost[st],mincost);
    }
    return cost[st]+edge;
}
void meow(int st,int tot){//第二次更新各点的lowestcost并且found ans
    vis[st]=false;
    MIN(cost[st],tot);
    for(int i=0;i<e[st].size();i++){
        int v=e[st][i].fi,w=e[st][i].se;
        if(!vis[v])continue;
        meow(v,cost[st]+w);
    }
    MAX(ans,val[st]-cost[st]);
}
int main()
{
    int x,y,z;
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",val+i);cost[i]=val[i];
        }
        for(int i=1;i<n;i++){
                scanf("%d%d%d",&x,&y,&z);
                e[x].emplace_back(y,z);
                e[y].emplace_back(x,z);
        }
        ans=0;
        dfs(1,cost[1],0);
        //for(int i=1;i<=n;i++)printf("%d ",cost[i]);printf("\n");
        meow(1,cost[1]);
      //  for(int i=1;i<=n;i++)printf("%d ",cost[i]);printf("\n");
        for(int i=1;i<=n;i++)e[i].clear();
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-08-25 16:07:11

hdu 6201 dfs的相关文章

hdu 4109 dfs+剪枝优化

求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己增加的)出发,0到1~n个节点之间的距离为1,mt[i]表示从0点到第i个节点目前所得的最长路径 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const

HDU 1015 dfs回溯

题目真长.....看了好长时间才看懂.. 就是给你一个32位数字和一个最多15个字符的字符串,从字符串中选出5个字符,若满足题中给的那个式子,输出字典序最大的那5个字符,若不满足,输出no solution. 为了解决字典序问题,在输入字符串后,把字符串按从大到小排一下序,搜索一下若满足条件输出即可. 贴代码. 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <

hdu 1312 DFS算法简单题

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 此题与油田那题很像是练习深搜的好题,题意是从"@"开始,遍历整个图,找到连接的 "."有多少个 但要考虑变化,简单处理下就行了,主要是斜角的不要了,而且判断结束方式也不一样 具体看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

HDU 6201 transaction transaction transaction(拆点最长路)

transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 88    Accepted Submission(s): 39 Problem Description Kelukin is a businessman. Every day, he travels around

HDU 5143 DFS

分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合法能否进入下层递归来减少内存消耗. /** @Date : 2017-09-27 15:08:23 * @FileName: HDU 5143 DFS.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link :

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

hdu 2212 DFS(水题)

DFS Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4923    Accepted Submission(s): 3029 Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every digit

HDU 6201 transaction transaction transaction DFS 图论

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6201 题目描述: 有一棵节点的树, 每条边有权值, 可以任意选择起点和终点, 得到一个值 = 终点值 - 经过的边权值 - 起点值, 最大化这个值 解题思路: 我们先要求得的值是每个节点有书的时候的最小花费, 由于此最小花费可能是本节点买书, 也有可能是儿子买书, 现在我们将从叶子节点开始向上遍历, 这样只会使得所有的父子节点互相更新, 可是这个时候兄弟节点还没有更新, 所以我们做第二遍DFS由父

HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过所需要的花费.现在需要你在树上选择两个点,一个作为买入商品的点,一个作为卖出商品的点,当然需要考虑从买入点到卖出点经过边的花费.使得收益最大.允许买入点和卖出点重合,即收益最小值为0. 解法:我们设1为根节点,假设一开始一个人身上的钱为0.我们设dp[i][0]表示从根节点走到i及其子树并中任一点买