Truck History最小生成树
作者:互联网
Truck History
原题链接https://vjudge.net/contest/352170#problem/E
题意为有 n行字母 他们之间的距离就是不同字母的个数,相当于题目将所有的地点给出,我们只需要枚举写出地图,正常计算即可。
注意Kruskal很容易记忆化超限,
Prim:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
long long map[3005][3005];
long long dis[300005];
bool vis[300005];
long long ans;
long long n;
long long maxx;
struct node
{
char s[35];
}stu[39005];
void prim()
{
ans = 0;
long long i, j;
for (i = 1; i <= n; i++)
{
dis[i] = map[1][i];
vis[i] = false;
}
vis[1] = true;
for (i = 0; i < n - 1; i++)
{
long long minn = INF;
long long p = 1;
for (j = 1; j <= n; j++)
{
if (!vis[j] && dis[j] < minn)
{
minn = dis[j];
p = j;
}
}
// cout << "***" << endl;
// for (j = 1; j <= n; j++)
// {
// cout << dis[j] << " ";
// }
// cout << endl;
if (minn == INF)
{
ans = -1;
return;
}
ans += minn;
vis[p] = true;
for (j = 1; j <= n; j++)
{
if (!vis[j] && dis[j] > map[p][j])
{
dis[j] = map[p][j];
}
}
}
return;
}
int main()
{
while(~scanf("%lld",&n))
{
if(n==0)
{
break;
}
long long i, j;
for (i = 1; i <= n;i++)
{
scanf("%s", stu[i].s);
}
for (i = 1; i <= n;i++)
{
for (j = 1; j <= n;j++)
{
if (i == j)
{
map[i][j] = 0;
}
else
{
long long z,sum = 0;
for (z = 0; z < 7;z++)
{
if(stu[i].s[z]!=stu[j].s[z])//判断每两个字符串之间不同字母的个数。
{
sum++;
}
}
map[i][j] = sum;
map[j][i] = sum;
}
}
}
prim();
printf("The highest possible quality is 1/%lld.\n", ans);
}
return 0;
}
Kruskal:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
using namespace std;
long long pre[300005];
char map[3005][20];
struct node
{
long long u;
long long v;
long long w;
} stu[3000005];
long long zz;
long long cnt;
long long ans;
bool cmp(node x, node y)
{
return x.w < y.w;
}
long long find(long long x)
{
if (x == pre[x])
{
return x;
}
return pre[x] = find(pre[x]);
}
void join(long long x, long long y, long long w)
{
long long ss1 = find(x);
long long ss2 = find(y);
if (ss2 != ss1)
{
pre[ss2] = ss1;
cnt--;
ans += w;
}
}
int main()
{
long long n, i, j;
while (~scanf("%lld", &n))
{
if (n == 0)
{
break;
}
ans = 0;
cnt = n;
long long num = 1;
for (i = 0; i <= n; i++)
{
pre[i] = i;
}
for (i = 1; i <= n; i++)
{
scanf("%s", map[i]);
}
// for (i = 1; i <= n; i++)
// {
// cout << map[i] << endl;
// }
long long k = 0;
for (i = 1; i < n; i++)
{
for (j = i + 1; j <= n; j++)
{
stu[k].u = i;
stu[k].v = j;
long long sum = 0;
// cout << map[i] << " " << map[j] << endl;
for (zz = 0; zz < 7; zz++)
{
if (map[i][zz] != map[j][zz])
{
sum++;
}
}
stu[k].w = sum;//这里注意直接读取结果,不要装进二维数组中再读取,会记忆化超限。
// cout << "sum=" << sum << endl;
k++;
}
}
sort(stu, stu + k, cmp);
//for (i = 0; i < k; i++)
//{
// cout << stu[i].u << " " << stu[i].v << " " << stu[i].w << endl;
//}
for (i = 0; i < k; i++)
{
join(stu[i].u, stu[i].v, stu[i].w);
if (cnt == 1)
{
break;
}
}
printf("The highest possible quality is 1/%lld.\n", ans);
}
return 0;
yeyuluo
发布了137 篇原创文章 · 获赞 3 · 访问量 1948
私信
关注
标签:pre,return,最小,long,Truck,ans,include,find,History 来源: https://blog.csdn.net/yeyuluo/article/details/104064000