最小生成树Prim poj1258 poj2485 poj1789
poj:1258
Agri-Net
Time Limit: 1000 MS Memory Limit: 10000 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
Farmer John ordered a high speed connection for his farm and is
going to share his connectivity with the other farmers. To minimize
cost, he wants to lay the minimum amount of optical fiber to connect his
farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of
farms, you must find the minimum amount of fiber needed to connect them
all together. Each farm must connect to some other farm such that a
packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
the first line contains the number of farms, N (3 <= N <= 100).
The following lines contain the N x N conectivity matrix, where each
element shows the distance from on farm to another. Logically, they are N
lines of N space-separated integers. Physically, they are limited in
length to 80 characters, so some lines continue onto others. Of course,
the diagonal will be 0, since the distance from farm i to itself is not
interesting for this problem.
Output
that is the sum of the minimum length of fiber required to connect the
entire set of farms.
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
题意:N*N个城市 联通全部城市花费的最小代价 eg:0 4 9 2 第二个城市到第一个代价为4 第三个到第一个代价为9 第四个到第一个代价为21
用的Prim
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
#define INF 100001 int t,map[][]; int prim()
{
int sum_dis=;
int m=,s=; ///s用来标记选当时中的点 m用来标记选过的点
int point; ///标记当时所在的点
int u[]= {false};
u[s]=true;
int min;
int low_dis[]; for(int i=; i<=t; i++)
low_dis[i]=INF; while(true)
{
if(t==m)
break;
min=INF;
for(int i=; i<=t; i++)
{
if(!u[i]&&low_dis[i]>map[s][i])
low_dis[i]=map[s][i]; ///各点到s点的距离
if(!u[i]&&min>low_dis[i])
{
min=low_dis[i]; ///选取最近的到s的距离
point=i; ///记录这一点
}
} ///遍历完一行了
sum_dis+=min;
s=point;
u[s]=true; ///这点找过了
m++; }
return sum_dis;
} int main()
{
while(cin>>t)
{
for(int i=; i<=t; i++)
{
for(int j=; j<=t; j++)
{
cin>>map[i][j];
}
}
cout<<prim()<<endl;
}
return ;
}
poj2485
Highways
Time Limit: 1000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
Flatopian towns are numbered from 1 to N. Each highway connects
exactly two towns. All highways follow straight lines. All highways can
be used in both directions. Highways can freely cross each other, but a
driver can only switch between highways at a town that is located at the
end of both highways.
The Flatopian government wants to minimize the length of the longest
highway to be built. However, they want to guarantee that every town is
highway-reachable from every other town.
Input
The first line of each case is an integer N (3 <= N <= 500),
which is the number of villages. Then come N lines, the i-th of which
contains N integers, and the j-th of these N integers is the distance
(the distance should be an integer within [1, 65536]) between village i
and village j. There is an empty line after each test case.
Output
contains an integer, which is the length of the longest road to be built
such that all the villages are connected, and this value is minimum.
Sample Input
1 3
0 990 692
990 0 179
692 179 0
Sample Output
692
题意: 最小代价走最远路程 样例意思与上一题一样
解析:这次多了一个判断
///走的路最长 花费时间最小
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std;
#define INF 0x1f1f1f1f
int n,a[][]; int prim()
{
///前提的初始化
int low[];
int low_ss;
int vis[]= {false};
int i,j,point,p,s=,m=;
int min,res=;
vis[s]=true;
for(int i=; i<=n; i++)
low[i]=; ///进行遍历
while(true)
{
low_ss=INF;
if(n==m) ///同样遍历了全部点
break;
for(int i=; i<=n; i++)
{
if(!vis[i]&&low[i]>a[s][i])
low[i]=a[s][i];
if(!vis[i]&&low_ss>low[i])
{
low_ss=low[i];
point=i;
}
}
if(res<low_ss) ///判断寻找最短代价
res=low_ss;
s=point;
vis[s]=true;
m++;
}
return res; } int main()
{
int t;
scanf("%d",&t);
while(t--)
{
///输入部分
scanf("%d",&n);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d",&a[i][j]);
}
}
///输出部分
printf("%d\n",prim());
}
return ;
}
poj1789
Truck History
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
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 to is the original type and td the type derived from it and d(to,td) 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
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
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. 题意:
题意大概是这样的:用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数。一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance,现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小。
例如有如下4个编号:
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
显然的,第二,第三和第四编号分别从第一编号衍生出来的代价最小,因为第二,第三和第四编号分别与第一编号只有一个字母是不同的,相应的distance都是1,加起来是3。也就是最小代价为3。
问题可以转化为最小代价生成树的问题。因为每两个结点之间都有路径,所以是完全图。
此题的关键是将问题转化为最小生成树的问题。每一个编号为图的一个顶点,顶点与顶点间的编号差即为这条边的权值,题目所要的就是我们求出最小生成树来。这里我用prim算法来求最小生成树。
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
#define INF 2001
int n;
char map[INF][];
int dis[INF][INF]= {}; int weig(int i,int j)
{
int w=;
for(int k=; k<=; k++)
{
if(map[i][k]!=map[j][k])
w++;
}
return w;
} int prim()
{
int m=,s=; ///m 遍历过的所有的点 s以后判断其是否走过
int low_dis[INF]; ///每一行中距离该点的距离
int minmin; ///每一行距离该点最近的距离
bool u[]= {false}; ///判断是否遍历过
int sum_dis=; ///最终的最小距离
u[s]=true;
int point; ///暂时标记当时遍历的点 for(int i=;i<=n;i++)
low_dis[i]=; while()
{
if(n==m)
break;
minmin=;
for(int i=; i<=n; i++)
{
if(!u[i]&&low_dis[i]>dis[s][i])
low_dis[i]=dis[s][i];
if(!u[i]&&minmin>low_dis[i])
{
minmin=low_dis[i];
point =i;
}
}
sum_dis+=minmin;
s=point;
u[s]=;
m++; }
return sum_dis; } int main()
{
while(~scanf("%d",&n))
{
if(n==)
break;
///输入部分
for(int i=; i<=n; i++)
for(int j=; j<=; j++)
cin>>map[i][j];
///将字符串转化成数字
for(int i=; i<n; i++)
for(int j=i+; j<=n; j++)
dis[i][j]=dis[j][i]=weig(i,j);
printf("The highest possible quality is 1/%d.\n",prim());
}
return ;
}
样例的dis[i][j]==111 2,3,4到1的距离为1 因为就一个字符不同
22 3,4到2的距离为2 因为有两个字符不同
2 4到3的距离为2 因为有两个字符不同 ——————纵向比较即可
最小生成树Prim poj1258 poj2485 poj1789的更多相关文章
- Poj1258 Agri-Net (最小生成树 Prim算法 模板题)
题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
- POJ1258 (最小生成树prim)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46319 Accepted: 19052 Descri ...
- 最小生成树—prim算法
最小生成树prim算法实现 所谓生成树,就是n个点之间连成n-1条边的图形.而最小生成树,就是权值(两点间直线的值)之和的最小值. 首先,要用二维数组记录点和权值.如上图所示无向图: int map[ ...
- 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。
//归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...
- 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)
matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...
- 最小生成树Prim算法(邻接矩阵和邻接表)
最小生成树,普利姆算法. 简述算法: 先初始化一棵只有一个顶点的树,以这一顶点开始,找到它的最小权值,将这条边上的令一个顶点添加到树中 再从这棵树中的所有顶点中找到一个最小权值(而且权值的另一顶点不属 ...
- 转载:最小生成树-Prim算法和Kruskal算法
本文摘自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/30/2615542.html 最小生成树-Prim算法和Kruskal算法 Prim算 ...
- 最小生成树Prim
首先解释什么是最小生成树,最小生成树是指在一张图中找出一棵树,任意两点的距离已经是最短的了. 算法要点: 1.用book数组存放访问过的节点. 2.用dis数组保存对应下标的点到树的最近距离,这里要注 ...
- 最小生成树Prim算法和Kruskal算法
Prim算法(使用visited数组实现) Prim算法求最小生成树的时候和边数无关,和顶点树有关,所以适合求解稠密网的最小生成树. Prim算法的步骤包括: 1. 将一个图分为两部分,一部分归为点集 ...
随机推荐
- vs2015 npm list 更新问题
在更新npm list时候,经常会非常的慢,今天试了一个诡异的方法,就是在文件夹下面直接把所有缓存全部删除,全部重新下,结果感觉反而速度快很多. 原来的更新包80M竟然1个小时没有下载完. C:\Us ...
- 买茶叶想到的哪个比较便宜 x1/y1 >x2/y2 x代表多少钱 y代表 多少克 无聊的试炼
茶叶1 128元 200克 茶叶2 330元 160克 当然这个哪个便宜 一眼就知道了,这里不过抛砖引玉 128元 330元 200克 160克 我们把价钱用x表示 多少克 ...
- keras框架的MLP手写数字识别MNIST,梳理?
keras框架的MLP手写数字识别MNIST 代码: # coding: utf-8 # In[1]: import numpy as np import pandas as pd from kera ...
- centos7 安装网卡
1.虚拟机测试,先开启命令行 su systemctl set-default multi-user.target reboot 2.编辑网卡 虚拟机网络设置成桥接模式 vi /etc/sysconf ...
- 2018.11.03 NOIP模拟 树(长链剖分优化dp)
传送门 考虑直接推式子不用优化怎么做. 显然每一个二进制位分开计算贡献就行. 即记录fi,jf_{i,j}fi,j表示距离iii这个点不超过jjj的点的每个二进制位的0/10/10/1个数. 但直接 ...
- 将项目部署到 github上(部署到码云操作一样,前提是有码云账号)
来源:http://www.cnblogs.com/fengxiongZz/p/6477456.html 首先你需要自己的网页文件(俗称项目) 第一步:登录到Github上,新建一个repositor ...
- sqlserver2008查询性能优化(文摘)
第1章 sql查询性能调整 第4章 索引分析
- 注意JDBC驱动的版本和JDK的版本是否匹配 JDBC连接Mariadb
Java利用JDBC连接Mariadb的过程和MySQL基本一致. 但是需要注意JDBC驱动的版本和JDK的版本是否匹配: JDBC和JDK版本对应关系 JDBC版本 JDK版本 2.x 1.8 1. ...
- BT1120中的串行传输
BT1120不仅支持并行传输,也定义了并行传输.详细说明可以看ITU-R BT1120,在这里只做概述和总结.
- (转载)Fiddler实战深入研究(二)
原文来源于:http://www.cnblogs.com/tugenhua0707/p/4637771.html,作者:涂根华 !个人觉得文章写的特别好,故收藏于此,感谢原作者的分享 Fiddler实 ...