Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int minCost (int n ,vector < vector <int> > &edg, vector < vector <int> > &newe)
- {
- vector < pair <int ,int> > adj [n+1];
- //vector <vector <bool> > broken (n+1, vector <bool> (n+1,false));
- for (int i = 0 ; i < newe.size(); i++)
- {
- int x , y,w;
- x= newe[i][0];
- y = newe[i][1];
- w = newe[i][2];
- // broken[x][y]= true;
- // broken[y][x]=true;
- adj[x].push_back({y,w});
- adj[y].push_back({x,w});
- }
- for (int i = 0 ; i < edg.size(); i++)
- {
- int x , y;
- x= edg[i][0];
- y = edg[i][1];
- // if (broken[x][y])continue;
- adj[x].push_back({y,0});
- adj[y].push_back({x,0});
- }
- priority_queue < pair <int, int> , vector < pair <int , int> > , greater < pair<int , int> > > q;
- vector <bool> vis (n,false);
- q.push({0,1});
- int ans = 0;
- while (!q.empty())
- {
- pair <int, int> cur = q.top();
- q.pop();
- int val = cur.second;
- int cost = cur.first;
- if (vis[val])
- continue;
- ans+=cost;
- vis[val]= true;
- for (auto x : adj[val])
- {
- int y = x.first;
- int w = x.second;
- if(!vis[y])
- q.push({w,y});
- }
- }
- return ans;
- }
- int main() {
- /*
- {{1, 2}, {2, 3}, {3, 4},{4,5},{1,6}{2,4}}
- {{1, 6, 410}, {2, 4, 800}, {1, 5, 8}}
- [[1, 2], [2, 3], [4, 5], [3, 5], [1, 6], [2, 4]]
- [[1, 6, 410], [2, 4, 800]]
- */
- int n = 6;
- vector < vector <int> > edges ={{1, 4}, {4, 5}, {2, 3}};
- vector < vector <int> > newEdges = {{1, 2, 5}, {1, 3, 10}, {1, 6, 2}, {5, 6, 5}};
- cout<<(minCost(n, edges, newEdges));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement