标签:style class blog code http tar
题目链接:Truck History
题意就是N个卡车的型号,一代一代的发展,两辆卡车的型号中 不同字母的个数代表着两辆卡车的距离,确定一个点,遍历到所有的点,使之这个距离最小。
很明显最小生成树,稠密图,1次AC,水过
PS:厚颜无耻的先看了Discuss才做的,否则我也漏“.”,罪过罪过
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> const int N = 2001; const int INF = 1e8; using namespace std; int mapp[N][N]; int n,ans; int dis[N]; bool vis[N]; char a[N][8]; void init() { memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); } int SUM(int x,int y) { int sum = 0; for(int i = 0;i<7;i++) { if(a[x][i]!=a[y][i]) sum++; } return sum; } void Prim() { ans = 0; int pos,minn; for(int i = 0;i<n;i++) dis[i] = mapp[0][i]; vis[0] = true; for(int i = 0;i<n;i++) { minn = INF; for(int j = 0;j<n;j++) { if(!vis[j] && dis[j]<minn) { pos = j; minn = dis[j]; } } if(minn!=INF) ans += minn; vis[pos] = true; for(int j = 0;j<n;j++) { if(!vis[j] && dis[j] > mapp[pos][j]) dis[j] = mapp[pos][j]; } } } int main() { while(cin>>n && n) { init(); for(int i = 0;i<n;i++) cin>>a[i]; int i,j; for( i = 0;i<n;i++) { for(j = 0;j<n;j++) { if(i==j) mapp[i][j] = 0; else mapp[i][j] = SUM(i,j); } } Prim(); cout<<"The highest possible quality is 1/"<<ans<<'.'<<endl; } return 0; }
POJ 1789-Truck History,布布扣,bubuko.com
标签:style class blog code http tar
原文地址:http://blog.csdn.net/wjw0130/article/details/31007551