记录编号 |
226787 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[UVa 10828] 随机程序 |
最终得分 |
100 |
用户昵称 |
神利·代目 |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.494 s |
提交时间 |
2016-02-18 11:08:50 |
内存使用 |
0.46 MiB |
显示代码纯文本
#include<cstdio>
#include<cstring>
#define eps 1e-4
#define ABS(A) ((A)>0?(A):(-(A)))
int n,q,num,ask,u,k,v,shu,max,first[101];
double t,x,ans,chu[101],o[105][105],ANS[105];
struct bian
{
int v,next;
}e[11111];
inline void add(int u,int v)
{
e[++shu].v=v;
e[shu].next=first[u];
first[u]=shu;
}
inline void work()
{
for(int i=1;i<n;++i)
{
if(ABS(o[i][i])<eps)
{
k=0;
for(int j=i+1;j<n;++j)
if(ABS(o[j][i])>eps)
k=j;
if(!k)
continue;
for(int j=i;j<=n;++j)
x=o[k][j],o[k][j]=o[i][j],o[i][j]=x;
}
for(int j=1;j<n;++j)
if(j!=i)
{
x=-o[j][i]/o[i][i];
o[j][i]=0;
for(int l=i+1;l<=n;++l)
o[j][l]+=o[i][l]*x;
}
}
for(int i=1;i<n;++i)
{
if(ABS(o[i][i])<eps&&ABS(o[i][n])<eps)
{
ANS[i]=0;
continue;
}
if(ABS(o[i][i])<eps)
{
ANS[i]=1e12;
continue;
}
int j;
for(j=i+1;j<n;++j)
if(ABS(o[i][j])>eps)
break;
if(j!=n)
ANS[i]=1e12;
else
ANS[i]=o[i][n]/o[i][i];
}
}
int main()
{
freopen("backtoKR.in","r",stdin);
freopen("backtoKR.out","w",stdout);
scanf("%d",&n);
while(n)
{
memset(chu,0,sizeof(chu));
memset(o,0,sizeof(o));
memset(first,0,sizeof(first));
++num,++n,shu=0;
scanf("%d%d",&u,&v);
while(u)
{
add(v,u);
chu[u]+=1;
scanf("%d%d",&u,&v);
}
for(int i=1;i<n;++i)
if(chu[i]>0)
chu[i]=1/chu[i];
o[1][n]=o[1][1]=1;
for(int i=first[1];i;i=e[i].next)
o[1][e[i].v]+=-chu[e[i].v];
for(int i=2;i<n;++i)
{
o[i][i]=1;
for(int j=first[i];j;j=e[j].next)
o[i][e[j].v]+=-chu[e[j].v];
}
work();
printf("Case #%d:\n",num);
scanf("%d",&q);
for(int i=1;i<=q;++i)
{
scanf("%d",&ask);
if(ANS[ask]>1e11)
printf("infinity\n");
else
printf("%.3lf\n",ANS[ask]);
}
scanf("%d",&n);
}
}