zoj 2233 Pollution

zoj 2233 Pollution,第1张

zoj 2233 Pollution
#include<iostream>#include<cstring>#include<set>#include<stdio.h>using namespace std;enum {    SIZ = 108,};struct Node {    int n;    int d[SIZ];};Node tank[SIZ];set<int> s[SIZ];double dat[2][SIZ];double eps = 1e-4;int T,N,M;bool same(double *a, double *b){    for(int i=1; i<=N; i++){        if( (a[i]>b[i]&&a[i]-b[i]>eps) || (b[i]>a[i]&&b[i]-a[i]>eps)) return false;    }    return true;}void loop(double *cur, double *nex){    memset(nex, 0, sizeof(dat[0]));    int i,j;    double t;    for(i=1; i<=N; i++){        if(tank[i].n==0){ nex[i] = cur[i]; continue;        }        t = cur[i]/tank[i].n;        for(j=0; j<tank[i].n; j++){ nex[tank[i].d[j]] += t;        }    }}void output(double *cur){    for(int i=1; i<=N; i++){        printf("%.3lfn", cur[i]);    }    printf("n");}void fun(){    double *cur = dat[0], *nex=dat[1], *tmp;    do{        loop(cur, nex);        tmp=cur, cur=nex, nex=tmp;    }while(!same(cur, nex));    output(cur);}void readIn(){    scanf("%d%d ", &N,&M);    for(int i=1; i<=N; i++){        scanf("%lf ", &dat[0][i]);        s[i].clear();    }    int a,b;    for(int i=1; i<=M; i++){        scanf("%d%d ", &a,&b);        s[a].insert(b);        s[b].insert(a);    }    for(int i=1; i<=N; i++){        tank[i].n = s[i].size();        a=0;        for(set<int>::iterator it=s[i].begin();     it!=s[i].end(); ++it){ tank[i].d[a++] = *it;        }    }}int main(){    scanf("%d ", &T);    while(T--){        readIn();        fun();    }    return 0;}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4929357.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存