poj 2472
題意:
? ? ?給你一個無向圖,然后每條邊的權值就是不被抓的概率,有個貨要從1逃到n,問你他的最安全概率是多少?
思路:
? ? ?給你一個無向圖,然后每條邊的權值就是不被抓的概率,有個貨要從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 10000000using 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; }
總結
- 上一篇: hdu4539 郑厂长系列故事——排兵布
- 下一篇: hdu1024 最大m子序列和