-
Notifications
You must be signed in to change notification settings - Fork 0
/
B. Bakery
75 lines (70 loc) · 1.6 KB
/
B. Bakery
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
//DAY 2 PROBLEM 1
//DIFFICULTY: 1300
//Implementation problem using weighted graph
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
unordered_set<int>s;
void addEdge(vector <pair<long long, long long> > adj[], int u, int v, long long wt)
{
adj[u].push_back(make_pair(v, wt));
adj[v].push_back(make_pair(u, wt));
}
// Print adjacency list representaion ot graph
void printGraph(vector<pair<long long,long long> > adj[], int V)
{
int v, w;
long long min_weight=INT_MAX;
for (int u = 1; u <= V; u++)
{
if(s.find(u)!=s.end())
{
// cout << "Node " << u << " makes an edge with \n";
for (auto it = adj[u].begin(); it!=adj[u].end(); it++)
{
v = it->first;
w = it->second;
if(s.find(v)==s.end())
min_weight=(min_weight<w)?min_weight:w;
// cout << "\tNode " << v << " with edge weight ="
// << w << "\n";
}
//cout << "\n";
}
}
if(min_weight!=INT_MAX)
cout<<min_weight<<endl;
else
cout<<-1<<endl;
}
void tests() {
int n,m,k;
cin>>n>>m>>k;
vector<pair<long long, long long> > adj[n+3];
while(m--)
{
int u,v;
long long l;
cin>>u>>v>>l;
addEdge(adj, u, v, l);
}
s.clear();
for(int i=0;i<k;i++)
{
int x;
cin>>x;
s.insert(x);
}
printGraph(adj,n);
}
int32_t main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
int t = 1;
//cin>>t;
while(t--){
tests();
}
return 0 ;
}