最大生成树
来源:互联网 发布:mysql tmp table size 编辑:程序博客网 时间:2024/05/21 19:48
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
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
/*#define inf 0x3f3f3f
int lowcost[1010],v[1010],n,m;
int edge[1010][1010];
void prim()
{
long long sum=0;
int next,max,i;
memset(v,0,sizeof(v));
for(i=1;i<=n;i++)
lowcost[i]=edge[1][i];
v[1]=1;
for(i=2;i<=n;i++)
{
int minn=-inf;
for(int j=1;j<=n;j++)
{
if(!v[j]&&minn<lowcost[j])
{
minn=lowcost[j];
next=j;
}
}
if(minn==-inf)
{
cout<<-1<<endl;return ;
}
v[next]=1;
sum+=minn;
for(int j=1;j<=n;j++)
{
if(!v[j]&&lowcost[j]<edge[next][j])
lowcost[j]=edge[next][j];
}
}
cout<<sum<<endl;
}*/
/*int main()
{
int a,b,c;
while(cin>>n>>m)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
edge[i][j]=-inf;
}
while(m--)
{
//cin>>a>>b>>c;
scanf("%d%d%d",&a,&b,&c);
if(edge[a][b]<c)
edge[a][b]=edge[b][a]=c;
}
prim();
}
}prim*/
struct node
{
int s,e,v;
};
node edge[20020];
int fat[1010];
bool cmp(node a,node b)
{
return a.v>b.v;
}
int find(int x)
{
if(fat[x]!=x) fat[x]=find(fat[x]);
return fat[x];
}
void unionn(int x,int y)
{
int fa=find(x);
int fb=find(y);
if(fa!=fb) fat[fa]=fb;
}
int main()
{
int n,m,i,a,b,c,tot,k;
while(cin>>n>>m)
{
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
edge[i].s=a;
edge[i].e=b;
edge[i].v=c;
}
sort(edge+1,edge+1+m,cmp);
for(i=1;i<=n;i++) fat[i]=i;
tot=0;k=0;
for(i=1;i<=m;i++)
{
if(find(edge[i].s)!=find(edge[i].e))
{
unionn(edge[i].s,edge[i].e);
tot+=edge[i].v;
k++;
}
}
if(k==n-1) cout<<tot<<endl;
else cout<<-1<<endl;
}
}
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
/*#define inf 0x3f3f3f
int lowcost[1010],v[1010],n,m;
int edge[1010][1010];
void prim()
{
long long sum=0;
int next,max,i;
memset(v,0,sizeof(v));
for(i=1;i<=n;i++)
lowcost[i]=edge[1][i];
v[1]=1;
for(i=2;i<=n;i++)
{
int minn=-inf;
for(int j=1;j<=n;j++)
{
if(!v[j]&&minn<lowcost[j])
{
minn=lowcost[j];
next=j;
}
}
if(minn==-inf)
{
cout<<-1<<endl;return ;
}
v[next]=1;
sum+=minn;
for(int j=1;j<=n;j++)
{
if(!v[j]&&lowcost[j]<edge[next][j])
lowcost[j]=edge[next][j];
}
}
cout<<sum<<endl;
}*/
/*int main()
{
int a,b,c;
while(cin>>n>>m)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
edge[i][j]=-inf;
}
while(m--)
{
//cin>>a>>b>>c;
scanf("%d%d%d",&a,&b,&c);
if(edge[a][b]<c)
edge[a][b]=edge[b][a]=c;
}
prim();
}
}prim*/
struct node
{
int s,e,v;
};
node edge[20020];
int fat[1010];
bool cmp(node a,node b)
{
return a.v>b.v;
}
int find(int x)
{
if(fat[x]!=x) fat[x]=find(fat[x]);
return fat[x];
}
void unionn(int x,int y)
{
int fa=find(x);
int fb=find(y);
if(fa!=fb) fat[fa]=fb;
}
int main()
{
int n,m,i,a,b,c,tot,k;
while(cin>>n>>m)
{
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
edge[i].s=a;
edge[i].e=b;
edge[i].v=c;
}
sort(edge+1,edge+1+m,cmp);
for(i=1;i<=n;i++) fat[i]=i;
tot=0;k=0;
for(i=1;i<=m;i++)
{
if(find(edge[i].s)!=find(edge[i].e))
{
unionn(edge[i].s,edge[i].e);
tot+=edge[i].v;
k++;
}
}
if(k==n-1) cout<<tot<<endl;
else cout<<-1<<endl;
}
}
阅读全文
0 0
- 最大生成树
- poj1797最大生成树
- hdu3367(最大生成树)
- poj1797最大生成树
- poj3273 最大生成树
- 最大生成树
- POJ2377(最大生成树)
- poj3723(最大生成树)
- 最大生成树Kruskal
- Aizu2224 最大生成树??
- 最大生成树 Keroskal
- 最大生成树
- 最大生成树
- 最大生成树与最小生成树
- matlab prime最大生成树
- 【最大生成树】POJ 2377
- POJ2263&&1797 最大生成树
- hdu 4133 最大生成树
- Linux网络编程——TCP服务器
- SQL Server 字符串 系统常用函数 贴心
- I/O流常用的简化操作
- JAVA入门
- 关于group by 两个或以上条件的分析
- 最大生成树
- nyoj 325zb的生日
- 二进制颜色代码大全(含图)透明度与十六进制代码转换
- Intellij IDEA、PyCharm、PhpStorm、WebStorm等Jetbrains软件激活
- Linux(1)---Ubuntu下终端的常用快捷键
- 二叉树的基本概念和主要性质
- ReactNative——Toast
- java基础----编解码 转换流
- css 浮动 相对定位 绝对定位区别