• cf 17B


    B. Hierarchy
    time limit per test
    2 seconds
    memory limit per test
    64 megabytes
    input
    standard input
    output
    standard output

    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: «employee ai 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.

    Input

    The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: aibi and ci (1 ≤ ai, bi ≤ n0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.

    Output

    Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.

    Sample test(s)
    input
    4
    7 2 3 1
    4
    1 2 5
    2 4 1
    3 4 1
    1 3 5
    output
    11
    input
    3
    1 2 3
    2
    3 1 2
    3 1 3
    output
    -1
    Note

    In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    using namespace std;
    #define INF 1<<30
    int n,m,tt,f[1010];
    int main()
    {
          int u,v,w,ans=0;
          scanf("%d",&n);
          for(int i=1;i<=n;i++)
                scanf("%d",&tt),f[i]=INF;
          scanf("%d",&m);
          for(int i=0;i<m;i++)
          {
                scanf("%d%d%d",&u,&v,&w);
                if(f[v]>w)
                      f[v]=w;
          }
          int k=1,flag=1;
          for(int i=1;i<=n;i++)
          {
                if(f[i]==INF&&k)
                {
                      k=0;
                      continue;
                }
                else if(f[i]==INF)
                {
                      flag=0;
                      printf("-1
    ");
                      break;
                }
                ans+=f[i];
          }
          if(flag)
                printf("%d
    ",ans);
          return 0;
    }
    

      

  • 相关阅读:
    55种网页常用小技巧(javascript) (转)
    如何利用RadioButtonList实现datagrid列的单选 (转)
    实现数据分类汇总的SQL语句 (转)
    在ASP.Net中两种利用CSS实现多界面的方法. (转)
    ASP.NET 中 Session 实现原理浅析 [1] 会话的建立流程
    用户控件中使用客户端脚本的控件名称问题 (转)
    快速理解.NET Framework[翻译] (转)挺不错的翻译
    table的宽度,单元格内换行问题 (转)
    实现类似Windows资源管理器的DataGrid(转)
    vs.net web项目使用visual source safe进行源代码管理(转)
  • 原文地址:https://www.cnblogs.com/a972290869/p/4225402.html
Copyright © 2020-2023  润新知