畅通工程
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 37969    Accepted Submission(s): 16915
Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
 
Input
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
 
Output
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
 
Sample Input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
 
Sample Output
3
?

C/C++:

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <climits>
#include <bitset>
#define eps 1e-6
using namespace std; int n, m, my_pre[]; struct node
{
int a, b, a_b_distance;
}my_round[]; bool cmp(node a, node b)
{
return a.a_b_distance < b.a_b_distance;
} int my_find(int x)
{
int r = x;
while (r != my_pre[r])
r = my_pre[r];
int i = x, j;
while (r != my_pre[i])
{
j = my_pre[i];
my_pre[i] = r;
i = j;
}
return r;
} void my_join(int a, int b)
{
int n1 = my_find(a), n2 = my_find(b);
if (n1 != n2)
my_pre[n1] = n2;
} int kruskal()
{
int my_ans = ;
sort(my_round, my_round + n, cmp);
for (int i = ; i < n; ++ i)
{
if (my_find(my_round[i].a) == my_find(my_round[i].b)) continue;
my_join(my_round[i].a, my_round[i].b);
my_ans += my_round[i].a_b_distance;
}
int temp_pre = my_find();
for (int i = ; i <= m; ++ i)
{
if (temp_pre == my_find(i)) continue;
return ;
}
return my_ans;
} int main()
{
ios::sync_with_stdio(false); while(scanf("%d%d", &n, &m), n)
{
/**
Initialize
*/
for (int i = ; i <= m; ++ i)
my_pre[i] = i;
memset (my_round, , sizeof(my_round)); /**
Date Input
*/
for (int i = ; i < n; ++ i)
{
scanf("%d%d%d", &my_round[i].a, &my_round[i].b, &my_round[i].a_b_distance);
} /**
Process
*/
int my_temp = kruskal();
if (my_temp)
printf("%d\n", my_temp);
else
printf("?\n");
}
return ;
}

最新文章

  1. windows下获取IP地址的两种方法
  2. Django model 中meta options
  3. AxWebBrowser与WebBrowserU盾登陆时的使用
  4. 【wikioi】1191 数轴染色(线段树+水题)
  5. webService—使用javaxws发布自己的webService
  6. GitHub windows客户端拉代码和提交代码
  7. cf C. Dima and Salad
  8. c# 图片简单模糊 非高斯模糊
  9. Java I/O theory in system level
  10. UVA 11992 - Fast Matrix Operations(段树)
  11. TypeScript设计模式之组合、享元
  12. 开涛spring3(4.3) - 资源 之 4.3 访问Resource
  13. JS获取当前周
  14. HTML复习 2019-2-11
  15. 【Python3练习题 020】 求1+2!+3!+...+20!的和
  16. firewall 和 iptables 常用命令
  17. POJ 3436 ACM Computer Factory 最大流,拆点 难度:1
  18. Python之路番外(第三篇):Pycharm的使用秘籍
  19. 多用StringBuilder,少用字符串拼接
  20. MySQL添加字段和修改字段

热门文章

  1. jar包的多层级maven依赖的坑与正确传递方法
  2. CF991D Bishwock
  3. 阻塞IO模型
  4. RIDE的Edit界面
  5. c语言1博客作业02
  6. EXC_BAD_ACCESS的本质详解以及僵尸模式调试原理
  7. 简易数据分析 13 | Web Scraper 抓取二级页面
  8. 课堂练习 Word count
  9. 实战--dango自带的分页(极简)
  10. fenby C语言 P14