Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

无解代码 一直WA求解释

Posted by qiuyifeng111 at 2018-08-20 20:38:18 on Problem 1797
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cmath> 
#include<iomanip>
using namespace std;
struct Edge{int u,v,w;};
vector<Edge>edge;
int n;
int fa[100005],eu,ev,cnt;
int pos;
bool cmp(Edge a,Edge b){ 
    if(a.w!=b.w) return a.w>b.w;
}
int find(int x){
        if(fa[x]==x) return fa[x];
        else return fa[x]= find(fa[x]);
}
void Union(int x,int y){
        int fx=find(x),fy=find(y);
        if(fx!=fy)
        fa[fx]=fy;
      }
inline void kruskal(){
    sort(edge.begin(),edge.end(),cmp);
    for(int i=0;i<edge.size();i++){
    //	cout<<edge[i].u<<" "<<edge[i].v<<" "<<edge[i].w<<"\n";
        eu=find(edge[i].u), ev=find(edge[i].v);
		if(eu!=ev) {
        pos=edge[i].w;
        Union(edge[i].u,edge[i].v);
		cnt++;
	    }
		if(find(1)==find(n)){
			break;
		}
        if(cnt==n-1) break;
     } 
}
int main(){
	int t;
	cin>>t;
	for(int i=1;i<=t;i++){
		int m;
        cin>>n>>m;
		 cnt=0;
		 pos=0;
		 edge.clear();
		 for(int j=0;j<=n;j++)
		 fa[i]=i;
		 for(int j=1;j<=m;j++){
		 	int a,b,c;
		 	scanf("%d%d%d",&a,&b,&c);
		 	Edge s;
		 	s.u=a;
		 	s.v=b;
		 	s.w=c;
		 	edge.push_back(s);
		 }
		  cout<<"Scenario #"<<i<<":\n";
		  kruskal();
          cout<<pos<<"\n\n";
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator