1. 程式人生 > >hdu 6201 dfs

hdu 6201 dfs

stream main con print dfs cond efi bsp for



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