Gym 100703A(最小生成树)
来源:互联网 发布:物业app软件取名 编辑:程序博客网 时间:2024/06/05 15:01
这道题题意就是给你n个长度为m的字符串,然后每两个字符串之间的距离是对应的每位的距离的最大值,然后把这些字符串连成一棵树,问这棵树最大边的最小值,其实就是求最小生成树的最大边,代码如下
#include <iostream>#include <string.h>#include <algorithm>using namespace std;const int maxn=1005;const int INF=0x3f3f3f3f;bool vis[maxn];int lowc[maxn];int pre[maxn];int Max[maxn][maxn];bool used[maxn][maxn];char s[maxn][50];int g[maxn][maxn];void prim(int cost[][maxn],int n){ int ans=0; memset(vis,false,sizeof(vis)); memset(Max,0,sizeof(Max)); memset(used,false,sizeof(used)); vis[0]=true; pre[0]=-1; for(int i=1; i<n; i++) { lowc[i]=cost[0][i]; pre[i]=0; } lowc[0]=0; for(int i=1; i<n; i++) { int minc=INF; int p=-1; for(int j=0; j<n; j++) if(!vis[j]&&minc>lowc[j]) { minc=lowc[j]; p=j; } //if(minc==INF) // return -1; ans+=minc; vis[p]=true; used[p][pre[p]]=used[pre[p]][p]=true; for(int j=0; j<n; j++) { if(vis[j]&&j!=p) { Max[j][p]=Max[p][j]=max(Max[j][pre[p]],lowc[p]); // cout<<Max[j][p]<<endl; } if(!vis[j]&&lowc[j]>cost[p][j]) { lowc[j]=cost[p][j]; pre[j]=p; } } }}int main(){ ios::sync_with_stdio(false); int n,m; while(cin>>n>>m) { int sum=0,ans=0; memset(s,0,sizeof(s)); memset(g,0,sizeof(g)); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { cin>>s[i][j]; } } for(int i=0; i<n; i++) { for(int k=i+1; k<n; k++) { for(int j=0; j<m; j++) { int tmp=abs((s[i][j]-'a')-(s[k][j]-'a')); ans=max(ans,tmp); } g[i][k]=g[k][i]=ans; ans=0; } } /* for(int i=0;i<n;i++) { for(int j=0;j<n;j++) cout<<g[i][j]<<" "; cout<<endl; }*/ prim(g,n); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(i==j) continue; sum=max(sum,Max[i][j]); // cout<<Max[i][j]<<endl; } } cout<<sum<<endl; } return 0;}
0 0
- Gym 100703A(最小生成树)
- Gym 100703A Tea-drinking 最小生成树
- Gym 100703A Tea-drinking 最小生成树
- A - Tea-drinking-Gym 100703A-优先队列/最小生成树
- Gym - 100203H Highways 最小生成树
- GYM 100712 F.Travelling Salesman(最小生成树-Kruskal)
- Gym 100803F There is No Alternative(最小生成树)
- poj_2031Building a Space Station(最小生成树)
- A - 畅通工程(最小生成树)
- POJ2031_Building a Space Station(最小生成树)
- poj2031Building a Space Station 最小生成树
- codeforces 76A Gift 最小生成树
- Building a Space Station--最小生成树
- poj1751 Highways 最小生成树(小组赛A)
- 【最小生成树入门专题1】A
- 【codeforces】gym 101137 K - Knights of the Old Republic【用最小生成树对图做集合dp】
- codeforces gym 2016-2017 NEERC, Moscow Subregional K. Knights of the Old Republic 最小生成树+dp
- HDU 5723-A - Abandoned country-最小生成树+树dp
- 51Nod 1073 - 约瑟夫环(数论)
- 稀疏自动编码器 (Sparse Autoencoder)
- <base>标签在制做Iframe管理系统框架
- python内置函数02
- sql查询语句
- Gym 100703A(最小生成树)
- mysql学习
- 第3周项目3-求集合并集
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends(水题)
- 1024!末尾有几个0
- c/c++ AVLTree 平衡二叉树
- HDU 1271 整数对
- CMD help
- 51nod 1057 N的阶乘(大数-划分)