TYVJ P1933 绿豆蛙的归宿 题解(未完成)

P1933 「Poetize3」绿豆蛙的归宿

时间: 1000ms / 空间: 131072KiB / Java类名: Main

背景

随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿。

描述

给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出发能够到达所有的点,所有的点也都能够到达终点。绿豆蛙从起点出发,走向终点。
到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率为 1/K 。
现在绿豆蛙想知道,从起点走到终点的所经过的路径总长度期望是多少?

输入格式

第一行: 两个整数 N M,代表图中有N个点、M条边
第二行到第 1+M 行: 每行3个整数 a b c,代表从a到b有一条长度为c的有向边

输出格式

从起点到终点路径总长度的期望值,四舍五入保留两位小数。

测试样例1

输入

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

输出

7.00

备注

对于20%的数据   N<=100
对于40%的数据   N<=1000
对于60%的数据   N<=10000
对于100%的数据  N<=100000,M<=2*N

——————我是华丽丽的分割线——————————————

数学期望DP

好题,难题。很费解。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<cstdlib>
 8 #include<iomanip>
 9 #include<cassert>
10 #include<climits>
11 #define maxn 10001
12 #define F(i,j,k) for(int i=j;i<=k;i++)
13 #define M(a,b) memset(a,b,sizeof(a))
14 #define FF(i,j,k) for(int i=j;i>=k;i--)
15 #define inf 0x7fffffff
16 using namespace std;
17 int read(){
18     int x=0,f=1;char ch=getchar();
19     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
20     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
21     return x*f;
22 }
23 struct edge{
24     int to,w,next;
25 }p[200010];
26 int tot,n;
27 int head[100010];
28 void addedge(int a,int b,int c){
29     p[tot].to=b;
30     p[tot].w=c;
31     p[tot].next=head[a];
32     head[a]=tot++;
33 }
34 double dp[100010];
35 int out[100010],q[100010];
36 int out1[100010];
37 int main()
38 {
39     std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
40 //  freopen("data.in","r",stdin);
41 //  freopen("data.out","w",stdout);
42     int m,a,b,c;
43     cin>>n>>m;
44     M(head,-1);
45     while(m--){
46         cin>>a>>b>>c;
47         addedge(b,a,c);
48         out[a]=++out1[a];
49     }
50     int s=0,e=-1;
51     q[++e]=n;
52     while(s<=e){
53         s++;
54         int u=q[s];
55         for(int i=head[u];i!=-1;i=p[i].next){
56             int v=p[i].to;
57             dp[v]+=(dp[u]+p[i].w)/out[v];
58             if(--out1[v]==0) q[++e]=v;
59         }
60     }
61     cout<<setiosflags(ios::fixed)<<setprecision(2)<<dp[1];
62     cout<<endl;
63     return 0;
64 }

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#define maxn 10001
#define F(i,j,k) for(int i=j;i<=k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x7fffffff
using namespace std;
int read(){
int x=0,f=1;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;
}
struct edge{
int to,w,next;
}p[200010];
int tot,n;
int head[100010];
void addedge(int a,int b,int c){
p[tot].to=b;
p[tot].w=c;
p[tot].next=head[a];
head[a]=tot++;
}
double dp[100010];
int out[100010],q[100010];
int out1[100010];
int main()
{
std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
int m,a,b,c;
cin>>n>>m;
M(head,-1);
while(m--){
cin>>a>>b>>c;
addedge(b,a,c);
out[a]=++out1[a];
}
int s=0,e=-1;
q[++e]=n;
while(s<=e){
s++;
int u=q[s];
for(int i=head[u];i!=-1;i=p[i].next){
int v=p[i].to;
dp[v]+=(dp[u]+p[i].w)/out[v];
if(--out1[v]==0) q[++e]=v;
}
}
cout<<setiosflags(ios::fixed)<<setprecision(2)<<dp[1];
cout<<endl;
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#define maxn 10001
#define F(i,j,k) for(int i=j;i<=k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x7fffffff
using namespace std;
int read(){
    int x=0,f=1;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;
}
struct edge{
    int to,w,next;
}p[200010];
int tot,n;
int head[100010];
void addedge(int a,int b,int c){
    p[tot].to=b;
    p[tot].w=c;
    p[tot].next=head[a];
    head[a]=tot++;
}
double dp[100010];
int out[100010],q[100010];
int out1[100010];
int main()
{
    std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
//  freopen("data.in","r",stdin);
//  freopen("data.out","w",stdout);
    int m,a,b,c;
    cin>>n>>m;
    M(head,-1);
    while(m--){
        cin>>a>>b>>c;
        addedge(b,a,c);
        out[a]=++out1[a];
    }
    int s=0,e=-1;
    q[++e]=n;
    while(s<=e){
        s++;
        int u=q[s];
        for(int i=head[u];i!=-1;i=p[i].next){
            int v=p[i].to;
            dp[v]+=(dp[u]+p[i].w)/out[v];
            if(--out1[v]==0) q[++e]=v;
        }
    }
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<dp[1];
    cout<<endl;
    return 0;
}
 

C
C++
C++11
Java
Pascal
Python 2.7
Python 3.3
Ruby
C#
VB.Net
F#
清空代码

时间: 2024-08-03 02:29:03

TYVJ P1933 绿豆蛙的归宿 题解(未完成)的相关文章

[题解] [LuoguP4316] 绿豆蛙的归宿

[题解] [LuoguP4316] 绿豆蛙的归宿 一.前言 ? 今天本着停课不停学的决心,看了看数学期望.结果是 见不熟悉之数学起身慌而走之.话虽然这么说,但是例题还是要做的,题解也是要写的.凭本人小学水平数学,粗略的理解数学期望就是操作的足够多了之后趋于平均的一个?的东西.,嘛,就相当于骰子扔多了平均值就会趋于一个常数,但是怎么都不会扔出7就对了.(此处为古贺朋绘酱默哀qwq,永远都做不出自己想要的梦我永远喜欢古贺朋绘) 二.题意简述 ? 说了一个大点的废话以后,进入这一篇的正题. ? 题意:

BZOJ3036: 绿豆蛙的归宿

3036: 绿豆蛙的归宿 Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 362  Solved: 255[Submit][Status][Discuss] Description 随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度.绿豆蛙从起点出发,走向终点. 到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率

BZOJ3036: 绿豆蛙的归宿&amp;Wikioi2488:绿豆蛙的归宿

3036: 绿豆蛙的归宿 Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 108  Solved: 73[Submit][Status] Description 随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度.绿豆蛙从起点出发,走向终点.到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率为 1/K .现在绿豆

【BZOJ3036】绿豆蛙的归宿 拓补排序+概率

[BZOJ3036]绿豆蛙的归宿 Description 随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度.绿豆蛙从起点出发,走向终点.到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率为 1/K .现在绿豆蛙想知道,从起点走到终点的所经过的路径总长度期望是多少? Input 第一行: 两个整数 N M,代表图中有N个点.M条边第二行到第 1+M 行: 每

2488 绿豆蛙的归宿

2488 绿豆蛙的归宿 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出发能够到达所有的点,所有的点也都能够到达终点.绿豆蛙从起点出发,走向终点. 到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率为 1/K . 现

25.绿豆蛙的归宿(拓扑排序)

绿豆蛙的归宿(拓扑排序) 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出发能够到达所有的点,所有的点也都能够到达终点.绿豆蛙从起点出发,走向终点. 到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率为 1/K . 现在绿豆蛙想知

AC日记——绿豆蛙的归宿 codevs 2488

绿豆蛙的归宿 思路: topsort+期望dp: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 100005 int head[maxn],cnt,E[maxn<<2],V[maxn<<2],du[maxn]; int sta[maxn],top,n,

codevs 2488 绿豆蛙的归宿

2488 绿豆蛙的归宿 http://codevs.cn/problem/2488/ 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出发能够到达所有的点,所有的点也都能够到达终点.绿豆蛙从起点出发,走向终点. 到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该

【BZOJ 3036】 3036: 绿豆蛙的归宿 (概率DP)

3036: 绿豆蛙的归宿 Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 491  Solved: 354 Description 随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度.绿豆蛙从起点出发,走向终点.到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率为 1/K .现在绿豆蛙想知道,从起点走到终点的所经