zoj1655(最短路)

来源:互联网 发布:网络黄金egd还有希望吗 编辑:程序博客网 时间:2024/06/04 01:36

//求n到其他点的损率的乘积最大 #include<cstdio>#include<iostream>#include<cstring>#include<queue>using namespace std;const int mn=105,mm=40005;struct Edge{int to,next;double w;}edges[mm];int n,m,head[mn],tot,inq[mn];double dis[mn],w[mn];void add(int u,int v,double w){edges[tot].to=v;edges[tot].w=w;edges[tot].next=head[u];head[u]=tot++;edges[tot].to=u;edges[tot].w=w;edges[tot].next=head[v];head[v]=tot++;}void spfa()  {memset(dis,0,sizeof(dis));//这个地方要注意啊     memset(inq,0,sizeof(inq));      dis[n]=1;      inq[n]=1;      deque<int> q;      q.push_back(n);      while(q.size())      {          int x=q.front();          q.pop_front();          inq[x]=0;          for(int i=head[x];~i;i=edges[i].next)          {              int v=edges[i].to;              if(dis[v]<dis[x]*edges[i].w)              {                  dis[v]=dis[x]*edges[i].w;                  if(!inq[v])                {                    if(q.size()&&dis[v]>dis[q.front()])                      q.push_front(v);                      else q.push_back(v);                      inq[v]=1;                  }              }          }      }  }int main(){int u,v;double ww;while(~scanf("%d%d",&n,&m)){tot=0;memset(head,-1,sizeof(head));for(int i=1;i<n;++i)scanf("%lf",w+i);for(int i=0;i<m;++i){scanf("%d%d%lf",&u,&v,&ww);add(u,v,1-ww);}spfa();double ans=0;for(int i=1;i<n;++i)ans+=dis[i]*w[i];printf("%.2lf\n",ans);}return 0;}


原创粉丝点击