题目连接
Prime
/*
* @Author: Achan
* @Date: 2018-10-26 17:09:43
* @Last Modified by: Achan
* @Last Modified time: 2018-10-29 17:27:28
*/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
#define X first
#define Y second
#define eps 1e-2
#define gcd __gcd
#define pb push_back
#define PI acos(-1.0)
#define lowbit(x) (x)&(-x)
#define fin freopen("in.txt","r",stdin);
#define fout freopen("out.txt","w",stdout);
#define bug printf("!!!!!\n");
#define mem(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<(int)k;i++)
#define per(i,j,k) for(int i=j;i<=(int)k;i++)
#define pset(x) setiosflags(ios::fixed)<<setprecision(x)
#define io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
typedef long long ll;
typedef long double LD;
typedef pair<int,int> pii;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') { if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int inf = 1<<30;
const ll INF = 1e18 ;
const int mod = 1e9+7;
const int maxn = 100+2;
struct Point
{
int x;
int y;
}P[maxn];
double get_length(int a, int b)
{
int len1 = P[a].x - P[b].x;
int len2 = P[a].y - P[b].y;
return sqrt(len1*len1 + len2*len2);
}
struct edge
{
double w;
int to;
int nex;
}E[maxn*maxn];
int n;
int tot;
int head[maxn];
void init()
{
memset(head,-1,sizeof(head));
// fill(mp[0],mp[0]+maxn*maxn,inf);
tot = 0;
}
void add_edge(int u, int v, double w)
{
E[tot].w = w;
E[tot].to = v;
E[tot].nex = head[u];
head[u] = tot++;
}
bool vis[maxn];
double dis[maxn];
void Prim(int s)
{
fill(dis, dis + maxn, inf);
memset(vis, 0, sizeof(vis));
priority_queue <pair<double, int> > q;
q.push(make_pair(0.0,s));
double ans = 0; //最小生成树总长度
int count = 0; //统计加入点的数量 (区别kruskal加入的边)
while(!q.empty() && count<n)
{
int cur = q.top().second;
double w = - q.top().first;
q.pop();
if(vis[cur]) continue;
vis[cur] = 1;
count ++; //统计生成树的总结数
ans += w; // + 到下个新点的最短选择
for(int i=head[cur]; ~i; i=E[i].nex) //遍历邻接表
{
int to = E[i].to;
double tw = E[i].w;
if(!vis[to] && dis[to] > tw)
{
dis[to] = tw; //维护当前可行路,非三角更新!!
q.push(make_pair(-tw , to)); //可行队列
}
}
}
if(count != n) { puts("oh!"); return ;}
cout<<pset(1)<<ans*100.0<<endl;
}
int main(void)
{
//fin
//fout
io //cin不能和读入挂一起混用 , 如同printf() 不能与cout一起用
int T;T = read();
while(T--)
{
init();
n = read();
for(int i=1;i<=n;i++) P[i].x = read(), P[i].y = read();
// for(int i=1;i<=n;i++) cin>>P[i].x>>P[i].y;
double len;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
len = get_length(i,j);
if(len>= 10.0 && len <=1000.0) add_edge(i,j,len);
}
}
Prim(1);
}
}
Status: Accepted
Time: 31ms
Memory: 1920kB
Length: 2972
Lang: G++
Kruskal
/*
* @Author: Achan
* @Date: 2018-10-27 11:13:58
* @Last Modified by: Achan
* @Last Modified time: 2018-10-27 18:17:24
*/
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
#define X first
#define Y second
#define eps 1e-2
#define gcd __gcd
#define pb push_back
#define PI acos(-1.0)
#define lowbit(x) (x)&(-x)
#define fin freopen("in.txt","r",stdin);
#define fout freopen("out.txt","w",stdout);
#define bug printf("!!!!!\n");
#define mem(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<(int)k;i++)
#define per(i,j,k) for(int i=j;i<=(int)k;i++)
#define io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
typedef long long ll;
typedef long double LD;
typedef pair<int,int> pii;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1e18 ;
const int mod = 1e9+7;
const int maxn = 1e2+2;
const int mov[4][2] = {-1,0,1,0,0,1,0,-1};
const int Mov[8][2] = {-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') { if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct Pnode
{
int x;
int y;
}P[maxn];
double get_length(int a, int b)
{
int len1 = P[a].x - P[b].x;
int len2 = P[a].y - P[b].y;
return sqrt(len1*len1 + len2*len2);
}
struct node
{
int from;
int to;
double w;
}edge[maxn* maxn] ;
int tot , n;
int found[maxn];
inline void init() {tot = 0;}
void add_edge(int u, int v, double w)
{
edge[tot].from = u;
edge[tot].to = v;
edge[tot++].w = w;
}
inline bool cmp(node a, node b) { return a.w<b.w; }
inline int find(int x){return x== found[x]? x:find(found[x]);}
void kruskal()
{
for(int i=1;i<=n;i++) found[i] = i;
double ans = 0;
int count = 0;
for(int i=0;i<tot;i++)
{
int p1 = edge[i].from;
int p2 = edge[i].to;
p1 = find(p1);
p2 = find(p2);
if(p1!=p2)
{
ans += edge[i].w;
found[p1] = p2;
count ++;
}
if(count == n-1) break;
}
if(count != n-1) puts("oh!");
else cout<<setiosflags(ios::fixed)<<setprecision(1)<<ans*100.0<<endl;
}
int main(void)
{
//fin
// fout
io
int T;
T = read();
while(T--)
{
init();
n = read();
for(int i = 1;i<=n;i++)
P[i].x = read(), P[i].y = read();
for(int i=1;i<=n; i++)
for(int j = 1; j<=n ; j++)
{
double len = get_length(i,j);
if(len>=10.0 && len <= 1000.0)
add_edge(i,j,len);
}
sort(edge,edge+tot,cmp);
kruskal();
}
}
Status: Accepted
Time: 171ms
Memory: 1920kB
Length: 2972
Lang: G++