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

惊!交了四遍都RE的代码突然交过了,有点不可思议,附代码

Posted by xuesu at 2015-05-13 22:53:07 on Problem 1202
import java.math.BigDecimal;
import java.util.*;

class mes{
	int son,nxt;
	mes(){son=nxt=0;}
}
public class Main {
	final static int maxn=650;
	static int head[];
	static BigDecimal p[][];
	static mes s[];
	static int fa[][];
	static int n,k,m;
	static int vis[];
	static int que[];
	
	static void addedge(int f,int t,int ind)
	{
	    s[ind].nxt=head[f];
	    s[ind].son=t;
	    head[f]=ind;
	}
	static Scanner scanner;
	static void init()
	{
		for(int i=0;i<maxn;i++){
			head[i]=-1;
		}
		scanner = new Scanner(System.in);
		n=scanner.nextInt();
		k=scanner.nextInt();
		for(int i=0; i<k; i++)
		{
			int son,f,m;
			son=scanner.nextInt();
			f=scanner.nextInt();
			m=scanner.nextInt();
			addedge(f,son,2*i);
        	addedge(m,son,2*i+1);
        	fa[son][0]=f;
        	fa[son][1]=m;
		}
		for(int i=1; i<=n; i++)
		{
			p[i][i]=BigDecimal.ONE;
		}
	}
	

	static void calc()
	{
		int front=0,tail=0;
		final BigDecimal mul=BigDecimal.valueOf(0.5);
		for(int i=1; i<=n; i++)
		{
			if(fa[i][0]==0&&fa[i][1]==0&&vis[i]==0)
			{
        	   que[tail++]=i;
        	   vis[i]=1;
			}
		}
		while(tail!=front)
		{
			int tp=que[front];front++;
			vis[tp]=2;
			int f=fa[tp][0],m=fa[tp][1];
			if(f!=0&&m!=0)
			{
				p[tp][f]=p[tp][m]=p[f][tp]=p[m][tp]=(p[f][m].add(BigDecimal.ONE)).multiply(mul);
				for(int i=1; i<=n; i++)
				{
					if(i!=f&&i!=tp&&i!=m)
					{
						p[i][tp]=p[i][tp].add((p[i][m].add(p[i][f])).multiply(mul));
						p[tp][i]=p[i][tp];
					}
				}
			}
			
			for(int sp=head[tp]; sp!=-1; sp=s[sp].nxt)
			{
				int son=s[sp].son;
				if(vis[son]!=0)continue;
				if(vis[fa[son][0]]==2&&vis[fa[son][1]]==2)
				{
		        	que[tail++]=son;
					vis[son]=1;
				}
			}
		}
	}
	static void show()
	{
		m=scanner.nextInt();
		for(int i=0; i<m; i++)
		{
			int f,t;
			f=scanner.nextInt();
			t=scanner.nextInt();
			String ans = p[t][f].multiply(BigDecimal.valueOf(100)).toPlainString();
			if(ans.indexOf(".")>0&&ans.indexOf(".")<ans.length()){
				while(ans.length()>1&&ans.endsWith("0")){
					ans=ans.substring(0,ans.length()-1);
				}
			}
			if(ans.endsWith(".")&&ans.length()>1)ans=ans.substring(0,ans.length()-1);
			System.out.println(ans+"%");
		}
	}
	public static void main(String[] args) {
		head=new int[maxn];
		p=new BigDecimal[maxn][maxn];		
		s=new mes[2*maxn];
		fa=new int[maxn][2];
		que=new int[maxn];
		vis = new int[maxn];
		for(int i=0;i<maxn;i++){p[i]=new BigDecimal[maxn];s[i]=new mes();fa[i]=new int[2];}
		for(int i=0;i<maxn;i++){for(int j=0;j<maxn;j++){p[i][j]=BigDecimal.ZERO;}}
		init();
		calc();
		show();
	}
}

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