博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HOJ Tangled in Cables 图论 最小生成树 prim
阅读量:4955 次
发布时间:2019-06-12

本文共 3182 字,大约阅读时间需要 10 分钟。

Tangled in Cables

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

Several data sets will be given in an input file.

  • The first line of each data set 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 {a–z, A–Z, 0–9} 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.

Input is terminated by End-of-File.

Output

For each data set in the input, output 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

 

 题目:
 问他能不能用现有的钱连接所有的人
分析:
 最小生成树算法,prim算法实现,见代码分析:
 

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define X 1005
#define INF 0x7f //最大值
string name[X],ch1,ch2;//储存之前输入的名字,后面输入的有连接关系两个人
double map[X][X],dis[X];//储存有连接关系的两人的值,计算距离
bool use[X]; //标记用过没
int main()
{
 freopen("sum.in","r",stdin);
 freopen("sum.out","w",stdout);
 double total,cur;
 int n,m;
 while(scanf("%lf",&total)!=EOF)
 {
  memset(map,INF,sizeof(map));//全置为无穷大
  scanf("%d",&n);
  for(int i=0;i<n;i++)//输入所有人的名字
   cin>>name[i];
  scanf("%d",&m);
  for(int i=0;i<m;i++)
  {
   bool flag1 = false,flag2 = false;
   int x,y;
   cin>>ch1>>ch2;
   scanf("%lf",&cur);
   for(int j=0;j<n&&(!flag1||(!flag2));j++)
   {
    if(!flag1&&name[j]==ch1)//如果还没找到,并且当前字符串等于之前某个人的名字
    { //根据&&符号短路的关系,先判之前有没有找到,已找到的话就不用继续比较了
     x = j;   //记录之前输入的名字的序号
     flag1 = true;
    }
    if(!flag2&&name[j]==ch2)//同理
    {
     y = j;
     flag2 = true;
    }
   }
   map[x][y] = map[y][x] = cur;//记录到关系地图中
  }

  ///标准prim算法模板

  memset(dis,INF,sizeof(dis));//置为无穷大

  memset(use,false,sizeof(use));//标记未使用过
  int k;
  for(int i=0;i<n;i++)
   dis[i] = map[0][i];
  use[0] = true;
  double ans = 0,MIN;
  for(int i=0;i<n-1;i++)
  {
   MIN = INF;
   for(int j=1;j<n;j++)
    if(!use[j]&&MIN>dis[j])
     MIN = dis[k=j];
   ans+=dis[k];
   use[k] = true;
   for(int j=1;j<n;j++)
    if(!use[j])
     dis[j] = min(dis[j],map[k][j]);
  }
  if(ans<=total)
   printf("Need %.1lf miles of cable\n",ans);
  else
   printf("Not enough cable\n");
 }
 return 0;
}

 

转载于:https://www.cnblogs.com/yejinru/archive/2012/02/29/2374666.html

你可能感兴趣的文章
SpringBoot访问html访问不了的问题
查看>>
{width=200px;height=300px;overflow:hidden}
查看>>
C#生成随机数
查看>>
CSS基础学习 20.CSS媒体查询
查看>>
2019春季第十一周作业
查看>>
洛谷P4591 [TJOI2018]碱基序列 【KMP + dp】
查看>>
iOS CoreData介绍和使用(以及一些注意事项)
查看>>
OS笔记047代理传值和block传值
查看>>
Android应用程序与SurfaceFlinger服务的连接过程分析
查看>>
coco2dx服务器简单例子
查看>>
Java回顾之多线程
查看>>
sqlite
查看>>
机电行业如何进行信息化建设
查看>>
Windows Azure Platform Introduction (4) Windows Azure架构
查看>>
【转】chrome developer tool 调试技巧
查看>>
mahout运行测试与kmeans算法解析
查看>>
互相给一巴掌器
查看>>
Android SDK环境变量配置
查看>>
VM10虚拟机安装图解
查看>>
9、总线
查看>>