forked from fatemehkarimi/uvaSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva-10462.cpp
More file actions
135 lines (115 loc) · 3 KB
/
uva-10462.cpp
File metadata and controls
135 lines (115 loc) · 3 KB
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
//uva 10462
//Is There A Second Way Left ?
#include <iostream>
#include <climits>
#include <vector>
#include <set>
using namespace std;
void _union(vector <int> & ufds, int x, int y);
int findParent(vector <int> & ufds, int x);
int Kruskal(int n, multiset <pair <int, pair <int, int> > > & Edges, multiset < pair <int, pair <int, int> > > & unUsed);
int dfs(vector < vector <pair <int, int> > > & Tree, vector <bool> & visited, int root, int dest);
int main(void)
{
int T = 0, tcounter = 0;
cin >> T;
while(T--){
++tcounter;
int n, m;
cin >> n >> m;
multiset <pair <int, pair <int, int> > > Edges;
for(int i = 0; i < m; ++i){
int u, v, cost;
cin >> u >> v >> cost;
--u, --v;
if(u == v)
continue;
Edges.insert(make_pair(cost, make_pair(u, v)));
}
multiset < pair <int, pair <int, int> > > unUsed;
int result = Kruskal(n, Edges, unUsed);
cout << "Case #" << tcounter << " : ";
if(result == -1){
cout << "No way" << endl;
continue;
}
else if(unUsed.empty()){
cout << "No second way" << endl;
continue;
}
int secondMin = INT_MAX;
vector < vector < pair <int, int> > > Tree(n);
for(auto a : Edges){
Tree[a.second.first].push_back(make_pair(a.second.second, a.first));
Tree[a.second.second].push_back(make_pair(a.second.first, a.first));
}
for(auto a : unUsed){
int cost = a.first;
int u = a.second.first;
int v = a.second.second;
vector <bool> visited(n, false);
int minEdge = dfs(Tree, visited, u, v);
secondMin = min(secondMin, result - minEdge + cost);
}
cout << secondMin << endl;
}
return 0;
}
int Kruskal(int n, multiset <pair <int, pair <int, int> > > & Edges, multiset < pair <int, pair <int, int> > > & unUsed)
{
int result = 0;
vector <int> ufds(n, -1);
multiset <pair <int, pair <int, int> > > Heap = Edges;
while(!Heap.empty()){
int cost = Heap.begin()->first;
int u = Heap.begin()->second.first;
int v = Heap.begin()->second.second;
Heap.erase(Heap.begin());
if(findParent(ufds, u) != findParent(ufds, v)){
_union(ufds, u, v);
result += cost;
}
else{
unUsed.insert(make_pair(cost, make_pair(u, v)));
auto itr = Edges.find(make_pair(cost, make_pair(u, v)));
Edges.erase(itr);
}
}
for(int i = 1; i < n; ++i)
if(findParent(ufds, 0) != findParent(ufds, i))
result = -1;
return result;
}
int findParent(vector <int> & ufds, int x)
{
int root = x;
while(ufds[root] != -1)
root = ufds[root];
while(ufds[x] != -1){
int next = ufds[x];
ufds[x] = root;
x = next;
}
return root;
}
void _union(vector <int> & ufds, int x, int y)
{
int p1 = findParent(ufds, x);
int p2 = findParent(ufds, y);
ufds[p2] = p1;
return;
}
int dfs(vector < vector <pair <int, int> > > & Tree, vector <bool> & visited, int root, int dest)
{
int minEdge = INT_MIN;
visited[root] = true;
for(auto a : Tree[root])
if(!visited[a.first]){
if(a.first == dest)
return a.second;
int result = dfs(Tree, visited, a.first, dest);
if(result != INT_MIN)
minEdge = max(a.second, result);
}
return minEdge;
}