poj1789(MST)
来源:互联网 发布:手机财经软件 编辑:程序博客网 时间:2024/06/15 15:56
思路:题目的意思就是求解一个源点到所有点距离之和最小,然后就是这个距离怎么计算就是两个字符串对应位置上不同字母的个数的多少,然后跑kruskal;
点击题目链接
/*****************************************Author :Crazy_AC(JamesQi)Time :2015File Name :*****************************************/// #pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <algorithm>#include <iomanip>#include <sstream>#include <string>#include <stack>#include <queue>#include <deque>#include <vector>#include <map>#include <set>#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <limits.h>using namespace std;#define MEM(a,b) memset(a,b,sizeof a)typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> ii;const int inf = 1 << 30;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;char s[2010][2010];int F[2010];int n;struct Edge{int u,v,w;bool operator < (const Edge& b)const{return w < b.w;}}E[2000 * 2000];int get(int i,int j){int num = 0;for (int k = 0;k < strlen(s[i]);k++)if (s[i][k] != s[j][k]) num++;return num;}int cnt;void Init(){cnt = 0;for (int i = 1;i <= n;i++){F[i] = i;for (int j = i + 1;j <= n;j++){int d = get(i,j);E[cnt].u = i;E[cnt].v = j;E[cnt].w = d;cnt++;}}}int Find(int x){if (F[x] == x) return x;else return F[x] = Find(F[x]);}void Kruskal(){sort(E,E + cnt);int sum = 0;int tol = 0;for (int i = 0;i < cnt;i++){int t1 = Find(E[i].u);int t2 = Find(E[i].v);if (t1 != t2){F[t1] = t2;sum += E[i].w;tol++;}if (tol == n - 1) break;}printf("The highest possible quality is 1/%d.\n",sum);}int main(){// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);while(scanf("%d",&n) != EOF && n){for (int i = 1;i <= n;i++)scanf("%s",s[i]);Init();Kruskal();}return 0;}
0 0
- poj1789(MST)
- 【POJ1789】【MST】【n^2 prime】
- Truck History poj1789 (prim算法 MST)
- prim算法(poj1789)
- poj1789(prim)
- poj1789
- poj1789
- poj1789
- poj1789
- poj1789
- POJ1789
- poj1789
- poj1789
- poj1789
- poj1789
- POJ1789
- POJ1789
- poj1789
- 不重叠模式串个数 KMP hdu 2087 剪花布条
- Unity3D:spine使用
- 初等数论四大定理(威尔逊定理,欧拉定理,中国剩余定理,费马小定理)
- map进行字符串的查找
- 解决Mysql乱码问题
- poj1789(MST)
- SSL,TLS,IPSec 和HTTPS
- cell背景颜色设置
- MySQL 日期时间函数
- SVN常用命令
- UIView的layoutSubviews和drawRect
- 单例模式,完全版
- 操作系统学习3
- Linux Shell 通配符、元字符、转义符使用实例介绍