zoukankan      html  css  js  c++  java
  • 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("
    ");
            meow(1,cost[1]);
          //  for(int i=1;i<=n;i++)printf("%d ",cost[i]);printf("
    ");
            for(int i=1;i<=n;i++)e[i].clear();
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    杜教筛
    虚树
    带修莫队
    线性基
    区间修改区间求和cdq分治
    矩阵快速幂求斐波那契数列
    点分治成品
    Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1) C(二分+KMP)
    线性筛
    矩阵快速幂
  • 原文地址:https://www.cnblogs.com/MeowMeowMeow/p/7504672.html
Copyright © 2011-2022 走看看