Bad Cowtractors,t题目链接,click here
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13524 Accepted: 5590
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 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.
Source
USACO 2004 December Silver
prim:
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
int mp[maxn][maxn];
int vis[maxn];
int dis[maxn];
int n,ans,m;
int prim()
{
memset(vis,0,sizeof(vis));
for(int i=1; i<=n; i++)
dis[i]=mp[1][i];
vis[1]=1;
ans=0;
int minn,minid=1,sum=0;
for(int i=2; i<=n; i++)
{
minn=-inf;
for(int j=1; j<=n; j++)
if(!vis[j]&&dis[j]>minn)
{
minn=dis[j];
minid=j;
}
vis[minid]=1;
if(minn==-inf)
{
return -1;
printf("****%d\n",i);
}
sum+=minn;//记录所有最大边权值;
for(int j=1; j<=n; j++)
if(!vis[j]&&dis[j]return sum;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
mp[i][j] = i==j?0:-inf;
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
mp[a][b]=mp[b][a]=max(mp[a][b],c);
}
printf("%d\n",prim());
}
return 0;
}
kruskal:
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1005;
struct node
{
int a,b,c;
}mp[maxn*maxn];
int f[maxn],n,m;
void init(int n)
{
for(int i=0;i<=n;i++)
f[i]=i;
}
int fin(int a)
{
return f[a] == a ? f[a] : f[a] = fin(f[a]);
}
void unio(int a,int b)
{
int x=fin(a),y=fin(b);
if(xelse
f[y]=x;
}
int cmp(node a,node b)
{
return a.c>b.c;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init(n);
for(int i=0;iscanf("%d%d%d",&mp[i].a,&mp[i].b,&mp[i].c);
sort(mp,mp+m,cmp);
long long ans=0;
for(int i=0;iif(fin(mp[i].a)!=fin(mp[i].b))
{
unio(mp[i].a,mp[i].b);
ans+=mp[i].c;
n--;
if(n==1)
break;
}
}
if(n!=1)
ans=-1;
printf("%lld\n",ans);
}
return 0;
}