Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<queue>
- #include<fstream>
- #include<set>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- #define Max 100
- #define pb push_back
- #define READ(f) freopen(f, "r", stdin)
- #define WRITE(f) freopen(f, "w", stdout)
- #define G struct prims
- G
- {
- int u,v,cost;
- bool operator < (const G& a) const{
- return cost>a.cost;
- }
- }p[Max];
- priority_queue<G>q,q1,x;
- int visited[Max+1];
- int main()
- {
- READ("in.txt");
- memset(visited,0,sizeof(visited));
- int num_edge,sum=0,src;
- cin>>num_edge;
- for(int i=1;i<=num_edge;i++)
- {
- cin>>p[i].u>>p[i].v>>p[i].cost;
- q.push(p[i]);
- }
- cin>>src;
- visited[src]=1;
- while(!q.empty())
- {
- G pop;
- pop=q.top();
- if((visited[pop.u]) || (visited[pop.v]))
- {
- if((visited[pop.u]) && (visited[pop.v]))
- {
- q.pop();
- continue;
- }
- visited[pop.u]=visited[pop.v]=1;
- sum+=pop.cost;
- q.pop();
- }
- else
- {
- //q.push(pop);
- //x.push(pop);
- }
- }
- //cout<<sum;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement