您的位置:首页 > 编程语言 > C语言/C++

Truck History

2013-04-21 15:35 148 查看
H -
Truck History

poj1789-Truck History

这道题真TMD恶心,find函数直接使用return居然直接TIL,分开写就AC了,之前我还以为是因为用了C++输入输出导致错误,害得我试了好多次

// File Name: poj1754.cpp
// Author: rudolf
// Created Time: 2013年04月21日 星期日 14时31分58秒

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
//const int maxn=2010;
using namespace std;
const int maxn=2012;
int fa[maxn];
char mapp[maxn][10];

struct node
{
int x,y,value;
bool operator<(const node & a)const
{
return value <a.value;
}
}edge[maxn*(maxn-1)/2];

void init()
{
for(int i=0;i<maxn;i++)
fa[i]=i;
}

int find(int x)
{
// return x==fa[x]?x:find(fa[x]);
if(x==fa[x])
return x;
return fa[x]=find(fa[x]);
}
int Kruscal(int k)
{
init();
sort(edge,edge+k);
int ans=0;
for(int i=0;i<k;i++)
{
int x1=find(edge[i].x);
int x2=find(edge[i].y);
if(x1!=x2)
{
fa[x1]=x2;
ans+=edge[i].value;
}
}
return ans;
}

int Dist(char *c1,char *c2)
{
int ans=0;
for(int i=0;i<7;i++)
{
if(c1[i]!=c2[i])
ans++;
}
return ans;
}

int main()
{
int n;
while(cin>>n,n)
{
int k=0;
for(int i=0;i<n;i++)
{
cin>>mapp[i];
for(int j=0;j<i;j++)
{
edge[k].x=i;
edge[k].y=j;
edge[k].value=Dist(mapp[i],mapp[j]);
k++;
}
}
cout<<"The highest possible quality is 1/"<<Kruscal(k)<<"."<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息