题目链接:
https://www.lydsy.com/JudgeOnline/problem.php?id=1491
题目大意:
见链接
思路:
直接用floyd算法求最短路,同时更新最短路的数目即可。
1 #include<bits/stdc++.h> 2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf 3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数,会超时 4 #define Min(a, b) ((a) < (b) ? (a) : (b)) 5 #define Mem(a) memset(a, 0, sizeof(a)) 6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1)) 7 #define MID(l, r) ((l) + ((r) - (l)) / 2) 8 #define lson ((o)<<1) 9 #define rson ((o)<<1|1) 10 #define Accepted 0 11 #pragma comment(linker, "/STACK:102400000,102400000")//栈外挂 12 using namespace std; 13 inline int read() 14 { 15 int x=0,f=1;char ch=getchar(); 16 while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} 17 while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 18 return x*f; 19 } 20 21 typedef long long ll; 22 const int maxn = 100 + 10; 23 const int MOD = 1000000007;//const引用更快,宏定义也更快 24 const int INF = 1e9 + 7; 25 const double eps = 1e-6; 26 27 int Map[maxn][maxn]; 28 ll p[maxn][maxn]; 29 double ans[maxn]; 30 int main() 31 { 32 int n, m; 33 scanf("%d%d", &n, &m); 34 for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)Map[i][j] = INF; 35 while(m--) 36 { 37 int u, v, w; 38 scanf("%d%d%d", &u, &v, &w); 39 Map[u][v] = Map[v][u] = w; 40 p[u][v] = p[v][u] = 1;//路径条数 41 } 42 for(int k = 1; k <= n; k++) 43 { 44 for(int i = 1; i <= n; i++) 45 { 46 for(int j = 1; j <= n; j++) 47 { 48 if(Map[i][k] + Map[k][j] < Map[i][j]) 49 { 50 Map[i][j] = Map[i][k] + Map[k][j]; 51 p[i][j] = p[i][k] * p[k][j]; 52 } 53 else if(Map[i][k] + Map[k][j] == Map[i][j]) 54 p[i][j] += p[i][k] * p[k][j]; 55 } 56 } 57 } 58 for(int k = 1; k <= n; k++)//k为s-t的必经点 59 for(int s = 1; s <= n; s++) 60 for(int t = 1; t <= n; t++) 61 if(s != t && s != k && k != t) 62 { 63 if(Map[s][k] + Map[k][t] == Map[s][t])//最短路径可以经过k点 64 { 65 ans[k] += 1.0 * p[s][k] * p[k][t] / p[s][t]; 66 } 67 } 68 for(int i = 1; i <= n; i++)printf("%.3f ", ans[i]); 69 return Accepted; 70 }