POJ 1789Truck History
来源:互联网 发布:python 元组转换字典 编辑:程序博客网 时间:2024/05/17 18:28
题意:
题意比交难理解,直接描述了,说这里有一些字符串,一个字符串必须由另外一字符串 “衍生” 出来,衍生的价值是 这两个字符串之间的 distance 距离,即俩个字符串的相同位置总共有几个字符不一样。比如 aaaaaaa 和 baaaaaa的 distance 距离为 1;给你 N 个字符串,让你给出一个衍生方案,使最后的衍生价值最小。
思路:
这道题可以转化为求最小生成树的一道题,每个字符串可以看成一个点,每个字符串之间的 distance 距离便是这俩个点的权值。这里用P算法来做这道题,注意的是把邻接矩阵的对角线设为无穷大,即自己与自己的距离是无穷大。
代码:
import java.util.Scanner;public class Main {final static int MAXN = 2000 + 3;final static int INF = 100000 + 3;static int[][] arv = new int[MAXN][MAXN];static int[] used = new int[MAXN];static int[] lowCose = new int[MAXN];public static void main(String[] args){Scanner sc = new Scanner(System.in);int N;while(true){N = sc.nextInt();if(N == 0 ){break;}String[] str = new String[MAXN];for(int i = 1; i <= N; i++){str[i] = sc.next();}for(int i = 1; i <= N; i++){for(int j = i + 1; j <= N; j++){arv[i][j] = arv[j][i] = getDis(str[i], str[j]);}arv[i][i] = INF;}int ans = prim(1,N);System.out.print("The highest possible quality is 1/");System.out.println(ans + ".");}sc.close();}public static int getDis(String a, String b){int dis = 0;char[] ca = a.toCharArray();char[] cb = b.toCharArray();for(int i = 0; i < a.length(); i++){if(ca[i] != cb[i]){dis++;}}return dis;}public static int prim(int sta, int N){for(int i = 1; i <= N; i++){lowCose[i] = arv[sta][i];used[i] = 0;}used[sta] = 1;int cntWgt = 0;for(int i = 1; i <= N -1; i++){int v = -1;int mini = INF;for(int j = 1; j <= N; j++){if(used[j] == 0 && mini > lowCose[j]){mini = lowCose[j];v = j;}}if(v != -1){cntWgt += lowCose[v];used[v] = 1;for(int j = 1; j <= N; j++){if(used[j] == 0 && lowCose[j] > arv[v][j]){lowCose[j] = arv[v][j];}}}}return cntWgt;}}
0 0
- POJ 1789 Truck History
- POJ 1789 Truck History
- POJ 1789 Truck History
- poj 1789 Truck History
- poj 1789 Truck History
- Poj 1789 Truck History
- poj 1789 Truck History
- poj 1789 Truck History
- poj 1789 Truck History
- poj 1789 Truck History
- POJ-1789-Truck History
- poj-1789-Truck History
- POJ 1789 Truck History
- poj 1789 Truck History
- POJ 1789 Truck History
- POJ 1789 Truck History
- POJ 1789 Truck History
- POJ 1789 Truck History
- C语言数组实现栈操作
- 配置LVS + Keepalived高可用负载均衡集群之图文教程
- 回调函数
- 希尔排序
- 老旧电商系统升级改造日记 - 2. 数据导入,然后搞定硬编码问题
- POJ 1789Truck History
- Java---设计模块(设计模块的简介及最简单的俩个单例代码加测试)
- 可视化MNIST之降维探索Visualizing MNIST: An Exploration of Dimensionality Reduction-2
- 《古文字学》学习
- UFLDL Tutorial 笔记
- Android开发中的一个小功能 清空搜索框的文字
- Android开发:解决android:gravity不能居中问题
- Micro2440数据传输---串口通信
- ubuntu下安装编译链