【nyoj1274zzuli1925】信道安全(SPFA+vector)

xiaoxiao2021-02-28  10

Description

Alpha 机构有自己的一套网络系统进行信息传送。情报员 A 位于节点 1,他准备将一份情报 发送给位于节点 n 的情报部门。可是由于最近国际纷争,战事不断,很多信道都有可能被遭到监 视或破坏。 经过测试分析,Alpha 情报系统获得了网络中每段信道安全可靠性的概率,情报员 A 决定选 择一条安全性最高,即概率最大的信道路径进行发送情报。 你能帮情报员 A 找到这条信道路径吗?

Input

第一行: T 表示以下有 T 组测试数据 ( 1≤T ≤8 ) 对每组测试数据: 第一行: n m  分别表示网络中的节点数和信道数 (1<=n<=10000,1<=m<=50000) 接下来有 m 行, 每行包含三个整数 i,j,p,表示节点 i 与节点 j 之间有一条信道,其信 道安全可靠性的概率为 p%。 ( 1<=i, j<=n 1<=p<=100)

Output

每组测试数据,输出占一行,一个实数 即情报传送到达节点 n 的最高概率,精确到小数点后 6 位。

Sample Input

1 5 7 5 2 100 3 5 80 2 3 70 2 1 50 3 4 90 4 1 85 3 1 70

Sample Output

61.200000

SPFA+vector实现:

#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<math.h> #include<vector> #include<queue> using namespace std; typedef long long LL; #define N 10006 #define esp 0.0005 double dist[N]; struct edge { int to; double cost; }; int n; vector<edge>q[N]; int vis[N]; void SPFA(int s) { int i,u,v; double w; queue<int>Q; for(i=1; i<=n; i++) { vis[i]=0; dist[i]=0; } dist[1]=1; Q.push(s); while(!Q.empty()) { u=Q.front(); Q.pop(); vis[u]=0; //出队,下车 for(i=0; i<q[u].size(); i++) { v=q[u][i].to,w=q[u][i].cost; if(dist[v]<dist[u]*w) { dist[v]=dist[u]*w; if(!vis[v]) { vis[v]=1; //入队,上车 Q.push(v); } } } } } int main() { int m,t,a,i,b,w; cin>>t; while(t--) { scanf("%d%d",&n,&m); for(i=1; i<=n; i++) q[i].clear(); edge pp; while(m--) { scanf("%d%d%d",&a,&b,&w); pp.to=b; pp.cost=w/100.0; q[a].push_back(pp); pp.to=a; q[b].push_back(pp); } SPFA(1); printf("%.6f\n",dist[n]*100); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-750172.html

最新回复(0)