-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathAirportwrong.cpp
158 lines (132 loc) · 5.06 KB
/
Airportwrong.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
/*
Name: Mehul Chaturvedi
IIT-Guwahati
*/
/*
PROBLEM STATEMENT
AIRPORTS
The government of a certain developing nation wants to improve transportation in one of its most inaccessible areas, in an attempt to attract investment. The region consists of several important locations that must have access to an airport.
Of course, one option is to build an airport in each of these places, but it may turn out to be cheaper to build fewer airports and have roads link them to all of the other locations. Since these are long distance roads connecting major locations in the country (e.g. cities, large villages, industrial areas), all roads are two-way. Also, there may be more than one direct road possible between two areas. This is because there may be several ways to link two areas (e.g. one road tunnels through a mountain while the other goes around it etc.) with possibly differing costs.
A location is considered to have access to an airport either if it contains an airport or if it is possible to travel by road to another location from there that has an airport. You are given the cost of building an airport and a list of possible roads between pairs of locations and their corresponding costs. The government now needs your help to decide on the cheapest way of ensuring that every location has access to an airport. The aim is to make airport access as easy as possible, so if there are several ways of getting the minimal cost, choose the one that has the most airports.
Input
The first line of input contains the integer T (T < 25), the number of test cases. The rest of the input consists of T cases. Each case starts with two integers N, M and A (0 < N ≤ 10, 000, 0 ≤ M ≤ 100, 000, 0 < A ≤ 10, 000) separated by white space. N is the number of locations, M is the number of possible roads that can be built, and A is the cost of building an airport.
The following M lines each contain three integers X, Y and C (1 ≤ X, Y ≤ N, 0 < C ≤ 10, 000), separated by white space. X and Y are two locations, and C is the cost of building a road between X and Y .
Output
Your program should output exactly T lines, one for each case. Each line should be of the form ‘Case #X: Y Z’, where X is the case number Y is the minimum cost of making roads and airports so that all locations have access to at least one airport, and Z is the number of airports to be built. As mentioned earlier, if there are several answers with minimal cost, choose the one that maximizes the number of airports.
Sample Input
2
4 4 100
1 2 10
4 3 12
4 1 41
2 3 23
5 3 1000
1 2 20
4 5 40
3 2 30
Sample Output
Case #1: 145 1
Case #2: 2090 2
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unordered_map<int, int> umapii;
typedef unordered_map<int, bool> umapib;
typedef unordered_map<string, int> umapsi;
typedef unordered_map<string, string> umapss;
typedef map<string, int> mapsi;
typedef map<pair<int, int>, int> mappiii;
typedef map<int, int> mapii;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef unordered_set<int> useti;
#define uset unordered_set
#define it iterator
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define MOD 1000000007
bool mysort(pii a, pii b){
return (a.second<b.second);
}
void create(vector<pii>* graph, vector<int> &component, int* visited, int start, int a, int &total_cost){
//cout << "Start "<<start << '\n';
visited[start] = 1;
cout<<total_cost<<endl;
for (int i = 0; i < graph[start].size(); ++i)
{
int next = graph[start].at(i).first;
if (visited[next] == 0)
{
if (a<=graph[start].at(i).second)
{
continue;
}else{
component.push_back(next);
total_cost = total_cost + graph[start].at(i).second;
//cout << "Next "<<next << '\n';
create(graph, component, visited, next, a, total_cost);
}
}
}
return;
}
int main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
int t;
cin>>t;
int l = 0;
while(t--){
l++;
int n, m, a, total_cost, total_airports;
total_cost = 0;
total_airports = 0;
cin>>n>>m>>a;
vector<pii>* graph = new vector<pii>[n];
int* visited = new int[n];
for (int i = 0; i < n; ++i)
{
visited[i] = 0;
}
while(m--){
int x, y, c;
cin>>x>>y>>c;
graph[x-1].push_back({y-1, c});
graph[y-1].push_back({x-1, c});
}
//Sorting the graph
for (int i = 0; i < n; ++i)
{
sort(graph[i].begin(), graph[i].end(), mysort);
}
vector<vector<int>> components;
//int check = 0;
for (int i = 0; i < n; ++i)
{
if (visited[i] == 0)
{
vector<int> component;
component.push_back(i);
create(graph, component, visited, i, a, total_cost);
components.push_back(component);
}
}
total_airports = components.size();
cout <<"Case #"<<l<<": "<<total_cost+total_airports*a<<" "<<total_airports << '\n';
for (int i = 0; i < components.size(); ++i)
{
for (int j = 0; j < components[i].size(); ++j)
{
cout<<components[i][j]<<" ";
}
cout << '\n';
}
cout << "Total road cost = "<<total_cost << '\n';
}
return 0 ;
}