zoukankan      html  css  js  c++  java
  • hdu4318 最短路变形

    和hdu有一题差不多。给的是损失比,1-c%就是保存了多少,找出最大的保存率即可。

    #include<stdio.h> 
    #include<iostream>  
    #include<string.h>
    #include<queue>  
    using namespace std;  
    #define inf 99999999 
    #define maxn 50010 
    #define maxm maxn*51  
    int n,e,head[maxn],pre[maxm],nex[maxm];  
    double cost[maxm],dis[maxn];  
    bool vis[maxn];  
    queue<int> q;  
    void add(int u,int v,double c)  
    {  
        pre[e]=v;
        nex[e]=head[u];
        cost[e]=c;
        head[u]=e++;  
    }  
    double spfa(int st,int end)  
    {  
        memset(dis,0,sizeof(dis));  
        dis[st]=1;  
        q.push(st);  
        while(!q.empty())  
        {  
            int u=q.front();  
            vis[u]=0;  
            q.pop();  
            for(int i=head[u];i!=-1;i=nex[i])  
                if(dis[pre[i]]<dis[u]*(1-cost[i]))  
                {  
                    dis[pre[i]]=dis[u]*(1-cost[i]);  
                    if(!vis[pre[i]])  
                    {  
                        q.push(pre[i]);  
                        vis[pre[i]]=1;  
                    }  
                }  
        }  
        return dis[end];  
    }  
    int main()  
    {  
        while(scanf("%d",&n)!=EOF)  
        {  
            e=0;  
            memset(head,-1,sizeof(head));  
            for(int i=1;i<=n;i++)  
            {  
                int m;  
                scanf("%d",&m);  
                for(int j=0;j<m;j++)  
                {  
                    int u,c;  
                    scanf("%d%d",&u,&c);  
                        add(i,u,c*1.0/100);  
                }  
            }  
            int st,end,tot;  
            scanf("%d%d%d",&st,&end,&tot);  
            double px=spfa(st,end);  
            if(px==0)  
                printf("IMPOSSIBLE!
    ");  
            else  
                printf("%.2f
    ",(1-px)*tot);  
      
        }   
        return 0;  
    }  
  • 相关阅读:
    Codeforces Round #447 Div. 2 A.B.C
    Codeforces Round #445 Div. 2 A ACM ICPC+B Vlad and Cafes
    51Nod 1035 最长的循环节 数论
    Codeforces Round #444 (Div. 2) C.Solution for Cube 模拟
    POJ 3111 K Best
    POJ 2976 Dropping tests
    POJ 3045 Cow Acrobats
    POJ 3045 Cow Acrobats
    POJ 3273
    POJ 3258 River Hopscotch
  • 原文地址:https://www.cnblogs.com/sweat123/p/4749670.html
Copyright © 2011-2022 走看看