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

这算是prime不?

Posted by wlgclining at 2017-08-30 12:06:25 on Problem 1287
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 99999
int s[1005][1005],dis[1005],book[1005];
void init(int n){
	int i,j;
	for(i=0;i<=n;i++){
		for(j=0;j<=n;j++)
			s[i][j]=maxn;
		dis[i]=maxn,book[i]=0;
	}
}
int main(){
	int n,m,i,j,k;
		while(cin>>n&&n){
			cin>>m;
			init(n);
			while(m--){
				int a,b,c;
				cin>>a>>b>>c;
					if(s[a][b]>c)
						s[a][b]=c,s[b][a]=c;
			}
			int maxx=maxn,t1=0,t2=0;
			book[1]=1;
			k=n;
			while(k--){
			for(i=1;i<=n;i++){
				if(book[i]){
				for(j=2;j<=n;j++)
				{
					if(book[j]==0)
					{
						if(dis[j]>s[i][j])
						dis[j]=s[i][j];
						if(dis[j]<maxx)
						maxx=dis[j],t2=j;
					}
				}	
			}
			}
			book[t2]=1;
			maxx=maxn;
		}
		k=0;
		for(i=2;i<=n;i++)
		k+=dis[i];
		cout<<k<<endl;	
		}
	return 0;
}

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