Bad Cowtractors

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem Description
Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie.

Realizing Farmer John will not pay her, Bessie decides to
do the worst job possible. She must decide on a set of connections to install so
that (i) the total cost of these connections is as large as possible, (ii) all
the barns are connected together (so that it is possible to reach any barn from
any other barn via a path of installed connections), and (iii) so that there are
no cycles among the connections (which Farmer John would easily be able to
detect). Conditions (ii) and (iii) ensure that the final set of connections will
look like a "tree".

 
Input
* Line 1: Two space-separated integers: N and M
<br> <br>* Lines 2..M+1: Each line contains three space-separated
integers A, B, and C that describe a connection route between barns A and B of
cost C.
 
Output
* Line 1: A single integer, containing the price of the
most expensive tree connecting all the barns. If it is not possible to connect
all the barns, output -1.
 
Sample Input
5 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 17
 
Sample Output
42
 #include <iostream>
#include <cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
int a[][];
int dis[];
bool vis[];
int n, m;
void Prime()
{
for (int i = ; i <= n; i++)
{
vis[i] = false;
dis[i] = a[][i];
}
dis[] = ;
vis[] = true;
int ans = ;
for (int i = ; i <= n; i++)
{
int minn = ;
int p = -;
for (int j = ; j <= n; j++)
{
if (!vis[j] && dis[j]>minn)// 是大于,找出最大的边
minn = dis[p = j];
}
if (p == -)
{
cout << "-1" << endl;
return;
}
vis[p] = true;
ans += minn;
for (int j = ; j <= n; j++)
{
if (!vis[j] && dis[j]<a[p][j])//尽可能让边变大
dis[j] = a[p][j];
}
}
cout << ans << endl;
}
int main()
{
while (cin >> n >> m)
{
//初始化为0
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
a[i][j] = ;
}
int x, y, z;
while (m--)
{
scanf("%d%d%d", &x, &y, &z);
if (z>a[x][y])
a[x][y] = a[y][x] = z;
}
Prime();
}
return ;
}

最新文章

  1. 关于HTML的FORM上传文件问题
  2. WireShar使用笔记
  3. 检测Linux VPS是Xen、OpenVZ还是KVM真假方法
  4. UITapGestureRecognizer 的用法
  5. (一)熟悉执行流程——基于ThinkPHP3.2的内容管理框架OneThink学习
  6. android智能家居在线语音控制
  7. Microsoft Visual C++ 不支持long long
  8. jvm系列 (五) ---类的加载机制
  9. 2D特效和3D特效
  10. RabbitMQ 笔记-基本概念
  11. Cookie操作类、压缩、序列化
  12. 给一个Unix域套接字bind一个路径名
  13. Luogu4782 【模板】2-SAT 问题(2-SAT)
  14. C# Invoke方法
  15. 批量设置样式json版
  16. table-cell 布局
  17. 第二节 java流程控制(判断结构+选择结构)
  18. oracle开机自启,开机自动关闭防火墙,开机监听自启
  19. HDU 3535 AreYouBusy(混合背包)
  20. JS+MySQL获取 京东 省市区 地区

热门文章

  1. 浅谈Android Studio3.0更新之路(遇坑必入)
  2. ✅问题:Rails.ajax自定义请求
  3. idea JMX 连接器服务器通信错误
  4. Arrays.copyof(&#183;&#183;&#183;)与System.arraycopy(&#183;&#183;&#183;)区别
  5. hdu——过山车(二分图,匈牙利算法)
  6. Java 调用 PHP 实例(五)
  7. vue-cli脚手架目录讲解
  8. MVC3 之asp.net 与vb.net 互转练习
  9. 用django发送异步邮件
  10. android中自定义view构造函数ContentItemView(Context context, AttributeSet paramAttributeSet)的用处