bzoj2306 [Ctsc2011]幸福路径 倍增 Floyd
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=2306
题解
倍增 Floyd。
令 \(f[i][j][k]\) 表示走了 \(2^i\) 步,从 \(j\) 到 \(k\) 的距离最大值。
然后转移就是 \(f[i][j][k] = \max\limits_{l=1}^n f[i-1][j][l] + p \cdot f[i-1][l][k]\)。
另外要每一个点建立一个长度为 \(0\) 的自环,用来统计总的最大值。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
const int N = 100 + 7;
const double INF = 1e18;
int n, m, st;
double p;
double a[N], f[N][N][N];
inline void work() {
for (int i = 1; i <= 30; ++i, p = p * p)
for (int j = 1; j <= n; ++j)
for (int k = 1; k <= n; ++k)
for (int l = 1; l <= n; ++l) smax(f[i][j][k], f[i - 1][j][l] + p * f[i - 1][l][k]);
// for (int i = 0; i <= 30; ++i)
// for (int j = 1; j <= n; ++j)
// for (int k = 1; k <= n; ++k) dbg("f[%d][%d][%d] = %.10lf\n", i, j, k, f[i][j][k]);
double ans = 0;
for (int i = 1; i <= n; ++i) smax(ans, f[30][st][i]);
ans += a[st];
printf("%.1lf\n", ans);
}
inline void init() {
read(n), read(m);
for (int i = 1; i <= n; ++i) scanf("%lf", &a[i]);
for (int i = 0; i <= 30; ++i)
for (int j = 1; j <= n; ++j) {
for (int k = 1; k <= n; ++k) f[i][j][k] = -INF;
f[i][j][j] = 0;
}
scanf("%d%lf", &st, &p);
int x, y;
for (int i = 1; i <= m; ++i) read(x), read(y), f[0][x][y] = p * a[y];
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
bzoj2306 [Ctsc2011]幸福路径 倍增 Floyd的更多相关文章
- BZOJ2306:[CTSC2011]幸福路径(倍增Floyd)
Description 有向图 G有n个顶点 1, 2, …, n,点i 的权值为 w(i).现在有一只蚂蚁,从给定的起点 v0出发,沿着图 G 的边爬行.开始时,它的体力为 1.每爬过一条边,它 ...
- 【bzoj2306】[Ctsc2011]幸福路径 倍增Floyd
题目描述 一张n个点的有向图,每个点有一个权值.一开始从点$v_0$出发沿图中的边任意移动,移动到路径上的第$i$个点 输入 每一行中两个数之间用一个空格隔开. 输入文件第一行包含两个正整数 n, ...
- BZOJ2306 [Ctsc2011]幸福路径[倍增]
这个有环的情况非常的讨厌,一开始想通过数学推等比数列的和,但是发现比较繁就不做了. 然后挖掘这题性质. 数据比较小,但是体力可以很接近1(恼怒),也就是说可能可以跳很多很多步.算了一下,大概跳了2e7 ...
- BZOJ2306: [Ctsc2011]幸福路径
Description 有向图 G有n个顶点 1, 2, -, n,点i 的权值为 w(i).现在有一只蚂蚁,从 给定的起点 v0出发,沿着图 G 的边爬行.开始时,它的体力为 1.每爬过一条 边,它 ...
- 【BZOJ 2306】 2306: [Ctsc2011]幸福路径 (倍增floyd)
2306: [Ctsc2011]幸福路径 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 912 Solved: 437 Description 有向 ...
- 【BZOJ2306】幸福路径(动态规划,倍增)
[BZOJ2306]幸福路径(动态规划,倍增) 题面 BZOJ 题解 不要求确切的值,只需要逼近 显然可以通过移动\(\infty\)步来达到逼近的效果 考虑每次的一步怎么移动 设\(f[i][j]\ ...
- [CTSC2011]幸福路径
题目描述 有向图 G有n个顶点 1, 2, …, n,点i 的权值为 w(i).现在有一只蚂蚁,从 给定的起点 v0出发,沿着图 G 的边爬行.开始时,它的体力为 1.每爬过一条 边,它的体力都会下降 ...
- BZOJ 2306: [Ctsc2011]幸福路径
Description 有向图 G有n个顶点 1, 2, -, n,点i 的权值为 w(i).现在有一只蚂蚁,从 给定的起点 v0出发,沿着图 G 的边爬行.开始时,它的体力为 1.每爬过一条 边,它 ...
- bzoj2165: 大楼(倍增floyd)
题目大意:一个有向图,n(<=100)个点求一条长度>=m(<=10^18)的路径最少经过几条边. 一开始以为是矩乘,蓝鹅当时还没开始写,所以好像给CYC安利错了嘿嘿嘿QWQ 第一眼 ...
随机推荐
- MyBatis系列:二、配置文件详解
本文会详细介绍MyBatis的常用配置 1.properties节点 <properties resource="mybatis-config.properties"> ...
- 004-unity3d MonoBehaviour脚本方法简介
一.MonoBehaviour 1.公共方法 CancelInvoke Cancels all Invoke calls on this MonoBehaviour. Invoke Invokes t ...
- requests模块(请求接口)
下面分别是get,post,入参json,添加cookie,添加header,上传/下载文件 的接口请求举例: import requests #导入模块 #1.发get请求 url = 'htt ...
- golang http Specifically check for timeout error
Specifically check for timeout error 特异性识别 golang http client 的超时错误 package main import ( "fmt& ...
- uni-app-在开启小程序在微信开发工具打开时,打开失败,解决方法:手动打开
这是我自学ui-app的第一张记录内容,问题都是我在实际开发中遇到的问题 1.微信开发工具打开失败,提示是: 接着我的流程就是打开上面提到的链接,进入页面:https://developers.wei ...
- django在线教育网站开发---需求分析
开发目录: django app设计 user model.py 编写 courses models.py 编写 -Course 课程基本信息 -Lesson 章节信息 -Video -视频 -Cou ...
- Centos7 -samba服务配置
Centos7 -samba服务配置 https://blog.csdn.net/zh515858237/article/details/76914905 http://blog.51cto.com/ ...
- 应用安全-web安全-WebShell整理
shellcode.aspx <%@ Page Language="C#" AutoEventWireup="true" Inherits="S ...
- C# DropDownList绑定添加新数据的三种方法
一.在前台手动绑定 <asp:DropDownList ID="DropDownList1" runat="server"> <asp: ...
- HackGame2 writeup
网址:http://hackgame.blackbap.org/ 第一关 突破客户端:无论输入什么密码都会提示"密码不能为空",使用浏览器检查网页元素会发现提交时会触发 javas ...