征兵问题

来源:互联网 发布:python中文手册 chm 编辑:程序博客网 时间:2024/05/01 17:44
★实验任务
XX有一个国家, 他要建立一个军队来保护他的国家, 现在他选出了 n 个男孩和 m 个女
孩,准备将这些人征召来当兵,正常情况下,每增加一个士兵要花费10000 元,但是在一
些男孩和女孩之间存在一些关系, 我们可以利用这些关系来减少成本。 如果女孩x 和男孩 y
关系强度为d ,如果其中一个已经在军队中那么另外一人加到军队里面 只需要花费
10000-d)元 (这里保证 0<=d<10000. 现在给你所有男女孩之间的关系,请你求出最
少的花费。
注意:每次征兵最多只能使用一个关系。如:女孩1 和男孩 1 有关系 d1,女孩1 和男
2 有关系 d2, 那么你只能使用d1 或者 d2
★数据输入
第一行有三个整数nmR(1<=n<=100001<=m<=10000, 0<=R<=50000), 表示男孩个
数,女孩个数,还有关系数。 接下来R 行,每行有三个整数 x yd0<=x<n
0<=y<m,0<=d<10000),表示图G 的一组关系。
★数据输出
输出最小花费。
输入示例          输出示例
5 5 8           71071
4 3 6831
1 3 4583
0 0 6592
0 1 3063
3 3 4975
1 3 2049

4 2 2104

 #include<iostream>    #include<algorithm>  #define P 10000  using namespace std;  struct Conscription  {      long int g, b, r;  };  bool cmp(Conscription a, Conscription b)  {      return  a.r>b.r;  }  long int UFfind(long int *p, long int root)  {      long int temp, leaf;      leaf = root;      while (p[root] != root)root = p[root];      while (leaf != root)      {          temp = p[leaf];          p[leaf] = root;          leaf = temp;      }      return root;  }  long int  G_map(long int e, long int m, Conscription*CC, long int *p)  {      long int temp, add, x, y;      for (add = 0, temp = 0; add<e; add++)      {          x = UFfind(p, CC[add].g);          y = UFfind(p, CC[add].b);          if (x != y)          {              temp += CC[add].r;              p[x] = p[y];          }      }      return temp;  }  int main()  {      Conscription*CC;      long int n, m, s, e, t;      long int *p;      scanf("%d%d", &n, &m);      m += n;      s = m*P;      p = new long int[m];      t = m;      while (t--)      {          p[t] = t;      }      scanf("%d", &t);      CC = new  Conscription[t];      e = t;      while (t--)      {          scanf("%d%d%d", &CC[t].g, &CC[t].b, &CC[t].r);          CC[t].b += n;      }      sort(CC, CC + e, cmp);      s -= G_map(e, m, CC, p);      printf("%d\n", s);      return 0;  }  




0 0