poj 2152 Fire

poj 2152 Fire,第1张

poj 2152 Fire
#include <cstdio>#include <cstring>#include <iostream>#include <vector>using namespace std;#define N 1010#define M (1<<30)struct City{    int d,w;}city[N];struct lianjie{    int child,d;    lianjie(int x,int y)    {        child=x;d=y;    }};vector <lianjie> tree[N];int num,dist[N],best[N],dp[N][N];void init(){    scanf("%d",&num);    int i,u,v,l;    for(i=1;i<=num;i++) tree[i].clear();    for(i=1;i<=num;i++) scanf("%d",&city[i].w);    for(i=1;i<=num;i++) scanf("%d",&city[i].d);    for(i=1;i<num;i++)    {        scanf("%d%d%d",&u,&v,&l);        tree[u].push_back(lianjie(v,l));        tree[v].push_back(lianjie(u,l));    }}void dis(int key){    int length=tree[key].size();    for(int i=0;i<length;i++)    {        int v=tree[key][i].child;        if(dist[v]!=-1) continue;        dist[v]=dist[key]+tree[key][i].d;        dis(v);    }}void dfs(int key,int parent){    int i,j,v,length=tree[key].size();    for(i=0;i<length;i++)    {        v=tree[key][i].child;        if(v!=parent)        dfs(v,key);    }    memset(dist,-1,sizeof(dist));    dist[key]=0;    best[key]=M;    dis(key);    for(i=1;i<=num;i++)    {        if(dist[i]>city[key].d) dp[key][i]=M;        else        { dp[key][i]=city[i].w; for(j=0;j<length;j++) {     v=tree[key][j].child;     if(v!=parent)     dp[key][i]+=min(best[v],dp[v][i]-city[i].w); } best[key]=min(best[key],dp[key][i]);        }    }}int main(){    int T;cin>>T;    while(T--)    {        init();        dfs(1,0);        cout<<best[1]<<endl;    }    return 0;}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4894741.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存