http://acm.hdu.edu.cn/showproblem.php?pid=4318

很裸的spfa

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<cmath>
#define LL long long

using namespace std;

const int N=50040;
const double M=200000000.0;
struct node
{
    struct tt *next;
}mem[N];
struct tt
{
    struct tt *next;
    int j;
    int k;
};
void build(int i,int j,int k)
{
    struct tt *t=new tt;
    t->j=j;
    t->k=k;
    t->next=mem[i].next;
    mem[i].next=t;
}
void Dele(int n)
{
    for(int i=1;i<=n;++i)
    {
        mem[i].next=NULL;
    }
}
int n;
double dist[N];
void  spfa(int st,int nd,double power)
{
    for(int i=1;i<=n;++i)
    {
        if(i==st)
        dist[i]=power;
        else
        dist[i]=-1.0;
    }
    bool in[N];
    memset(in,false,sizeof(in));
    queue<int>str;
    in[st]=true;
    str.push(st);
    struct tt *t;
    while(!str.empty())
    {
        int x=str.front();
        in[x]=false;
        str.pop();
        t=mem[x].next;
        while(t!=NULL)
        {
            if(dist[x]*(t->k)/100.0>dist[t->j])
            {
                dist[t->j]=dist[x]*(t->k)/100.0;
                if(!in[t->j])
                {
                    in[t->j]=true;
                    str.push(t->j);
                }
            }
            t=t->next;
        }
    }
}
int main()
{
    //freopen("data.txt","r",stdin);
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;++i)
        {
            int j,k,w;
            scanf("%d",&w);
            while(w--)
            {
                scanf("%d %d",&j,&k);
                build(i,j,100-k);
            }
        }
        int st,nd;
        double power;
        scanf("%d %d %lf",&st,&nd,&power);
        if(st==nd)
        {
            printf("0.00\n");
            continue;
        }
        spfa(st,nd,power);
        if(dist[nd]<0.0)
        printf("IMPOSSIBLE!\n");
        else
        {
            printf("%.2lf\n",power-dist[nd]);
        }
        Dele(n);
    }
    return 0;
}

  

posted on 2012-07-28 07:59  夜->  阅读(179)  评论(0编辑  收藏  举报