zoukankan      html  css  js  c++  java
  • poj 2472

    题意:
         给你一个无向图,然后每条边的权值就是不被抓的概率,有个货要从1逃到n,问你他的最安全概率是多少?

    思路:

          水题,直接跑就行了,一开始自己想多了,还转换了一下log,后来发现转换之后会有正环,有正环求最长路就呵呵了,直接跑就行了,具体看代码,我写的是spfa.


    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<queue>
    
    #define N_node 100 + 10
    #define N_edge 5500
    #define INF 10000000
    
    using namespace std;
    
    typedef struct
    {
       int to ,next;
       double cost;
    }STAR;
    
    STAR E[N_edge];
    int list[N_node] ,tot;
    double s_x[N_node];
    
    void add(int a ,int b ,double c)
    {
       E[++tot].to = b;
       E[tot].cost = c;
       E[tot].next = list[a];
       list[a] = tot;
    }
    
    void Spfa(int s ,int n)
    {
       int mark[N_node] = {0};
       for(int i = 0 ;i <= n ;i ++)
       s_x[i] = -INF;
       s_x[s] = 1 ,mark[s] = 1;
       queue<int>q;
       q.push(s);
       while(!q.empty())
       {
          int tou ,xin;
          tou = q.front();
          q.pop();
          mark[tou] = 0;
          for(int k = list[tou] ;k ;k = E[k].next)
          {
             xin = E[k].to;
             if(s_x[xin] < s_x[tou] * E[k].cost)
             {
                s_x[xin] = s_x[tou] * E[k].cost;
                if(!mark[xin])
                {
                   mark[xin] = 1;
                   q.push(xin);
                }
             }
          }
       }
       return ;
    }
    
    int main ()
    {
       int n ,m ,a ,b ,c ,i;
       while(~scanf("%d" ,&n) && n)
       {
          scanf("%d" ,&m);
          memset(list ,0 ,sizeof(list)) ,tot = 1;
          for(i = 1 ;i <= m ;i ++)
          {
             scanf("%d %d %d" ,&a ,&b ,&c);
             add(a ,b ,c * 0.01);
             add(b ,a ,c * 0.01);
          }
          Spfa(1 ,n);
          printf("%lf percent
    " ,s_x[n] * 100);
       }
       return 0;
    }
       

  • 相关阅读:
    leetcode 15. 三数之和
    leetcode 168. Excel表列名称
    142. 环形链表 II
    144. 二叉树的前序遍历(非递归)
    415. 字符串相加
    剑指 Offer 31. 栈的压入、弹出序列
    剑指 Offer 27. 二叉树的镜像
    剑指 Offer 29. 顺时针打印矩阵
    剑指 Offer 10- II. 青蛙跳台阶问题
    VBA 实现excel单元格多选
  • 原文地址:https://www.cnblogs.com/csnd/p/12062727.html
Copyright © 2011-2022 走看看