Submission #2825655


Source Code Expand

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define inf 1e18

typedef struct {
	int u;
	int v;
	double d;
}edge;

typedef struct {
	int N;
	int *u;
	int *u_rank;
}union_find;

union_find *make_union_find(int N){
	int i;
	union_find *uf = (union_find *)malloc(sizeof(union_find));
	uf->N = N;
	uf->u = (int *)malloc(sizeof(int) * N);
	uf->u_rank = (int *)malloc(sizeof(int) * N);
	for(i = 0; i < N; i++){
		(uf->u)[i] = i;
		(uf->u_rank)[i] = 1;
	}
	return uf;
}

void flush_uf(union_find *uf){
	int i;
	for(i = 0; i < uf->N; i++){
		uf->u[i] = i;
		uf->u_rank[i] = 1;
	}
}

int root_uf(int x, union_find *uf){
	int *u = uf->u;
	if(u[x] == x){
		return x;
	}
	else{
		u[x] = root_uf(u[x], uf);
		return u[x];
	}
}

void combine_uf(int x, int y, union_find *uf){
	int x_root = root_uf(x, uf);
	int y_root = root_uf(y, uf);
	int *u = uf->u;
	int *u_rank = uf->u_rank;
	if(x_root == y_root){
		return;
	}
	else if(u_rank[x_root] < u_rank[y_root]){
		u[x_root] = y_root;
		u_rank[y_root] += u_rank[x_root];
		u_rank[x_root] = 0;
	}
	else{
		u[y_root] = x_root;
		u_rank[x_root] += u_rank[y_root];
		u_rank[y_root] = 0;
	}
}

//xとyが同じ集合に属していれば1を,そうでなければ0を返す
int is_same_union_uf(int x, int y, union_find *uf){
	if(root_uf(x, uf) == root_uf(y, uf)){
		return 1;
	}
	else{
		return 0;
	}
}

//xが属する集合の要素数を返す
int rank_uf(int x, union_find *uf){
	return (uf->u_rank)[root_uf(x, uf)];
}

int compare(const void *a, const void *b){
	if(((edge *)a)->d - ((edge *)b)->d < 0){
		return -1;
	}
	else if(((edge *)a)->d - ((edge *)b)->d > 0){
		return 1;
	}
	else{
		return 0;
	}
}

double max(double a, double b){
	return a >= b ? a : b;
}

double min(double a, double b){
	return a <= b ? a : b;
}

int main(){
	int N, i, j, k, l;
	scanf("%d", &N);
	double *x = (double *)malloc(sizeof(double) * N);
	double *y = (double *)malloc(sizeof(double) * N);
	double *a = (double *)malloc(sizeof(double) * N);
	for(i = 0; i < N; i++){
		scanf("%lf%lf%lf", &x[i], &y[i], &a[i]);
	}
	edge *e = (edge *)malloc(sizeof(edge) * ((N * (N - 1)) / 2));
	double *amaxmin = (double *)malloc(sizeof(double) * (1 << N));
	union_find *uf = make_union_find(N);
	double mst, asum;
	int V;
	amaxmin[0] = inf;
	for(i = 1; i < (1 << N); i++){
//		printf("i = %d\n", i);
		mst = 0;
		asum = 0;
		V = 0;
		l = 0;
		for(j = 0; j < N; j++){
			if((i | (1 << j)) == i){
				for(k = j + 1; k < N; k++){
					if((i | (1 << k)) == i){
						e[l].u = j;
						e[l].v = k;
						e[l].d = hypot(x[j] - x[k], y[j] - y[k]);
						l++;
					}
				}
				asum += a[j];
				V++;
			}
		}
		qsort(e, l, sizeof(edge), compare);
//		printf("l = %d\n", l);
		flush_uf(uf);
		for(j = 0; j < l; j++){
			if(is_same_union_uf(e[j].u, e[j].v, uf) == 0){
				combine_uf(e[j].u, e[j].v, uf);
				mst += e[j].d;
			}
		}
		amaxmin[i] = (asum - mst) / V;
//		printf("(mst, asum, V, amaxmin) = (%lf, %lf, %d, %lf)\n", mst, asum, V, amaxmin[i]);
	}
	double *dp = (double *)malloc(sizeof(double) * (1 << N));
	dp[0] = inf;
	for(i = 1; i < (1 << N); i++){
		dp[i] = amaxmin[i];
		for(j = (i & (i - 1)); j > 0; j = (i & (j - 1))){
			dp[i] = max(dp[i], min(dp[j], dp[i ^ j]));
		}
	}
	printf("%.12lf\n", dp[(1 << N) - 1]);
	return 0;
}

Submission Info

Submission Time
Task E - Water Distribution
User abc050
Language C (GCC 5.4.1)
Score 1000
Code Size 3405 Byte
Status AC
Exec Time 107 ms
Memory 764 KB

Compile Error

./Main.c: In function ‘main’:
./Main.c:107:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ^
./Main.c:112:3: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lf%lf%lf", &x[i], &y[i], &a[i]);
   ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 2
AC × 46
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 105 ms 636 KB
001.txt AC 105 ms 636 KB
002.txt AC 104 ms 764 KB
003.txt AC 106 ms 636 KB
004.txt AC 106 ms 764 KB
005.txt AC 103 ms 764 KB
006.txt AC 105 ms 636 KB
007.txt AC 104 ms 636 KB
008.txt AC 104 ms 636 KB
009.txt AC 106 ms 636 KB
010.txt AC 106 ms 764 KB
011.txt AC 104 ms 636 KB
012.txt AC 106 ms 636 KB
013.txt AC 105 ms 636 KB
014.txt AC 105 ms 636 KB
015.txt AC 105 ms 636 KB
016.txt AC 104 ms 636 KB
017.txt AC 105 ms 764 KB
018.txt AC 105 ms 636 KB
019.txt AC 105 ms 636 KB
020.txt AC 105 ms 764 KB
021.txt AC 104 ms 636 KB
022.txt AC 105 ms 636 KB
023.txt AC 105 ms 636 KB
024.txt AC 106 ms 636 KB
025.txt AC 104 ms 636 KB
026.txt AC 104 ms 636 KB
027.txt AC 104 ms 636 KB
028.txt AC 105 ms 636 KB
029.txt AC 106 ms 636 KB
030.txt AC 107 ms 636 KB
031.txt AC 105 ms 636 KB
032.txt AC 105 ms 636 KB
033.txt AC 106 ms 636 KB
034.txt AC 105 ms 764 KB
035.txt AC 104 ms 636 KB
036.txt AC 103 ms 636 KB
037.txt AC 106 ms 636 KB
038.txt AC 104 ms 636 KB
039.txt AC 104 ms 764 KB
040.txt AC 1 ms 128 KB
041.txt AC 1 ms 128 KB
042.txt AC 1 ms 128 KB
043.txt AC 1 ms 128 KB
example0.txt AC 1 ms 128 KB
example1.txt AC 104 ms 636 KB