首页 > 其他 > 详细

hdu 6201 dfs

时间:2017-09-11 14:00:50      阅读:266      评论:0      收藏:0      [点我收藏+]


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;
}

 

hdu 6201 dfs

原文:http://www.cnblogs.com/MeowMeowMeow/p/7504672.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!