【BZOJ 1415】 [Noi2005]聪聪和可可
2016-02-02 09:42
357 查看
1 #include <cstdio>
2 #include <iostream>
3 #include <cmath>
4 #include <algorithm>
5 #include <cstring>
6 using namespace std;
7
8 int p[1001][1001];
9 double ans[1001][1001];
int n,m;
int tot,g[1001],nnext[1001*2],num[1001*2];
int team[1000000];
int d[1001];
int head,tail;
bool b[1001];
void Add(int x,int y)
{
tot++;
nnext[tot]=g[x];
g[x]=tot;
num[tot]=y;
}
void SPFA(int t)
{
while(head<tail)
{
int x=team[++head]; b[x]=false;
for(int i=g[x];i;i=nnext[i])
{
int tmp=num[i];
if(d[tmp]>d[x]+1||(d[tmp]==d[x]+1&&x<p[tmp][t]))
{
d[tmp]=d[x]+1;
p[tmp][t]=x;
if(!b[tmp])
{
team[++tail]=tmp;
b[tmp]=true;
}
}
}
}
}
double DFS(int x,int y)
{
// cout<< x<<' '<<y<<endl;
if(x==y) return 0.0;
if(ans[x][y]) return ans[x][y];
if(p[x][y]==y)return 1.0;if(p[p[x][y]][y]==y) return 1.0;
double aa=0;
int cnt=0;
for(int i=g[y];i;i=nnext[i])
aa+=DFS(p[p[x][y]][y],num[i])+1.0,cnt++;
aa+=DFS(p[p[x][y]][y],y)+1.0;
aa/=(double)(cnt+1);
ans[x][y]=aa;
return ans[x][y];
}
int main()
{
cin>>n>>m;int s,t;cin>>s>>t;
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
Add(x,y);
Add(y,x);
}
// cout<<"fasfa";
for(int i=1;i<=n;i++)
{
memset(d,63,sizeof(d));
d[i]=0;
head=tail=0;
team[++tail]=i;b[i]=true;
SPFA(i);
}
// cout<<endl;
printf("%.3lf\n",DFS(s,t));
return 0;
}
2 #include <iostream>
3 #include <cmath>
4 #include <algorithm>
5 #include <cstring>
6 using namespace std;
7
8 int p[1001][1001];
9 double ans[1001][1001];
int n,m;
int tot,g[1001],nnext[1001*2],num[1001*2];
int team[1000000];
int d[1001];
int head,tail;
bool b[1001];
void Add(int x,int y)
{
tot++;
nnext[tot]=g[x];
g[x]=tot;
num[tot]=y;
}
void SPFA(int t)
{
while(head<tail)
{
int x=team[++head]; b[x]=false;
for(int i=g[x];i;i=nnext[i])
{
int tmp=num[i];
if(d[tmp]>d[x]+1||(d[tmp]==d[x]+1&&x<p[tmp][t]))
{
d[tmp]=d[x]+1;
p[tmp][t]=x;
if(!b[tmp])
{
team[++tail]=tmp;
b[tmp]=true;
}
}
}
}
}
double DFS(int x,int y)
{
// cout<< x<<' '<<y<<endl;
if(x==y) return 0.0;
if(ans[x][y]) return ans[x][y];
if(p[x][y]==y)return 1.0;if(p[p[x][y]][y]==y) return 1.0;
double aa=0;
int cnt=0;
for(int i=g[y];i;i=nnext[i])
aa+=DFS(p[p[x][y]][y],num[i])+1.0,cnt++;
aa+=DFS(p[p[x][y]][y],y)+1.0;
aa/=(double)(cnt+1);
ans[x][y]=aa;
return ans[x][y];
}
int main()
{
cin>>n>>m;int s,t;cin>>s>>t;
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
Add(x,y);
Add(y,x);
}
// cout<<"fasfa";
for(int i=1;i<=n;i++)
{
memset(d,63,sizeof(d));
d[i]=0;
head=tail=0;
team[++tail]=i;b[i]=true;
SPFA(i);
}
// cout<<endl;
printf("%.3lf\n",DFS(s,t));
return 0;
}
相关文章推荐
- 利用pl/sql执行计划评估SQL语句的性能简析
- x86 SMI链路错误
- Cocos2d Android项目手动编译日记之SDK版本(二)
- http://www.oschina.net/code/snippet_2507499_53892
- 程序和shell脚本交互方法
- 【poj2891】同余方程组
- Android SDK开发包国内下载地址
- 体验jQuery和AngularJS的不同点及AngularJS的迷人之处
- Java2EE的controller层获取session的方法
- Android App 内存泄露之Handler
- springmvc文件上传使用uploadify插件
- C# Redis
- 比大小 log_2^3 与 log_3^5
- Android:MediaSession框架介绍[转]
- picasso使用简要说明
- realloc,malloc,calloc的区别
- Nginx详解-服务器集群
- UI_UISegmentedControl 控件
- iOS 自定义TabBarController
- 2016年1月书单推荐