zl程序教程

您现在的位置是:首页 >  其它

当前栏目

poj 2472

poj
2023-09-11 14:14:00 时间
题意:
     给你一个无向图,然后每条边的权值就是不被抓的概率,有个货要从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\n" ,s_x[n] * 100);
   }
   return 0;
}