CF#335 Intergalaxy Trips

时间:2023-01-17 20:26:18
 Intergalaxy Trips
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The scientists have recently discovered wormholes — objects in space that allow to travel very long distances between galaxies and star systems.

The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.

Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.

Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of galaxies within reach.

Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.

Output

Print a single real value — the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if CF#335 Intergalaxy Trips.

Sample test(s)
input
3
100 50 50
0 100 80
0 0 100
output
1.750000000000000
input
2
100 30
40 100
output
3.333333333333333
Note

In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is CF#335 Intergalaxy Trips.

题意:有n个点,每天可以从i到j的概率是P(i, j),每天也可以选择留在原地,问去到n的期望天数。

分析:这题是这样的。

如果我们从终点往前推,会简单很多。因为从前往后的话很难确定有哪些点转移到自身。

从终点开始的话,显然一开是在终点的天数是0。

又发现,每个点显然只能从更优(期望更小)的点转移到自己。

由于是从后往前推,所以这意味这已经推过的点不会再推。

所以,这是一个类似最短路的过程。

推的过程是这样的

设比x点优秀的点是v[1],v[2].....v[c]

那么x的期望显然满足

dp[x] = dp[v[1]] * p(x, v[1]) + dp[v[2]] * p(x, v[2]) * (1 - p(x, v[1]) ) + dp[v[3]] * p(x, v[3]) * (1 - p(x, v[2]) ) * (1 - p(x, v[1]) ) + ..... + dp[v[c]] * p(x, v[c]) * (1 - p(x, v[2]) ) * (1 - p(x, v[1]) ) * ..... * (1 - p(x, v[c - 1])) + dp[x] * (1 - p(x, v[2]) ) * (1 - p(x, v[1]) ) * ..... * (1 - p(x, v[c])) + 1

其中(1 - p(x, v[2]) ) * (1 - p(x, v[1]) ) * ..... * (1 - p(x, v[c]))是它留在原地的概率。

然后变形

dp[x] = (    dp[v[1]] * p(x, v[1]) + dp[v[2]] * p(x, v[2]) * (1 - p(x, v[1]) ) + dp[v[3]] * p(x, v[3]) * (1 - p(x, v[2]) ) * (1 - p(x, v[1]) ) + ..... + dp[v[c]] * p(x, v[c]) * (1 - p(x, v[2]) ) * (1 - p(x, v[1]) ) * ..... * (1 - p(x, v[c - 1]))      + 1    ) / (1 -  (1 - p(x, v[2]) ) * (1 - p(x, v[1]) ) * ..... * (1 - p(x, v[c])) )

这样就可以转移了。。。

 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const DB EPS = 1e-;
const int N = ;
int n, data[N][N];
DB dp[N], stay[N], cnt[N];
bool visit[N]; inline void Input()
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
scanf("%d", &data[i][j]);
} inline void Solve()
{
for(int i = ; i <= n; i++) dp[i] = INF, stay[i] = 1.0, cnt[i] = 0.0;
dp[n] = ;
for(int k = ; k <= n; k++)
{
int idx = -;
DB mn = INF;
for(int i = ; i <= n; i++)
if(!visit[i] && mn >= dp[i])
mn = dp[i], idx = i; if(idx == )
{
printf("%.12lf\n", dp[]);
break;
} visit[idx] = ;
for(int i = ; i <= n; i++)
if(!visit[i])
{
cnt[i] += stay[i] * dp[idx] * (0.01 * data[i][idx]);
stay[i] *= - 0.01 * data[i][idx];
if(fabs( - stay[i]) > EPS)
dp[i] = ( + cnt[i]) / ( - stay[i]);
}
}
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}

CF#335 Intergalaxy Trips的更多相关文章

  1. 【CF605E】Intergalaxy Trips(贪心,动态规划)

    [CF605E]Intergalaxy Trips(贪心,动态规划) 题面 Codeforces 洛谷 有\(n\)个点,每个时刻第\(i\)个点和第\(j\)个点之间有\(p_{ij}\)的概率存在 ...

  2. CodeForces 605 E&period; Intergalaxy Trips

    E. Intergalaxy Trips time limit per test:2 seconds memory limit per test:256 megabytes input:standar ...

  3. CF605E Intergalaxy Trips

    CF605E Intergalaxy Trips 考虑你是不知道后来的边的出现情况的,所以可以这样做:每天你都选择一些点进行观察,知道某天往这些点里面的某条边可用了,你就往这条边走.这样贪心总是对的. ...

  4. CF&num;335 Freelancer&&num;39&semi;s Dreams

    Freelancer's Dreams time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. CF&num;335 Board Game

    Board Game time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...

  6. CF&num;335 Lazy Student

    Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. CF&num;335 Sorting Railway Cars

    Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. CF &num;335 div1 A&period; Sorting Railway Cars

    题目链接:http://codeforces.com/contest/605/problem/A 大意是对一个排列进行排序,每一次操作可以将一个数字从原来位置抽出放到开头或结尾,问最少需要操作多少次可 ...

  9. &lbrack;Codeforces&rsqb;605E Intergalaxy Trips

    小C比较棘手的概率期望题,感觉以后这样的题还会贴几道出来. Description 给定一个n*n的邻接矩阵,邻接矩阵中元素pi,j表示的是从 i 到 j 这条单向道路在这一秒出现的概率百分比,走一条 ...

随机推荐

  1. 关于WebGIS开源解决方案的探讨

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 公司目前的多数项目采用的是ArcGIS产品+Oracle+W ...

  2. Angularjs 中使用指令绑定点击事件

    项目中,模板中的菜单是jQuery控制的,在Angularjs中就运行不到了,因为菜单项是ng-repeat之后的. 如html <ul id="main-menu"> ...

  3. RichTextBox选中文本时往自己的其他的位置实现拖拽

          private void Form1_Load(object sender, EventArgs e) { richTextBox1.AllowDrop = true; richTextB ...

  4. knockoutJS 快速上手

    翻译:Knockout 快速上手 - 3: knockoutJS 快速上手 许多时候,学会一种技术的有效方式就是使用它解决实际中的问题.在这一节,我们将学习使用 Knockout 来创建一个常见的应用 ...

  5. spring事务学习笔记

    事务管理器 default使用数据库默认的隔离级别

  6. LeetCode--017--电话号码的字母组合(java)

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...

  7. &lbrack;转&rsqb; 学会fetch的用法

    fetch是web提供的一个可以获取异步资源的api,目前还没有被所有浏览器支持,它提供的api返回的是Promise对象,所以你在了解这个api前首先得了解Promise的用法.参考阮老师的文章 那 ...

  8. Spring Boot干货系列:(七)默认日志框架配置

    Spring Boot干货系列:(七)默认日志框架配置 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候, ...

  9. 20155234 2006-2007-2 《Java程序设计》第4周学习总结

    20155234 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 为了避免重复的行为定义使用继承. 要学会如何正确判断使用继承的时机以及继承之后如何活用多态. ...

  10. TI 28335和AD采集

    使用TI 28335和片外AD7606,一个AD有8个通道可以采集,激活AD采集: #define EXTADLZ0 *(int *)0x4200 // Zone 0, ADC data, ADCH1 ...