# 洛谷2047 [noi2007]社交网络

## floyd统计最短路径数量

Posted by yjjr's blog on October 23, 2018 3 minutes to read

# 题目

## 输入输出样例

### 输入样例#1

4 4
1 2 1
2 3 1
3 4 1
4 1 1


### 输出样例#1

1.000
1.000
1.000
1.000


## 说明

50%的数据中：n ≤10，m ≤45

100%的数据中：n ≤100，m ≤4 500，任意一条边的权值c是正整数，满足：1 ≤c ≤1 000。

floyd同时统计最短路径数量

NOIP前刷水

# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
#define inf 0x3f3f3f
const int maxn=1e3+6;
int n,m;
ll Map[maxn][maxn],e[maxn][maxn];
double ans[maxn];
int main()
{
mem(Map,inf);mem(e,0);
rep(i,1,m){
Map[x][y]=Map[y][x]=z;
e[x][y]=e[y][x]=1;
}
rep(k,1,n)rep(i,1,n)rep(j,1,n){
if(i==j||j==k||i==k)continue;
if(Map[i][j]>Map[i][k]+Map[k][j]){
Map[i][j]=Map[i][k]+Map[k][j];
e[i][j]=e[i][k]*e[k][j];
continue;
}
if(Map[i][j]==Map[i][k]+Map[k][j])e[i][j]+=e[i][k]*e[k][j];
}
rep(k,1,n)rep(i,1,n)rep(j,1,n){
if(i==j||j==k||i==k)continue;
if(Map[i][k]+Map[k][j]==Map[i][j])ans[k]+=(1.0*e[i][k]*e[j][k])/e[i][j];
}
rep(i,1,n)printf("%.3lf\n",ans[i]);
return 0;
}