POJ 2075:Tangled in Cables 【Prim】

Tangled in Cables

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1

Problem Description

You are the owner of SmallCableCo and have purchased the franchise rights for a small town. Unfortunately, you lack enough funds to start your business properly and are relying on parts you have found in an old warehouse you bought.
Among your finds is a single spool of cable and a lot of connectors. You want to figure out whether you have enough cable to connect every house in town. You have a map of town with the distances for all the paths you may use to run your cable between the
houses. You want to calculate the shortest length of cable you must have to connect all of the houses together.

Input

Only one town will be given in an input.

  • The first line gives the length of cable on the spool as a real number.
  • The second line contains the number of houses, N
  • The next N lines give the name of each house‘s owner. Each name consists of up to 20 characters {az,AZ,09} and contains no whitespace or punctuation.
  • Next line: M, number of paths between houses
  • next M lines in the form

< house name A > < house name B > < distance >

Where the two house names match two different names in the list above and the distance is a positive real number. There will not be two paths between the same pair of houses.

Output

The output will consist of a single line. If there is not enough cable to connect all of the houses in the town, output

Not enough cable

If there is enough cable, then output

Need < X > miles of cable

Print X to the nearest tenth of a mile (0.1).

Sample Input

100.0
4
Jones
Smiths
Howards
Wangs
5
Jones Smiths 2.0
Jones Howards 4.2
Jones Wangs 6.7
Howards Wangs 4.0
Smiths Wangs 10.0

Sample Output

Need 10.2 miles of cable

数组要开大~~

AC-code:

#include<cstdio>
#include<cstring>
#define max 0x3f3f3f3f
int num;
float s[10005][10005];
float prim()
{
	int k,i,j,vis[10005];
	float ans=0.0,dis[10005];
	memset(vis,0,sizeof(vis));
	for(i=1;i<num;i++)
		dis[i]=s[0][i];
	dis[0]=0;
	vis[0]=1;
	for(j=1;j<num;j++)
	{
		float min=max;
		for(i=1;i<num;i++)
			if(!vis[i]&&dis[i]<min)
			{
				k=i;
				min=dis[i];
			}
		vis[k]=1;
		for(i=1;i<num;i++)
			if(!vis[i]&&dis[i]>s[k][i])
				dis[i]=s[k][i];
	 }
	 for(i=1;i<num;i++)
	 	ans+=dis[i];
	return ans;
}

int main()
{
	int n,i,j,k,p;
	float m,ans,total;
	char name[10000][200],str1[200],str2[200];
	scanf("%f",&total);
	scanf("%d",&num);
	for(i=0;i<num;i++)
		scanf("%s",name[i]);
	scanf("%d",&n);
	for(i=0;i<num;i++)
		for(j=i;j<num;j++)
			s[i][j]=s[j][i]=max;
	for(j=0;j<n;j++)
	{
		scanf("%s%s%f",str1,str2,&m);
		int k=-1,p=-1;
		for(i=0;i<num;i++)
		{
			if(strcmp(str1,name[i])==0)
				k=i;
			else if(strcmp(str2,name[i])==0)
				p=i;
			if(k!=-1&&p!=-1)
			{
				s[k][p]=s[p][k]=m;
				break;
			}
		}
	}
	ans=prim();
	if(ans>total)
		printf("Not enough cable\n");
	else
		printf("Need %.1f miles of cable\n",ans);
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 21:34:47

POJ 2075:Tangled in Cables 【Prim】的相关文章

POJ 2075 Tangled in Cables (c++/java)

http://poj.org/problem?id=2075 题目大意: 给你一些人名,然后给你n条连接这些人名所拥有的房子的路,求用最小的代价求连接这些房子的花费是否满足要求. 思路: 昨天20分钟的题,输入不小心写错了- -|||||看世界杯半场休息随便看了下发现了....T T 用map进行下标的映射,然后求MST即可. c++ #include<cstdio> #include<string> #include<map> #include<algorith

POJ 2075 Tangled in Cables (kruskal算法 MST + map)

Tangled in Cables Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6039   Accepted: 2386 Description You are the owner of SmallCableCo and have purchased the franchise rights for a small town. Unfortunately, you lack enough funds to start

poj 2075 Tangled in Cables

Tangled in Cables Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 19   Accepted Submission(s) : 6 Problem Description You are the owner of SmallCableCo and have purchased the franchise rights for

poj 3009 Curling 2.0 【DFS】

题意:从2出发,要到达3, 0可以通过,碰到1要停止,并且1处要变成0, 并且从起点开始沿着一个方向要一直前进,直至碰到1(或者3)处才能停止,(就是反射来反射去知道反射经过3).如果反射10次还不能到达3,就输出-1. 策略:深搜. 易错点,方向不容易掌握,并且,出题人把n, m顺序反了. 代码: #include<stdio.h> #include<string.h> int map[25][25]; int ans, n, m; const int dir[4][2] = {

Poj 3087 Shuffle&#39;m Up 【BFS】

Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6556 Accepted: 3077 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of pok

HDOJ 1233 还是畅通工程 【最小生成树】+【prim】

题意:... 策略:最最典型的prim算法. 代码: #include<stdio.h> #include<string.h> #define INF 0x3f3f3f3f #define MAXN 105 int map[MAXN][MAXN], di[MAXN], vis[MAXN]; int n; int prim() { int i, j, min, pos; memset(vis, 0, sizeof(vis)); memset(di, 0, sizeof(di)); p

HDU1162 Eddy&#39;s picture【Prim】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7231    Accepted Submission(s): 3656 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

poj 2411 Mondriaan&#39;s Dream 【dp】

题目:poj 2411 Mondriaan's Dream 题意:给出一个n*m的矩阵,让你用1*2的矩阵铺满,然后问你最多由多少种不同的方案. 分析:这是一个比较经典的题目,网上各种牛B写法一大堆.题解也是 我们可以定义状态:dp[i][st]:在第 i 行状态为 st 的时候的最大方案数. 然后转移方程:dp[i][st] = sum (dp[i-1][ss]) 即所有的当前行都是由上一行合法的状态转移而来. 而状态的合法性由两种铺法得到,第一种横放,注意要求前一行全满,然后竖放,上一行为空

poj 1236 Network of Schools 【强连通图】

题目:poj 1236 Network of Schools 类似题目hdoj 2767 3836 /*******以下kuang大神的解释,写的很好就不解释了*************************/ 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件.2,至少需要添加几条传输线路(边),使任