POJ-1789 Truck History---最小生成树Prim算法

题目链接:

https://vjudge.net/problem/POJ-1789

题目大意:

用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数。一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance,现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小。

思路:

最小生成树模板题,这里是稠密图,应该用prim算法

直接在原来模板的基础上稍加改动即可

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #include<stack>
 8 #include<map>
 9 #include<sstream>
10 using namespace std;
11 typedef long long ll;
12 const int maxn = 2e3 + 10;
13 const int INF = 1 << 30;
14 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
15 int T, n, m, x;
16 int Map[maxn][maxn];//存图
17 int lowcost[maxn], mst[maxn];
18 void prim(int u)//最小生成树起点
19 {
20     int sum_mst = 0;//最小生成树权值
21     for(int i = 1; i <= n; i++)//初始化两个数组
22     {
23         lowcost[i] = Map[u][i];
24         mst[i] = u;
25     }
26     mst[u] = -1;//设置成-1表示已经加入mst
27     for(int i = 1; i <= n; i++)
28     {
29         int minn = INF;
30         int v = -1;
31         //在lowcost数组中寻找未加入mst的最小值
32         for(int j = 1; j <= n; j++)
33         {
34             if(mst[j] != -1 && lowcost[j] < minn)
35             {
36                 v = j;
37                 minn = lowcost[j];
38             }
39         }
40         if(v != -1)//v=-1表示未找到最小的边,
41         {//v表示当前距离mst最短的点
42             //printf("%d %d %d\n", mst[v], v, lowcost[v]);//输出路径
43             mst[v] = -1;
44             sum_mst += lowcost[v];
45             for(int j = 1; j <= n; j++)//更新最短边
46             {
47                 if(mst[j] != -1 && lowcost[j] > Map[v][j])
48                 {
49                     lowcost[j] = Map[v][j];
50                     mst[j] = v;
51                 }
52             }
53         }
54     }
55     //printf("weight of mst is = %d\n", sum_mst);
56     printf("The highest possible quality is 1/%d.\n", sum_mst);
57 }
58 string s[maxn];
59 int sum(int i, int j)
60 {
61     int tot = 0;
62     for(int k = 0; k < 7; k++)
63     {
64         if(s[i][k] != s[j][k])tot++;
65     }
66     return tot;
67 }
68 int main()
69 {
70     while(cin >> n && n)
71     {
72         for(int i = 1; i <= n; i++)cin >> s[i];
73         for(int i = 1; i <= n; i++)
74         {
75             for(int j = 1; j <= n; j++)
76             {
77                 Map[i][j] = sum(i, j);
78             }
79         }
80         prim(1);
81     }
82     return 0;
83 }

原文地址:https://www.cnblogs.com/fzl194/p/8723176.html

时间: 2024-07-30 12:21:52

POJ-1789 Truck History---最小生成树Prim算法的相关文章

poj 1789 Truck History 最小生成树 prim

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

poj 1789 Truck History(kruskal算法)

题目链接:http://poj.org/problem?id=1789 思路:把每一行看成一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列写的,但是超时啦,不知道为什么,希望有人可以解答.后面用的数组sort排序然后才AC. code: 数组sort排序AC代码: #include<cstdio> #include<queue> #include<algorithm> #include<io

POJ 1789 -- Truck History(Prim)

 POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxn = 2000 + 10; 6 const int INF = 1000000; 7 int n;//有几个卡车 8 char str[maxn][10]; 9 int d[ma

POJ #1789 Truck History 最小生成树(MST) prim 稠密图 链式向前星

Description 题目:链接 这道题的数据集网上比较少,提供一组自己手写的数据: INPUT: 3 aaaaaaa baaaaaa abaaaaa OUTPUT: The highest possible quality is 1/2. 思路 题意比较不好理解,简而言之就是有 n 个字符串,设两个字符串之间的差异为 dis,dis 由两个字符串对应位置上不同字母的数量决定.比如串A"aaaaaaa" .串B"baaaaaa" 和串C"abaaaaa&

POJ 1789:Truck History 【Prim】

Truck History Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 123   Accepted Submission(s) : 36 Problem Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are

poj 1789 Truck History 最小生成树

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 ex

Kuskal/Prim POJ 1789 Truck History

题目传送门 1 /* 2 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 3 Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Prim.这是最小生成数的裸题,然而题目有点坑爹:( 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <string> 8 #include <algorithm> 9 #include

poj 1789 Truck History 解题报告

题目链接:http://poj.org/problem?id=1789 题目意思:给出 N 行,每行7个字符你,统计所有的 行 与 行 之间的差值(就是相同位置下字母不相同),一个位置不相同就为1,依次累加.问最终的差值最少是多少. 额.....题意我是没看懂啦= =......看懂之后,就转化为最小生成树来做了.这是一个完全图,即每条边与除它之外的所有边都连通.边与边的权值是通过这个差值来算出来的. 1 #include <iostream> 2 #include <cstdio>

ZOJ 2158 &amp;&amp; POJ 1789 Truck History (经典MST)

链接:http://poj.org/problem?id=1789 或  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1158 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for br

POJ 1789 Truck History (Kruskal 最小生成树)

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19860   Accepted: 7673 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick