poj 2377 Bad Cowtractors
来源:互联网 发布:剑三萝莉捏脸数据截图 编辑:程序博客网 时间:2024/06/05 17:40
Bad Cowtractors
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16564 Accepted: 6735
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
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.
OutputLine 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
Hint
OUTPUT DETAILS:
The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.
最小生成树
#include <stdio.h>#include <algorithm>#include <string.h>#include <math.h>#define LL long long#define INF 999999999const int maxn = 1100;int edge[1100][1100];bool vis[maxn];int lowcost[maxn];int out[maxn];int out1[maxn];using namespace std;int n,m;int prim(int num){ vis[num] = 1; int sum = 0; for(int i = 1; i <= n; i++) { lowcost[i] = edge[i][num]; if(lowcost[i] == INF) lowcost[i] = -1; } for(int i = 1; i < n; i++) { int maxx = -1; int temp; for(int j = 1; j <= n; j++) { if(vis[j] == 0 && lowcost[j] > maxx &&lowcost[j] != INF) { maxx = lowcost[j]; temp = j; } } vis[temp] = 1; if(maxx == -1) return -1; sum += maxx; for(int j = 1; j <= n; j++) { if(vis[j] == 0 && lowcost[j] < edge[j][temp] && edge[j][temp] != INF) { lowcost[j] = edge[j][temp]; } } } return sum;}int main(){ while(~scanf("%d%d",&n,&m)) { memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i++) { for(int j =1; j <= n; j++) { edge[i][j] = INF; } } for(int i = 0; i < m; i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); if(w > edge[u][v] || edge[u][v] == INF) edge[u][v] = edge[v][u] = w; } int ans = prim(1); printf("%d\n",ans); } return 0;}
- poj 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- POJ-2377-Bad Cowtractors
- POJ 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- Bad Cowtractors.(POJ-2377)
- poj 2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- 【POJ 2377 Bad Cowtractors】
- POJ 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- poj 2377Bad Cowtractors
- 如何识别机器批量访问还是浏览器正常访问
- LOESS局部加权非参数回归
- Neurons字幕组 | 2分钟看强化学习如何优雅地解决地形穿越问题(附论文下载)
- Training Region-based Object Detectors with Online Hard Example Mining
- 清华姚期智最新演讲:为什么现在是金融科技的“新”黄金时代
- poj 2377 Bad Cowtractors
- 播报 | 人工智能恐怖故事集:从虚拟空间汉尼拔到种族主义哥斯拉
- 分布式锁与实现(一)——基于Redis实现
- 通过vSphere客户端进行单台ESXi主机管理
- 懒加载和预加载
- 代码块的访问顺序
- Markdown 添加 Latex 数学公式
- python学习:设计程序
- 01背包装满