Black Spot
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Bootstrap: Jones's terrible leviathan will find you and drag the Pearl back to the depths and you along with it.
Jack: Any idea when Jones might release said terrible beastie?
Bootstrap: I already told you, Jack. Your time is up. It comes now, drawn with ravenous hunger to the man what bears the black spot.
Jack: Any idea when Jones might release said terrible beastie?
Bootstrap: I already told you, Jack. Your time is up. It comes now, drawn with ravenous hunger to the man what bears the black spot.
Captain Jack Sparrow has got a black spot on his hand and
he avoids going to high seas because sea monster Kraken
is waiting there for him. But he can’t stay in his place due
to his freedom-loving nature. And now Jack is going to Tortuga.
There are n islands in the Caribbean Sea. Jack is going to reach Tortuga,
sailing from island to island by routes that allow him to be
in the high seas for a short time.
Jack knows such routes for some pairs of islands,
but they could also be dangerous for him.
There is a probability to meet Kraken on each route.
Jack is in a hurry and he wants to reach Tortuga visiting as
small number of islands as possible.
If there are several variants of such paths he wants to
choose a path with the least probability of meeting Kraken.
But Jack will be satisfied with any path with minimal number of islands if the probability
of meeting Kraken on this path differs from the minimal one in no more than 10−6.
Help Jack find such path.
Input
The first line contains two integers n, m — the quantity of islands
and known routes between them (2 ≤ n ≤ 105; 1 ≤ m ≤ 105).
The second line contains two integers s and t —
the number of island where Jack is and the number of Tortuga (1 ≤ s, t ≤ n; s ≠ t).
Each of the following m lines contains three integers —
the numbers of islands ai and bi where the route is known and pi —
probability to meet Kraken on that route as percentage (1 ≤ ai, bi ≤ n; ai ≠ bi; 0 ≤ pi ≤ 99).
No more than one route is known between each pair of islands.
Output
In the first line output k — number of islands along the
path and p — probability to meet Kraken on that path.
An absolute error of p should be up to 10−6.
In the next line output k integers — numbers of islands in the order of the path.
If there are several solutions, output any of them.
Sample
input | output |
---|---|
4 4 1 3 1 2 50 2 3 50 1 4 10 4 3 10 |
3 0.19 1 4 3 |
Problem Author: Denis Dublennykh (prepared by Egor Shchelkonogov)
【分析】就是个最短路。题目要求遇见怪物的概率最小,可以求最大的遇不到怪物的概率。就是在SPFA中当距离相同时维护概率最大就行了。
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <queue> #include <vector> #define inf 0x3f3f3f3f #define met(a,b) memset(a,b,sizeof a) typedef long long ll; using namespace std; const int N = 100005; const int M = 240005; int n,m,cnt=0; int tot=0,s,t; int head[N],dis[N],vis[N],pre[N]; double va[N]; struct man { int to,next; double val; } edg[N]; void add(int u,int v,double val) { edg[tot].to=v; edg[tot].val=val; edg[tot].next=head[u]; head[u]=tot++; edg[tot].to=u; edg[tot].val=val; edg[tot].next=head[v]; head[v]=tot++; } void spfa() { met(dis,inf);met(vis,0); dis[s]=1; queue<int>q; q.push(s); vis[s]=1; va[s]=1; while(!q.empty()) { int w=q.front(); q.pop(); vis[w]=0; for(int i=head[w]; i!=-1; i=edg[i].next) { int v=edg[i].to;//printf("###%d %d %d ",v,dis[v],dis[w]); if(dis[v]>dis[w]+1) { dis[v]=dis[w]+1; pre[v]=w; va[v]=va[w]*edg[i].val; if(!vis[v]) { vis[v]=1; q.push(v); } } else if(dis[v]==dis[w]+1) { if(va[w]*edg[i].val-va[v]>0) { pre[v]=w; va[v]=va[w]*edg[i].val; if(!vis[v]) { vis[v]=1; q.push(v); } } } } } } int main() { int u,v,val; met(head,-1); scanf("%d%d",&n,&m); scanf("%d%d",&s,&t); while(m--) { scanf("%d%d%d",&u,&v,&val); double ff=val*1.0/100; add(u,v,1-ff); } spfa(); printf("%d %.7lf ",dis[t],1-va[t]); stack<int>p; for(int i=t;;i=pre[i]){ p.push(i); if(i==s)break; } while(!p.empty())printf("%d ",p.top()),p.pop();printf(" "); return 0; }