POJ 2377 最大生成树 prim实现

来源:互联网 发布:windows server 版本 编辑:程序博客网 时间:2024/05/02 06:45
Bad Cowtractors
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6904 Accepted: 3008

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.

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.

Source

USACO 2004 December Silver

 

题目意思很明了,就是想叫你求一个最大生成树

我的想法和网上流行的算法有点不一样,正所谓非主流就是这个意思。。。

主流的算法是利用克鲁斯卡尔算法,并且在排序的时候按降序排列

而我是把所有的边都存为负权,然后再利用prim求最小生成树、最后答案取负就可以了

注意:数据中有重边,必须要去掉。另外不连通的情况要慎重考虑

我的代码:

#include<stdio.h>#include<algorithm>#define inf 99999999using namespace std;struct node{int v1;int v2;int len;};node e[1005];int dis[1005][1005];void init(int n){int i,j;for(i=0;i<=n;i++)for(j=0;j<=n;j++)dis[i][j]=inf;}int prim(int n){int i,j,vx,vy,leng,min,minl;int res=0;for(i=1;i<=n-1;i++){e[i].v1=1;e[i].v2=i+1;e[i].len=dis[1][i+1];}for(i=1;i<=n-1;i++){min=-1;minl=2*inf;for(j=i;j<=n-1;j++){if(e[j].len<minl){minl=e[j].len;min=j;}}if(min==-1)break;res=res+minl;swap(e[i],e[min]);vx=e[i].v2;for(j=i+1;j<=n-1;j++){vy=e[j].v2;leng=dis[vx][vy];if(leng<e[j].len){e[j].len=leng;e[j].v1=vx;}}}return res;}int main(){int i,a,b,n,m,c;scanf("%d%d",&n,&m);init(n);for(i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);if(dis[a][b]==inf||-c<dis[a][b]){dis[a][b]=-c;dis[b][a]=-c;}}int ans=prim(n);if(ans>0)printf("-1/n");elseprintf("%d/n",-ans);return 0;}


 

----------------------------------------------------------------------------------------------------------传说中的分割线。。。

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 儿童7岁还坐不住怎么办 孩子缺锌手蜕皮裂开怎么办 宝宝读幼儿园哭怎么办 幼儿上课爱讲话怎么办 孩子上课总是乱跑怎么办 孩子听不懂老师讲课怎么办 高一上课听不懂怎么办 上课学生纪律差怎么办 一年级学生认字少怎么办 孩子上课做不住怎么办 幼儿园小孩上课乱跑怎么办 孩子挑食不爱吃饭怎么办 幼儿园孩子不听指令怎么办 1岁宝宝多动症怎么办 3岁宝宝胆小怎么办 爱挑食的孩子怎么办 小孩上课经常发呆怎么办 小孩不爱吃饭挑食怎么办 一年级小孩学习不好怎么办 宝宝上课坐不住怎么办 八个月婴儿拉稀怎么办 八个月孕妇拉稀怎么办 孩子好动爱喊怎么办 八个月小孩发烧怎么办 孩子好动怎么办学龄前教育 小孩好动症该怎么办 小孩子好动症该怎么办 怀孕5个月胎死亡怎么办 小孩多动调皮怎么办 手心老是出汗是怎么办 孩子吃饭特别慢怎么办 小孩子老想睡觉怎么办 孩子下午上课犯困怎么办 小孩子有多动症该怎么办 初中写作业犯困怎么办 孩子晚上学习困怎么办 小孩子容易兴奋激动怎么办 中考时过度兴奋怎么办 小孩兴奋不睡觉怎么办 孩子突然反常不听话怎么办? 婴儿亢奋不睡觉怎么办