Skip to content

Commit e7bc4de

Browse files
committed
Added more stuff
1 parent aa2eba0 commit e7bc4de

File tree

5 files changed

+358
-5
lines changed

5 files changed

+358
-5
lines changed

Advanced Graphs/Airport2.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
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.
11+
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.
12+
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.
13+
Input
14+
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.
15+
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 .
16+
Output
17+
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.
18+
Sample Input
19+
2
20+
4 4 100
21+
1 2 10
22+
4 3 12
23+
4 1 41
24+
2 3 23
25+
5 3 1000
26+
1 2 20
27+
4 5 40
28+
3 2 30
29+
Sample Output
30+
Case #1: 145 1
31+
Case #2: 2090 2
32+
33+
*/
34+
35+
#include <bits/stdc++.h>
36+
using namespace std;
37+
38+
class Edges{
39+
40+
public:
41+
42+
int src;
43+
int dest;
44+
int weight;
45+
46+
};
47+
48+
class compare{
49+
public:
50+
bool operator()(Edges const &a,Edges const &b){
51+
return a.weight<b.weight;
52+
}
53+
};
54+
55+
56+
57+
int findParent(int *parent ,int vertex){
58+
59+
if(parent[vertex]==vertex)
60+
return vertex;
61+
62+
return findParent(parent,parent[vertex]);
63+
64+
}
65+
66+
int getOutput(Edges *input,int size,int *parent,int V,int A,int *count){
67+
68+
int totalweight=0;
69+
70+
for(int i=0;i<size;i++){
71+
Edges e = input[i];
72+
73+
int p1 = findParent(parent,e.src);
74+
int p2 = findParent(parent,e.dest);
75+
76+
if(p1!=p2 && e.weight<A){
77+
totalweight += e.weight;
78+
parent[p1] = p2;
79+
}
80+
81+
82+
}
83+
84+
85+
86+
for(int i=1;i<=V;i++){
87+
88+
if(parent[i]==i)
89+
(*count)++;
90+
91+
92+
}
93+
94+
totalweight+=((*count)*A);
95+
96+
return totalweight;
97+
98+
}
99+
100+
int main()
101+
{
102+
int t;
103+
cin>>t;
104+
int c=1;
105+
while(t--){
106+
int V,E,A;
107+
cin>>V>>E>>A;
108+
109+
Edges *input = new Edges[E];
110+
111+
for(int i=0;i<E;i++){
112+
Edges e;
113+
cin>>e.src>>e.dest>>e.weight;
114+
input[i] = e;
115+
}
116+
117+
sort(input,input+E,compare());
118+
119+
int count =0;
120+
121+
int *parent = new int[V+1];
122+
123+
for(int i=0;i<=V;i++){
124+
parent[i] = i;
125+
}
126+
int output = getOutput(input,E,parent,V,A,&count);
127+
128+
cout<<"Case #"<<c<<": "<<output<<" "<<count<<endl;
129+
c++;
130+
131+
}
132+
133+
return 0;
134+
}

Advanced Graphs/Airportwrong.cpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
AIRPORTS
11+
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.
12+
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.
13+
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.
14+
Input
15+
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.
16+
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 .
17+
Output
18+
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.
19+
Sample Input
20+
2
21+
4 4 100
22+
1 2 10
23+
4 3 12
24+
4 1 41
25+
2 3 23
26+
5 3 1000
27+
1 2 20
28+
4 5 40
29+
3 2 30
30+
Sample Output
31+
Case #1: 145 1
32+
Case #2: 2090 2
33+
*/
34+
35+
#include <bits/stdc++.h>
36+
using namespace std;
37+
38+
typedef long long ll;
39+
typedef unordered_map<int, int> umapii;
40+
typedef unordered_map<int, bool> umapib;
41+
typedef unordered_map<string, int> umapsi;
42+
typedef unordered_map<string, string> umapss;
43+
typedef map<string, int> mapsi;
44+
typedef map<pair<int, int>, int> mappiii;
45+
typedef map<int, int> mapii;
46+
typedef pair<int, int> pii;
47+
typedef pair<long long, long long> pll;
48+
typedef unordered_set<int> useti;
49+
50+
#define uset unordered_set
51+
#define it iterator
52+
#define mp make_pair
53+
#define pb push_back
54+
#define all(x) (x).begin(), (x).end()
55+
#define f first
56+
#define s second
57+
#define MOD 1000000007
58+
59+
bool mysort(pii a, pii b){
60+
return (a.second<b.second);
61+
}
62+
63+
void create(vector<pii>* graph, vector<int> &component, int* visited, int start, int a, int &total_cost){
64+
//cout << "Start "<<start << '\n';
65+
visited[start] = 1;
66+
cout<<total_cost<<endl;
67+
for (int i = 0; i < graph[start].size(); ++i)
68+
{
69+
int next = graph[start].at(i).first;
70+
if (visited[next] == 0)
71+
{
72+
if (a<=graph[start].at(i).second)
73+
{
74+
continue;
75+
}else{
76+
component.push_back(next);
77+
total_cost = total_cost + graph[start].at(i).second;
78+
//cout << "Next "<<next << '\n';
79+
create(graph, component, visited, next, a, total_cost);
80+
}
81+
}
82+
}
83+
84+
return;
85+
}
86+
87+
int main( int argc , char ** argv )
88+
{
89+
ios_base::sync_with_stdio(false) ;
90+
cin.tie(NULL) ;
91+
92+
int t;
93+
cin>>t;
94+
int l = 0;
95+
while(t--){
96+
l++;
97+
int n, m, a, total_cost, total_airports;
98+
total_cost = 0;
99+
total_airports = 0;
100+
cin>>n>>m>>a;
101+
102+
vector<pii>* graph = new vector<pii>[n];
103+
int* visited = new int[n];
104+
for (int i = 0; i < n; ++i)
105+
{
106+
visited[i] = 0;
107+
}
108+
109+
while(m--){
110+
int x, y, c;
111+
cin>>x>>y>>c;
112+
graph[x-1].push_back({y-1, c});
113+
graph[y-1].push_back({x-1, c});
114+
}
115+
116+
//Sorting the graph
117+
for (int i = 0; i < n; ++i)
118+
{
119+
sort(graph[i].begin(), graph[i].end(), mysort);
120+
}
121+
122+
vector<vector<int>> components;
123+
//int check = 0;
124+
for (int i = 0; i < n; ++i)
125+
{
126+
if (visited[i] == 0)
127+
{
128+
129+
vector<int> component;
130+
component.push_back(i);
131+
create(graph, component, visited, i, a, total_cost);
132+
components.push_back(component);
133+
134+
}
135+
}
136+
137+
total_airports = components.size();
138+
139+
cout <<"Case #"<<l<<": "<<total_cost+total_airports*a<<" "<<total_airports << '\n';
140+
141+
for (int i = 0; i < components.size(); ++i)
142+
{
143+
for (int j = 0; j < components[i].size(); ++j)
144+
{
145+
cout<<components[i][j]<<" ";
146+
}
147+
cout << '\n';
148+
}
149+
150+
cout << "Total road cost = "<<total_cost << '\n';
151+
}
152+
153+
154+
return 0 ;
155+
156+
157+
158+
}

Advanced Graphs/Matrix.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,65 @@
77

88
/*
99
PROBLEM STATEMENT
10+
Fill The Matrix
11+
A matrix B (consisting of integers) of dimension N × N is said to be good if there exists an array A (consisting of integers) such that B[i][j] = |A[i] - A[j]|, where |x| denotes absolute value of integer x.
12+
13+
You are given a partially filled matrix B of dimension N × N. Q of the entries of this matrix are filled by either 0 or 1. You have to identify whether it is possible to fill the remaining entries of matrix B (the entries can be filled by any integer, not necessarily by 0 or 1) such that the resulting fully filled matrix B is good.
14+
Input
15+
The first line of the input contains an integer T denoting the number of test cases.
16+
17+
The first line of each test case contains two space separated integers N, Q.
18+
19+
Each of the next Q lines contain three space separated integers i, j, val, which means that B[i][j] is filled with value val.
20+
Output
21+
For each test case, output "yes" or "no" (without quotes) in a single line corresponding to the answer of the problem.
22+
Constraints
23+
1 ≤ T ≤ 10^6
24+
2 ≤ N ≤ 10^5
25+
1 ≤ Q ≤ 10^6
26+
1 ≤ i, j ≤ N
27+
0 ≤ val ≤ 1
28+
Sum of each of N, Q over all test cases doesn't exceed 106
29+
Input
30+
4
31+
2 2
32+
1 1 0
33+
1 2 1
34+
2 3
35+
1 1 0
36+
1 2 1
37+
2 1 0
38+
3 2
39+
2 2 0
40+
2 3 1
41+
3 3
42+
1 2 1
43+
2 3 1
44+
1 3 1
45+
Output
46+
yes
47+
no
48+
yes
49+
no
50+
51+
*/
52+
53+
54+
/*
55+
56+
There can be a really easy greedy solution the problem.We can initialise the array A by -1.
57+
There is crucial observation that the array A can be constructed with only two values 0 and 1.
58+
Sort the index value pairs.Now if both the given index are unassigned,assign them according to
59+
the value given.Like 1 3 1 would mean 1 and 3 would be different .So initialise a[1] with 0 and
60+
a[3]=a[1]XOR val.Keep doing this and if at some point when both the indices are intialised with
61+
values!= -1 ,then check if they satisfy the given value,if not break and print -1.
62+
63+
64+
1)I'll initialize an array of size n with -1
65+
2)I'll then iterate over it and start with setting the first A[i] = 0 and then dfs will
66+
set the rest
67+
3)Now in dfs i'll set the A[j](as already set A[i]) as A[i]^value if A[j]==-1
68+
4)If it's not then it has to be equal to A[i]^value else return false
1069
1170
*/
1271

Advanced Graphs/Test.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
1
2-
4 3
3-
1 2
4-
2 3
5-
3 1
6-
1 2 3 5
2+
5 6 100
3+
1 2 5
4+
1 3 5
5+
1 4 5
6+
1 5 5
7+
3 4 8
8+
4 5 8

Advanced Graphs/a.out

-12.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)