码迷,mamicode.com
首页 > 其他好文 > 详细

cf17B Hierarchy(额,,,水)

时间:2015-03-11 16:54:39      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

题意:

Nick‘s company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employeeai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.

Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.

 

 

思路:

implementation题,,【要先找出根】

 

代码:

int n,m;
int q[1005];
int g[1005][1005];
int ans[1005];
int fa[1005];


int main(){

    cin>>n;
    rep(i,1,n) cin>>q[i];
    cin>>m;

    int temp=-inf;
    int head;

    rep(i,1,n){
        if(q[i]>temp){
            temp=q[i];
            head=i;
        }
    }

    mem(g,inf);
    mem(ans,inf);

    rep(i,1,m){
        int a,b,c;
        cin>>a>>b>>c;
        g[a][b]=min(g[a][b],c);
    }
    rep(i,1,n){
        rep(j,1,n){
            if(g[j][i]<ans[i]){
                ans[i]=g[j][i];
                fa[i]=j;
            }
        }
    }

    int res=0;
    rep(i,1,n){
        if(i==head) continue;
        if(ans[i]==inf){
            puts("-1");
            ret 0;
        }
        res+=g[fa[i]][i];
    }
    print("%d\n",res);

    return 0;
}

 

cf17B Hierarchy(额,,,水)

标签:

原文地址:http://www.cnblogs.com/fish7/p/4329916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!