克鲁斯卡尔算法 畅通工程再续 Truck History 畅通工程

畅通工程再续

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 87   Accepted Submission(s) : 37
Problem Description
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
 

Input
输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
 

Output
每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
 

Sample Input
  
  
2 2 10 10 20 20 3 1 1 2 2 1000 1000
 

Sample Output
  
  
1414.2 oh!
 

Author
8600
 

Source
2008浙大研究生复试热身赛(2)——全真模拟
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct note{
	int x,y;
	double v;
}s[11000];
double a[110],b[110];
bool cmp(note a1,note a2)
{
	return a1.v<a2.v;
}
int n,per[110],k;
void init()
{
	for(int i=1;i<=111;i++)
	per[i]=i;
}
int fun(int x)
{
	int r=x;
	while(per[r]!=r)
	r=per[r];
	per[x]=r;
	return r;
}
int join(int x,int y)
{
	int fx=fun(x);
	int fy=fun(y);
	if(fx!=fy)
	{
		per[fx]=fy;
		return 1;
	}
	return 0;
}
int kru(int c)
{
	int i,j;
	init();
	double sum=0;
	sort(s,s+k,cmp);
	j=0;
	for(int i=0;i<k;i++)
	{
		if(join(s[i].x,s[i].y))
		{
			j++;
			sum+=s[i].v;
		}
		if(j==c-1)
		return printf("%.1f\n",sum*100);
	}
	return printf("oh!\n");
}
int main()
{
	int i,j,c;
	double d;
	scanf("%d",&n);
	while(n--)
	{
		
		scanf("%d",&c);
		k=0;
		for(i=1;i<=c;i++)
		scanf("%lf%lf",&a[i],&b[i]);
		for(i=1;i<=c;i++)
		{
			for(j=i+1;j<=c;j++)
			{
                 d=sqrt((a[j]-a[i])*(a[j]-a[i])+(b[j]-b[i])*(b[j]-b[i]));
				    if(d>=10.0&&d<=1000.0)
				    {
				    	s[k].x=i;
					    s[k].y=j;
					    s[k].v=d;
				    	k++;
					}
			}
		}
		kru(c);
}
	return 0;
}

Truck History

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 61   Accepted Submission(s) : 21
Problem Description
Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on.

Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as
1/Σ(to,td)d(to,td)

where the sum goes over all pairs of types in the derivation plan such that t o is the original type and t d the type derived from it and d(t o,t d) is the distance of the types.
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan.
 

Input
The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.
 

Output
For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.
 

Sample Input
   
   
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
 

Sample Output
   
   
The highest possible quality is 1/3.
 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[2020][10];
struct note{
	int q,z,v;
}s[4000002];
int per[2020],k;
bool cmp(note a1,note a2)
{
	return a1.v<a2.v;
}
int Q(int x,int y)
{
	int r=0;
	for(int i=0;i<7;i++)
	{
		if(a[x][i]!=a[y][i])
		r++;
	}
	return r;
}
void init()
{
	for(int i=1;i<=2000;i++)
	per[i]=i;
}
int fun(int x)
{
	int r=x;
	while(per[r]!=r)
	r=per[r];
	per[x]=r;
	return r;
}
int join(int x,int y)
{
	int fx=fun(x);
	int fy=fun(y);
	if(fx!=fy)
	{
		per[fx]=fy;
		return 1;
	}
	return 0;
}
int kru(int n)
{
	int i,j,sum;
	sum=0;j=0;
	init();
	sort(s,s+k,cmp);
	for(int i=0;i<k;i++)
	{
		if(join(s[i].q,s[i].z))
		{
			j++;
			sum+=s[i].v;
			
		}
		if(j==n-1)
		return printf("The highest possible quality is 1/%d.\n",sum);
   }
   return 0;
}
int main()
{
	int n,i,j;
	while(scanf("%d",&n),n)
	{
		k=0;
		for(i=1;i<=n;i++)
		scanf("%s",a[i]);
		for(i=1;i<=n;i++)
		{
			for(j=i+1;j<=n;j++)
			{
				s[k].q=i;
				s[k].z=j;
				s[k++].v=Q(i,j);
			}
		}
			kru(n);
	}
	return 0;
}

畅通工程

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 92   Accepted Submission(s) : 60
Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
 

Input
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N 行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
 

Output
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
 

Sample Input
   
   
3 3 1 2 1 1 3 2 2 3 4 1 3 2 3 2 0 100
 

Sample Output
   
   
3 ?
 

Source
浙大计算机研究生复试上机考试-2007年
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int per[110];
struct note{
	int q,l,v;
}s[1000];
void init()
{
	for(int i=0;i<=100;i++)
	per[i]=i;
}
bool cmp(note a1,note a2)
{
	return a1.v<a2.v;
}
int fun(int x)
{
	int r;
	r=x;
	while(per[r]!=r)
	{
		r=per[r];
	}
	per[x]=r;
	return r;
}
int join(int x,int y)
{
	int fx=fun(x);
	int fy=fun(y);
	if(fx!=fy)
	{
		per[fx]=fy;
		return 1;
	}
	else return 0;
}
int main()
{
	int n,m;
	int a,b,i,sum,k,j;
	while(scanf("%d%d",&a,&b),a)
	{
		for(i=0;i<a;i++)
		{
			scanf("%d%d%d",&s[i].q,&s[i].l,&s[i].v);
		}
		sort(s,s+a,cmp);
		init();
		sum=0;
		k=0;
		for(i=0;i<a;i++)
		{
			if(join(s[i].q,s[i].l))
			{
				k++;
				sum+=s[i].v;
			}
		}
		if(k>=b-1)printf("%d\n",sum);
		else printf("?\n");
	    }
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值