Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用priority queue又写了一个,还WA,奇了怪了,跪求测试数据或指点代码#include<iostream> #include<queue> using namespace std; class Edge { public: int u; int v; int w; friend bool operator < (Edge e1, Edge e2) { return e1.w>e2.w; } }; const int inf=100001; int parent[110]; int n; priority_queue<Edge> pq; int find(int k) { return parent[k]; } void unions(int i,int j) { for(int k=0;k<n;k++) if(parent[k]==j) parent[k]=parent[i]; } int kruskal() { long int sum=0; int k=0; for(int i=0;i<n;i++) parent[i]=i; while(k < n-1) { Edge e=pq.top(); pq.pop(); int u=e.u; int v=e.v; if(find(u)!=find(v)) { unions(u,v); sum+=e.w; k++; } } return sum; } int main(void) { while(cin>>n) { for(int i=0;i<n;i++) for(int j=0;j<n;j++) { int w; cin>>w; if(j > i) { Edge e; e.u=i; e.v=j; e.w=w; pq.push(e); } } cout<<kruskal()<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator