Re: 10034 - Freckles
Posted: Fri May 28, 2010 10:43 am
got AC..didnt commented out freopen and tht ws causing problem.. 

Code: Select all
#include <cstdlib>
#include<stdio.h>
#include<math.h>
using namespace std;
int main() {
int tc, n,i,j,k,count;
float **co,**gm,*nr,*d,res=0;
scanf("%d",&tc);
while(tc--){
scanf("%d",&n);
count=1;
co = new float*[(n + 1)*(3)];
gm = new float*[(n + 2)*(n + 2)];
for (i = 0; i <= n; i++) {
co[i] = new float[2];
gm[i] = new float[n + 1];
}
while(count<=n){
scanf("%f %f", &co[count][0],&co[count][1]);
count++;
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
float x=co[i][0]-co[j][0];
float y=co[i][1]-co[j][1];
gm[i][j]=sqrt(x * x + y*y); //x*x+y*y;
}
}
d=new float[n+1];
nr=new float[n+1];
for (i = 2; i <= n; i++) {
nr[i] = 1;
d[i] = gm[1][i];
}
count = 0;
int vnear;
float min;
while (count != n - 1) {
min = 9999;
for (i = 2; i <= n; i++) {
if (0 <= d[i] && d[i] < min) {
min = d[i];
vnear = i;
}
}
d[vnear] = -1;
res+=min;
for (i = 2; i <= n; i++) {
if (d[i] > gm[i][vnear]) {
d[i] = gm[i][vnear];
nr[i] = vnear;
}
}
count++;
}
printf("%.2f\n" ,res);
if(tc>0)
printf("\n");
}
return 0;
}
Code: Select all
3
3
1.0 1.0
2.0 2.0
2.0 4.0
1
1.0 1.0
2
0.0 0.0
2.0 2.0
Code: Select all
3.41
0.00
2.83
Code: Select all
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
Scanner scanner = new Scanner(reader);
try {
int cases = scanner.nextInt();
for (int i = 0; i < cases; i++) {
int frickles = scanner.nextInt();
double[][] vertexes = new double[frickles][2];
List<Edge> edges = new ArrayList<Edge>();
double previousX = scanner.nextDouble();
double previousY = scanner.nextDouble();
vertexes[0][0] = previousX;
vertexes[0][1] = previousY;
for (int j = 0; j < frickles - 1; j++) {
double x = scanner.nextDouble();
double y = scanner.nextDouble();
for (int k = 0; k <= j; k++) {
double[] vertex = vertexes[k];
edges.add(new Edge(j + 1, k, getLength(x, y, vertex[0], vertex[1])));
}
vertexes[j + 1][0] = x;
vertexes[j + 1][1] = y;
}
Collections.sort(edges);
Set<Integer> visitedVertex = new HashSet<Integer>();
double length = 0;
for (Edge edge : edges) {
if (visitedVertex.contains(edge.start) && visitedVertex.contains(edge.end)) {
continue;
}
visitedVertex.add(edge.start);
visitedVertex.add(edge.end);
length += edge.length;
if (visitedVertex.size() == frickles) {
break;
}
}
writer.write(String.format("%.2f", length));
if (i != cases - 1) {
writer.write("\n\n");
}
}
writer.flush();
} catch (IOException e) {
}
}
private static double getLength(double x, double y, double previousX, double previousY) {
double xLength = Math.abs(x - previousX);
double yLength = Math.abs(y - previousY);
return Math.sqrt(xLength * xLength + yLength * yLength);
}
static class Edge implements Comparable<Edge> {
int start;
int end;
Double length;
Edge(int start, int end, double length) {
this.start = start;
this.end = end;
this.length = length;
}
@Override
public String toString() {
return "Edge{" +
"start=" + start +
", end=" + end +
", length=" + length +
'}';
}
public int compareTo(Edge o) {
return length.compareTo(o.length);
}
}
}
Code: Select all
1
4
1.0 1.0
1.0 2.0
10.0 10.0
10.0 11.0
Code: Select all
2
3
1.0 1.0
2.0 2.0
2.0 4.0
3
1.0 1.0
2.0 2.0
2.0 4.0
Code: Select all
3.41
3.41
Code: Select all
AC.
While(1) Return Thanks.. :) ;
Top Secret wrote:Why wa? I used Kruskal's Mst algo.. I think its presentation error..
Code: Select all
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <utility>
#include <cmath>
using namespace std;
#define MAX 101
double freckles[MAX][2];
int u, v, cases;
double mst_cost;
vector<int> pset(1000);
priority_queue< pair< double, pair <double, double> > > edge_list;
double distance(double x1, double y1, double x2, double y2)
{
return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
void read_input()
{
scanf("%d", &v);
memset(freckles, 0, sizeof freckles);
for (u = 0; u < v; u++)
{
scanf("%lf %lf", &freckles[u][0], &freckles[u][1]);
if (u == 0) continue;
edge_list.push(make_pair(-distance(freckles[u - 1][0], freckles[u - 1][1],
freckles[u][0], freckles[u][1]),
make_pair(u, u - 1)));
}
edge_list.push(make_pair(-distance(freckles[v - 1][0], freckles[v - 1][1],
freckles[0][0], freckles[0][1]),
make_pair(v - 1, 0)));
}
void init_set(int _size)
{
pset.resize(_size);
int i;
for (i = 0; i < _size; i++)
{
pset[i] = i;
}
}
double find_set(int i)
{
if (pset[i] == i)
{
return i;
}
else
{
return pset[i] = find_set(pset[i]);
}
}
void union_set(int i, int j)
{
pset[find_set(i)] = find_set(j);
}
bool is_same_set(int i, int j)
{
return find_set(i) == find_set(j);
}
int main()
{
scanf("%d\n", &cases);
int i;
for (i = 0; i < cases; i++)
{
read_input();
mst_cost = 0;
init_set(v);
while (!edge_list.empty())
{
pair< double, pair <double, double> > front = edge_list.top();
edge_list.pop();
if (!is_same_set(front.second.first, front.second.second))
{
mst_cost += (-front.first);
union_set(front.second.first, front.second.second);
}
}
if (i == cases - 1)
printf("%0.2lf\n", mst_cost);
else
printf("%0.2lf\n\n", mst_cost);
}
return 0;
}
Code: Select all
9
3
4.0 2.0
2.0 2.0
1.0 1.0
3
1.0 1.0
2.0 2.0
3.0 3.0
3
1.0 1.0
2.0 2.0
2.0 4.0
2
1.0 1.0
2.0 2.0
2
-1.0 -1.0
1.0 -1.0
1
1.0 1.0
2
2.0 2.0
2.0 2.0
4
1.0 1.0
1.0 2.0
10.0 10.0
10.0 11.0
1
1.0 -1.0
Code: Select all
3.41
2.83
3.41
1.41
2.00
0.00
0.00
14.04
0.00