poj1789(Truck History)

题目大意:

    题目意思一个卡车类型由7个字符构成,每个字符所在的位置有它特定的含义,这里有一个Q的含义为N个不同卡车之间7个字符之间相同位置不同字符的个数cnt之和,求出最小的Q(存在一种N哥卡车两两比较后cnt之和最小)。

解题思路;

   主要是理解题意,把卡车看成编号为(1-N)的数,两两比较卡车的cnt值为编号x-y的cost值。然后分析后建图,因为求最小的Q值意为求图中生成树的最小权值就是求最小生成树。

代码:

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <sstream>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <cstdio>
  7 #include <string>
  8 #include <bitset>
  9 #include <vector>
 10 #include <queue>
 11 #include <stack>
 12 #include <cmath>
 13 #include <list>
 14 //#include <map>
 15 #include <set>
 16 using namespace std;
 17 /***************************************/
 18 #define ll long long
 19 #define int64 __int64
 20 /***************************************/
 21 const int INF = 0x7f7f7f7f;
 22 const double eps = 1e-8;
 23 const double PIE=acos(-1.0);
 24 const int d1x[]= {0,-1,0,1};
 25 const int d1y[]= {-1,0,1,0};
 26 const int d2x[]= {0,-1,0,1};
 27 const int d2y[]= {1,0,-1,0};
 28 const int fx[]= {-1,-1,-1,0,0,1,1,1};
 29 const int fy[]= {-1,0,1,-1,1,-1,0,1};
 30 /***************************************/
 31 void openfile()
 32 {
 33     freopen("data.in","rb",stdin);
 34     freopen("data.out","wb",stdout);
 35 }
 36 /**********************华丽丽的分割线,以上为模板部分*****************/
 37 int map[3000][3000];
 38 int lowcost[3000];
 39 int vis[3000];
 40 int n;
 41 int ce;
 42 int maax;
 43 int dijstra()
 44 {
 45     int i,j,k=0;
 46     int cnt=0;
 47     int min;
 48     for(i=1; i<=n; i++)
 49     {
 50         lowcost[i]=map[1][i];
 51         vis[i]=0;
 52     }
 53     vis[1]=1;
 54     // lowcost[sta]=0;
 55     for(i=1; i<n; i++)
 56     {
 57         min=INF;
 58         for(j=1; j<=n; j++)
 59         {
 60             if (!vis[j]&&lowcost[j]<min)
 61             {
 62                  min=lowcost[j];
 63                  k=j;
 64             }
 65         }
 66         vis[k]=1;
 67         cnt+=min;
 68         for(j=1;j<=n;j++)
 69             if(!vis[j]&&lowcost[j]>map[k][j])
 70                 lowcost[j]=map[k][j];
 71     }
 72     return cnt;
 73 }
 74 int main()
 75 {
 76     while(scanf("%d",&n)&&n)
 77     {
 78         char a[3000][7];
 79         int i,j,k,cnt=0;
 80         int Q;
 81         for(i=0;i<=n;i++)
 82             for(j=0;j<=n;j++)
 83                map[i][j]=INF;
 84         for(i=1;i<=n;i++)
 85         {
 86             scanf("%s",a[i]);
 87             for(cnt=0,j=i-1;j>=1;j--)
 88             {
 89                 for(k=0;k<7;k++)
 90                 {
 91                     if (a[i][k]!=a[j][k])
 92                         cnt++;
 93                 }
 94                 map[i][j]=cnt;
 95                 map[j][i]=cnt;
 96                 cnt=0;
 97             }
 98         }
 99         Q=dijstra();
100         printf("The highest possible quality is 1/%d.
",Q);
101     }
102     return 0;
103 }
View Code
屌丝终有逆袭日,*******。
原文地址:https://www.cnblogs.com/ZhaoPengkinghold/p/3833982.html