POJ 2377 Bad Cowtractors 最大生成树
来源:互联网 发布:苹果6数据快捷开关在哪 编辑:程序博客网 时间:2024/05/15 23:28
点击打开链接
Bad Cowtractors
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9681 Accepted: 4109
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".
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.
* 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 81 2 31 3 72 3 102 4 42 5 83 4 63 5 24 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.
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.
Source
USACO 2004 December Silver
给你n个节点,m条边让你求最大生成树,如果没有输出-1.
最近敲java敲的都不会c了。
Prime算法:
#include<stdio.h>#include<string.h>#define M 1007#define inf 0x3f3f3fint map[M][M],dis[M],vis[M];int n,m;void prime(){ int count=0,pos,min,i; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) dis[i]=map[1][i]; vis[1]=1; for(i=1;i<=n;i++) { min=-inf; pos=-1; for(int j=1;j<=n;j++) if(!vis[j]&&min<dis[j]) { pos=j; min=dis[j]; } if(min==-inf) break; vis[pos]=1; count+=min; for(int j=1;j<=n;j++) if(!vis[j]&&dis[j]<map[j][pos]) dis[j]=map[j][pos]; } //printf("%d\n",i); if(i!=n) printf("-1\n"); else printf("%d\n",count);}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) map[i][j]=-inf; int a,b,c; for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); if(map[a][b]<c) map[a][b]=map[b][a]=c; } prime(); } return 0;}
Kruksal算法:
#include<stdio.h>#include<algorithm>#define M 1007#define inf 0x3f3f3fusing namespace std;int pre[M];int n,m;struct E{ int u,v,pos;}edg[M*20];void init(){ for(int i=0;i<=n;i++) pre[i]=i;}int cmp(E a,E b){ return a.pos>b.pos;}int find(int x){ while(x!=pre[x]) x=pre[x]; return x;}void unio(int a,int b){ int x=find(a); int y=find(b); if(x!=y)pre[y]=x;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { init(); for(int i=0;i<m;i++) scanf("%d%d%d",&edg[i].u,&edg[i].v,&edg[i].pos); sort(edg,edg+m,cmp); int sum=0,k=0; for(int i=0;i<m;i++) if(find(edg[i].u)!=find(edg[i].v)) { unio(edg[i].u,edg[i].v); sum+=edg[i].pos; } for(int i=1;i<=n;i++) { pre[i]=find(i); if(pre[i]==i) k++; } if(k>1)printf("-1\n"); else printf("%d\n",sum); } return 0;}
0 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 (最大生成树,Kruskal)
- poj 2377 Bad Cowtractors(最大生成树)
- POJ 题目2377 Bad Cowtractors(最大生成树)
- POJ 2377 Bad Cowtractors(Kruskal求最大生成树)
- POJ 2377 Bad Cowtractors(最大生成树--kruskul)
- POJ 2377Bad Cowtractors(kruskal最大生成树)
- POJ 2377 Bad Cowtractors(最大生成树)
- POJ 2377 Bad Cowtractors(最大生成树)
- POJ 2377 Bad Cowtractors(最大生成树—prim算法)
- poj 2377 Bad Cowtractors(最大生成树模板)
- POJ 2377 Bad Cowtractors(裸的最大生成树)
- 执行计划
- Cocos2d-x:使用extensions(如CCScale9Sprite CCControlButton)注意事项
- task and back stack
- 如何避免窗口闪屏
- 数组、arraylist和list区别
- POJ 2377 Bad Cowtractors 最大生成树
- 头文件
- SAP MM 常用事务码(Tcode)
- mfc异常崩溃处理
- 回家的路
- sql2008未能加载包microsoft sql management studio package
- BIEE11G实现自定义列排序规则
- VM安装CentOS6.5固定IP设置——NAT模式
- cocos2dx 3.0关于NativeActivity安插广告或JAVA类之类问题