zoukankan      html  css  js  c++  java
  • cf17B Hierarchy(额,,,水)

    题意:

    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
    ",res);
    
        return 0;
    }
  • 相关阅读:
    学习:类和对象——构造函数和析构函数
    学习:类和对象——封装
    学习:引用类型
    学习:内存分区模型
    实现:通讯录管理系统
    实现:结构体案例
    学习:结构体
    实现:指针和冒泡函数和数组
    学习:指针
    学习:函数的分文件编写
  • 原文地址:https://www.cnblogs.com/fish7/p/4329916.html
Copyright © 2011-2022 走看看