最小生成树专题 F - Truck History(prime模板算法)

题意 : 什么鬼的汽车公司有个惊人的发现,吧啦吧啦吧啦…………….然后就给你n个卡车的车牌(我就没见过这种车牌),每2辆车的车牌中,有多少个字母不同就代表着这两辆车经历的多少年的演变。然后就让你求所有的这些车至少经历了多少年的演变。

这道题类似于坐标构图,数据量有些大 ,然后就:
这里写图片描述
。。。不好意思贴错了,是这个:
这里写图片描述
妈耶,疯狂优化以后终于AC了。。。。
最后发现是被string类型的数组给卡住了
好吧。。。不习惯用char的我有点小难受。。。
下面是代码:

#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#define INF 1e7 + 5
using namespace std;

char s[2010][10];
int mpt[2010][2010] ;
  bool visited[100000];
int cost[10000] ;
int len( char n[] , char  m[]){
    int coun = 0 ;
   for(int i = 0 ; i < 7 ; i++){
    if(n[i] != m[i] ) coun++ ;
   }
   return coun ;
}
int n ;

 int prime(){ ///模板真香!!!!!
 for(int i = 1; i <= n; i++)
    cost[i] = mpt[1][i];
  int min;
 // index begin from 1 not 0
  int ans = 0;

  cost[1] = 0;
  visited[1] = true;
  for(size_t i = 1; i < n; ++i)//loop N - 1 times
  {
    min = INF;
    int k;
    for(int j = 1; j <= n; ++j)// find the minimun edge between two edge set
    {
      if(!visited[j] && min > cost[j])
      {
        min = cost[j];
        k = j;
      }
    }
    visited[k] = true;
    ans = ans + min ;
    for(int j = 1; j <= n; ++j)// update the array of lowcost
    {
      if(!visited[j] && cost[j] > mpt[k][j])
        cost[j] = mpt[k][j];
    }
  }
  printf("The highest possible quality is 1/%d.\n",ans) ;

}


int main(){

 while( scanf("%d",&n) ){
  if(n == 0) break ;
 memset(cost ,INF , sizeof(cost)) ;
  memset(visited, false, sizeof(visited));
  memset(mpt , INF , sizeof(mpt)) ;
 for(int i = 1 ; i <= n ; i++){
    cin >> s[i] ;

    for(int j = 0 ; j < i ; j++){
        int l = len(s[i] , s[j]) ;
        mpt[i][j] = l ;
        mpt[j][i] = l ;
    }
 }
  prime() ;
 }
}

猜你喜欢

转载自blog.csdn.net/qq_42894605/article/details/81740762