BZOJ1491: [NOI2007]社交网络(Floyd 最短路计数)
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 2343 Solved: 1266
[Submit][Status][Discuss]
Description
.png)
Input
Output
输出包括n行,每行一个实数,精确到小数点后3位。第i行的实数表示结点i在社交网络中的重要程度。
Sample Input
1 2 1
2 3 1
3 4 1
4 1 1
Sample Output
1.000
1.000
1.000
HINT
社交网络如下图所示。
Source
最短路计数。。
mdzz想到一个做法,应该是$N^3$的,不过与边权有关,然后被卡成$90$分卡成一下午。。
就是直接dfs求最短路计数的时候统计答案,但是不能写记忆化。会wa
// luogu-judger-enable-o2
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
using namespace std;
const int MAXN = 1e5 + , INF = 1e8 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, M;
int dis[][], f[][], num[][][], w[][];
void Floyed() {
for(int k = ; k <= N; k++) {
dis[k][k] = ;
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
int GetAns(int bg, int now, int pre) {
int ans = ;
if(bg == now) return ;
for(int i = ; i <= N; i++) {
if(now != i && dis[bg][now] == dis[bg][i] + w[i][now]) {
int x = GetAns(bg, i, pre);
num[bg][pre][i] += x;
ans += x;
}
}
return ans;
}
double ans[MAXN];
main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
//freopen("a.out", "w", stdout);
#endif
N = read(); M = read();
memset(dis, 0x3f, sizeof(dis));
memset(w, 0x3f, sizeof(w));
for(int i = ; i <= M; i++) {
int x = read(), y = read(), z = read();
w[x][y] = w[y][x] = dis[x][y] = dis[y][x] = z;
}
Floyed(); for(int i = ; i <= N; i++) {
f[i][i] = ;
for(int j = ; j <= N; j++)
f[i][j] = GetAns(i, j, j);
} for(int i = ; i <= N; i++) {
double ans = ;
for(int s = ; s <= N; s++) {
for(int t = ; t <= N; t++)
if(s != i && t != i && s != t)
ans += (double)num[s][t][i] / f[s][t];
}
printf("%.3lf\n", ans);
}
return ;
}
90
标算是Floyd最短路计数,
// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
#define LL long long
using namespace std;
const int INF = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, M;
LL dis[][], num[][];
double ans[];
void Floyed() {
for(int k = ; k <= N; k++) {
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++) {
int to = dis[i][k] + dis[k][j];
if(to == INF) continue;
if(to < dis[i][j])
dis[i][j] = to, num[i][j] = num[i][k] * num[k][j];
else if(to == dis[i][j])
num[i][j] += num[i][k] * num[k][j];
}
}
for(int i = ; i <= N; i++) {
for(int s = ; s <= N; s++) {
for(int t = ; t <= N; t++) {
if(s == i || t == i || (dis[s][i] + dis[i][t] != dis[s][t]) || s == t) continue;
ans[i] += (double)(1.0 * num[s][i] * num[i][t]) / num[s][t];
}
}
}
}
int main() {
N = read(); M = read();
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++)
dis[i][j] = INF;
for(int i = ; i <= M; i++) {
int x = read(), y = read(), z = read();
dis[x][y] = dis[y][x] = z;
num[x][y] = num[y][x] = ;
}
Floyed();
for(int i = ; i <= N; i++)
printf("%.3lf\n", ans[i]);
return ;
}
BZOJ1491: [NOI2007]社交网络(Floyd 最短路计数)的更多相关文章
- 1491. [NOI2007]社交网络【最短路计数】
Description 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子里有n个人,人与人之间有不同程度的关系.我们将这 ...
- [BZOJ1491][NOI2007]社交网络 floyd
1491: [NOI2007]社交网络 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2196 Solved: 1170[Submit][Status ...
- 【BZOJ1491】[NOI2007]社交网络 Floyd
[BZOJ1491][NOI2007]社交网络 Description 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子 ...
- BZOJ 1491: [NOI2007]社交网络( floyd )
floyd...求最短路时顺便求出路径数. 时间复杂度O(N^3) ------------------------------------------------------------------ ...
- [NOI2007]社交网络(最短路)
[NOI2007]社交网络 Description 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子里有n个人,人与人之 ...
- 洛谷P2047||bzoj1491 [NOI2007]社交网络
https://www.luogu.org/problemnew/show/P2047 https://www.lydsy.com/JudgeOnline/problem.php?id=1491 也可 ...
- BZOJ1491 [NOI2007]社交网络[最短路计数]
$n$非常的小,结合题目计算式可以想到$O(n^3)$暴枚$s,t,v$,看$v$在不在$s\to t$最短路上($dis_{s,v}+dis_{v,t}=dis_{s,v}$是$v$在两点最短路上的 ...
- BZOJ1491 [NOI2007]社交网络 【floyd】
题目 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子里有n个人,人与人之间有不同程度的关系.我们将这个关系网络对应到一 ...
- 【Floyd】BZOJ1491: [NOI2007]社交网络
Description Solution n<=100自然联想Floyd 设两个数组d[n][n]存最短距离,t[n][n]存最短路径条数 更新d的时候顺便更新t,乘法原理 if(d[i][ ...
随机推荐
- Host 'XXX' is not allowed to connect to this MySQL server解决方案
如何允许远程连接mysql数据库呢,操作如下: 首先登录账号 mysql -uroot -p 使用mysql用户 use mysql 如果报此类错:ERROR 1820 (HY000): You mu ...
- Express中404页面
404页面是各大网站都需要的. 在做express项目时,应当注意,404页面在app.js中的判断是在最后的,使用这个中间件时,不需要next(),因为它是最后一个了. 它之前一般是router. ...
- 【补充】docker基础学习
docker 基础知识 之前写了一篇docker未授权访问的文章,现在来补充一下docker基础知识,以便更好的学习docker上的漏洞. docker是一款轻量级的虚拟化的产品,它属于层级化的架构. ...
- C# 压缩 解压 复制文件夹,文件的操作
命名空间:namespace System.IO.Compression 压缩: //目标文件夹 string fileDirPath = "/Downloads/试题" + us ...
- vue的ajax
vue的ajax常见的有两种 ,一种是 vue-resource,一种是axios vue-resource: 是vue的插件,非官方库, vue1.x 使用广泛 如何使用: 先在vue的脚本架上安装 ...
- javaSE练习3——数组
一.编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值. package com.test; public class t01 { public stat ...
- JSON语法格式
一.JSON数据格式 名称/值对 二.JSON值对数据类型 数字 字符串 逻辑值 数组(在方括号中) 对象 (在花括号中) null eg: { "staff ...
- react-router + redux + react-redux 的例子与分析
一个 react-router + redux + react-redux 的例子与分析 index.js import React from 'react' import ReactDom fr ...
- 详解HTML中的表单元素
代码详讲: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- echarts渲染一个风向图
今天给大家说一个用echarts渲染一个风向图,这里图上其实有三个要素,风向,风级和能见度,主要还是讲讲代码里面的坑 1.风向图标方向修改以及设置 var ownData = echarts.util ...