博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最小生成树Prim poj1258 poj2485 poj1789
阅读量:5033 次
发布时间:2019-06-12

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

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 has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
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 input includes several cases. For each case, 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

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Sample Input

40 4 9 214 0 8 179 8 0 1621 17 16 0

Sample Output

28 题意:N*N个城市   联通全部城市花费的最小代价  eg:0 4 9 2 第二个城市到第一个代价为4   第三个到第一个代价为9  第四个到第一个代价为21     用的Prim
#include 
#include
#include
using namespace std;#define INF 100001int t,map[105][105];int prim(){ int sum_dis=0; int m=1,s=1; ///s用来标记选当时中的点 m用来标记选过的点 int point; ///标记当时所在的点 int u[105]= {
false}; u[s]=true; int min; int low_dis[105]; for(int i=1; i<=t; i++) low_dis[i]=INF; while(true) { if(t==m) break; min=INF; for(int i=2; 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=1; i<=t; i++) { for(int j=1; j<=t; j++) { cin>>map[i][j]; } } cout<
<

poj2485

Highways

Time Limit: 1000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[] [] []

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.
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 input is an integer T, which tells how many test cases followed.
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

For each test case, you should output a line 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

130 990 692990 0 179692 179 0

Sample Output

692

题意: 最小代价走最远路程   样例意思与上一题一样

解析:这次多了一个判断  

///走的路最长 花费时间最小#include 
#include
#include
using namespace std;#define INF 0x1f1f1f1fint n,a[505][505];int prim(){ ///前提的初始化 int low[65537]; int low_ss; int vis[505]= {
false}; int i,j,point,p,s=1,m=1; int min,res=0; vis[s]=true; for(int i=1; i<=n; i++) low[i]=655339; ///进行遍历 while(true) { low_ss=INF; if(n==m) ///同样遍历了全部点 break; for(int i=2; 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

 poj1789

Truck History

Time Limit: 2000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[] [] []

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

4aaaaaaabaaaaaaabaaaaaaabaaaa0

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 
#include
#include
using namespace std;#define INF 2001int n;char map[INF][7];int dis[INF][INF]= {
0};int weig(int i,int j){ int w=0; for(int k=1; k<=7; k++) { if(map[i][k]!=map[j][k]) w++; } return w;}int prim(){ int m=1,s=1; ///m 遍历过的所有的点 s以后判断其是否走过 int low_dis[INF]; ///每一行中距离该点的距离 int minmin; ///每一行距离该点最近的距离 bool u[2000]= {
false}; ///判断是否遍历过 int sum_dis=0; ///最终的最小距离 u[s]=true; int point; ///暂时标记当时遍历的点 for(int i=1;i<=n;i++) low_dis[i]=10; while(1) { if(n==m) break; minmin=10; for(int i=2; 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]=1; m++; } return sum_dis;}int main(){ while(~scanf("%d",&n)) { if(n==0) break; ///输入部分 for(int i=1; i<=n; i++) for(int j=1; j<=7; j++) cin>>map[i][j]; ///将字符串转化成数字 for(int i=1; i

 

样例的dis[i][j]==111   2,3,4到1的距离为1  因为就一个字符不同

                        22     3,4到2的距离为2  因为有两个字符不同

                        2       4到3的距离为2    因为有两个字符不同    ——————纵向比较即可

 

 

转载于:https://www.cnblogs.com/zhangying/p/3862840.html

你可能感兴趣的文章
装箱拆箱(一)
查看>>
Python3 PyMySQL 的使用
查看>>
11个审查Linux是否被入侵的方法
查看>>
CentOS6.7源码安装MySQL5.6
查看>>
android Bitmap总结
查看>>
触发器简介
查看>>
JAVA反射机制的学习
查看>>
mysql - rollup 使用
查看>>
Chrome系列 Failed to load resource: net::ERR_CACHE_MISS
查看>>
出现函数重载错误call of overloaded ‘printfSth(double)’ is ambiguous
查看>>
SDUT 1941-Friday the Thirteenth(水)
查看>>
java API连接虚拟机上的hbase
查看>>
c#扩展出MapReduce方法
查看>>
Cookie工具类 - CookieUtil.java
查看>>
[转载]linux下各文件夹的结构说明及用途介绍
查看>>
HDUOJ----4502吉哥系列故事——临时工计划
查看>>
form action中get \post传递参数的问题
查看>>
CloudStack4.4安装 ubuntu14.04
查看>>
java.io.tmpdir在哪里?
查看>>
php session_unset与session_destroy的区别
查看>>