-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva216.cpp
173 lines (147 loc) · 4.94 KB
/
uva216.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Author: Golam Rahman Tushar
........Aust Cse 27th batch.........
*/
//{ Template
//{ C-headers
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <cctype>
#include <cassert>
#include <ctime>
//}
//{ C++-headers
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
//}
//}
//{ Floating-points
#define EPS DBL_EPSILON
#define abs(x) (((x) < 0) ? - (x) : (x))
#define zero(x) (abs (x) < EPS)
#define equal(a,b) (zero ((a) - (b)))
#define PI 2 * acos (0.0)
//}
#define INF 1<<29
#define ll long long
template <typename T>
std::string to_string(T const& value) {
stringstream sstr;
sstr << value;
return sstr.str();
}
//}
template <class T> T gcd(T a,T b){if(b==0) return a;else return gcd(b,a%b);}
template <class T> T lcm(T a,T b){return (a*b)/gcd(a,b);}
template <class T> T power( T a, T b){if(b==0) return 1; T x=a;for(T i=2;i<=b;i++) a=a*x;return a;}
template <class T> T BigMod(T a,T b,T c){if(a==0) return 0;if(b==0) return 1;if(b%2==0){T x=BigMod(a,b/2,c)%c;return (x*x)%c;}else return ((a%c)*BigMod(a,b-1,c)%c)%c;}
template <class T> T squ(T a) { return a*a; }
struct info {
int x, y;
}graph[10];
struct edge {
int u, v; double w;
bool operator < (const edge& c) const
{
return w<c.w;
}
};
vector<edge>v, ans;
int pre[10];
int find(int x)
{
return (pre[x]==x ? x: find(pre[x]));
}
int n, mp[157][157], track[10];
double cost;
vector<int>FINAL[10];
void mst()
{
int sz = v.size(), cnt = 0, i;
sort(v.begin(), v.end());
// for(i=0;i<sz;i++) cout<<v[i].u<<" "<<v[i].v<<" "<<v[i].w<<endl;
for(i=0;i<n;i++) { pre[i] = i; track[i] = 0; }
for(i=0;cnt<n-1&&i<sz;i++)
{
int u = find(v[i].u);
int vv = find(v[i].v);
if(u!=vv&&track[v[i].u]<2&&track[v[i].v]<2)
{
FINAL[v[i].u].push_back(v[i].v);
FINAL[v[i].v].push_back(v[i].u);
pre[u] = vv; track[v[i].u]++; track[v[i].v]++;
cost+=v[i].w;
ans.push_back(v[i]);
cnt++;
}
}
//cout<<"i : "<<i<<endl;
}
int main ()
{
int i, j, Case = 1; double wt[10][10];
while(scanf("%d",&n)==1&&n)
{
int cnt = 0;
for(i=0;i<n;i++)
{
scanf("%d %d",&graph[i].x, &graph[i].y);
mp[graph[i].x][graph[i].y] = i;
mp[graph[i].y][graph[i].x] = i;
for(j=0;j<i;j++)
{
edge ed;
ed.u = mp[graph[j].x][graph[j].y];
ed.v = mp[graph[i].x][graph[i].y];
ed.w = sqrt(squ(graph[i].x-graph[j].x)+squ(graph[i].y-graph[j].y));
wt[ed.u][ed.v] = wt[ed.v][ed.u] = ed.w;
/*cout<<graph[i].x<<" "<<graph[i].y<<" "<<graph[j].x<<" "<<graph[j].y<<endl;
cout<<" w :: "<<ed.w<<endl;*/
//cout<<ed.u<<" "<<ed.v<<" "<<ed.w<<endl;
v.push_back(ed);
}
}
cost = 0.0;
mst();
int source;
for(i=0;i<n;i++) if(track[i]==1&&FINAL[i].size()==1) { source = i; break; }
printf("**********************************************************\n");
printf("Network #%d\n",Case++);
int visited[10] = {0};
int x1, x2, y1, y2, des;
for(i=0;i<n-1;i++)
{
visited[source] = 1;
if(visited[FINAL[source][0]]==0) {
des = FINAL[source][0];
visited[des] = 1;
}
else {
des = FINAL[source][1];
visited[des] = 1;
}
x1 = graph[source].x; y1 = graph[source].y; x2 = graph[des].x; y2 = graph[des].y;
//cout<<"source "<<source<<" destination "<<des<<endl;
printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2lf feet.\n", x1, y1, x2, y2, wt[source][des]+16);
if(i+1<n-1) source = des;
}
printf("Number of feet of cable required is %.2lf.\n",cost+(n-1)*16);
for(i=0;i<n;i++) FINAL[i].clear();
v.clear(); ans.clear();
}
return 0;
}