Cod sursa(job #928207)

Utilizator LuffyBanu Lavinia Luffy Data 26 martie 2013 12:34:01
Problema Asmax Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include<stdio.h>
#define dim 16005
using namespace std;

int i,n,p,x,j,y,rad,cost[dim],mat[dim][dim],viz[dim],nod[dim],s[dim];
long long max=-99999999;

void bfs()
{
i=1; j=1; viz[1]=1; s[1]=cost[1]; nod[i]=1;

while(i<=j)
{x = nod[i];

 for(p=1; p<=mat[x][0]; p++)

    if(!viz[mat[x][p]])
    {
     y = mat[x][p];
     viz[y] = 1;
     j++; nod[j] = y;
     if(s[x] + cost[y] >= 0) {s[x] = s[x] + cost[y]; s[y] = s[x];}
     else s[y] = cost[y];

     if(s[y] > max) max = s[y];
    }
i++;
}


}




int main()
{
 FILE *f=fopen("asmax.in","r"), *g=fopen("asmax.out","w");

 fscanf(f,"%d",&n);
  for(i=1; i<=n; i++)
 fscanf(f,"%d",&cost[i]);


  for(i=1; i<n; i++)
 {fscanf(f,"%d%d",&x,&y);
  mat[x][0]++; mat[x][mat[x][0]]=y;
  mat[y][0]++; mat[y][mat[y][0]]=x;
 }

 bfs();

 fprintf(g,"%lld\n",max);

fclose(f);
fclose(g);
return 0;
}