[HDU] [1875] Smooth Project Renewed

Problem Description

Loading...

Problem Analysis

This problem was also not easy to finish…
The idea is to calculate the edge length corresponding to each point from the coordinates again.

However, the constraints in this problem really caused me a fair amount of trouble… After all, I was not very proficient with applying this yet, and the implementation I used was a relatively compact one… which left me with no idea how to modify it according to the problem statement. But I kept forcing myself through it… and finally found a breakthrough…

First, the distance restriction
Add a check when updating the closest distance to the tree
Second, whether a tree is formed in the end
Based on this kind of check in the previous <一>
it can cause inf to possibly be added during sum +=
That is, when it is possible to add inf
it also means that after searching all possible edges, no tree can be formed
So we can break and output

Successfully AC…
Feels like I’m about to drop dead…

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct xy {
	int x,y;
} a[120];
int main() {
	double dis[120];
	double e[120][120];
	bool flag[120];
	double inf=999999;
	int T;
	scanf("%d",&T);
	while(T--) {
		int N;
		scanf("%d",&N);
		for(int i=1; i<=N; i++) {
			dis[i]=inf;
			flag[i]=false;
		}
		for(int i=1; i<=N; i++) {
			for(int j=1; j<=N; j++) {
				e[i][j]=inf;
			}
		}
		for(int i=1; i<=N; i++) {
			scanf("%d %d",&a[i].x,&a[i].y);
			for(int j=1; j<=N; j++) {
				if(i==j)
					continue;
				else {
					double l=sqrt(pow(a[i].x-a[j].x,2)+pow(a[i].y-a[j].y,2));
					e[i][j]=l;
					e[j][i]=l;
				}
			}
		}
		double sum=0;
		dis[1]=0;
		bool x=false;
		while(true) {
			int v=-1;
			for(int u=1; u<=N; u++) {
				if(!flag[u]&&(v==-1||dis[u]<dis[v]))
					v=u;
			}
			if(v==-1)
				break;
			flag[v]=true;
			if(dis[v]==inf) {
				x=true;
				break;
			} else
				sum+=dis[v];
			for(int u=1; u<=N; u++) {
				if(e[v][u]<10||e[v][u]>1000) {
					continue;
				}
				dis[u]=min(dis[u],e[v][u]);
			}
		}
		if(x)
			printf("oh!\n");
		else
			printf("%.1lf\n",sum*100);
	}
	return 0;
}

Problem link: HDU 1875 Smooth Project Continued

Loading...