zoukankan      html  css  js  c++  java
  • MUTC 2 D Power transmission 最短路

    Power transmission

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 1403    Accepted Submission(s): 533


    Problem Description
    The project West-East power transmission is famous around the world. It transmits the electricity from western areas to east China. There are many nodes in the power system. Each node is connected with several other nodes in the system by cable. Power can be only transmitted between two connected nodes. For each node, it can’t send power to two or more other nodes at the same time. 
    As we have all known, power will be loss during the transmission. Bob is the chief engineer of the project. He wants to build a transmission line which send power from one node to another node and minimize the power loss at the same time. Now he asks you to help him solve the problem.
     

    Input
    There are several test cases. For each test case, the first line contains an integer N (0 < N ≤ 50000) which represents the number of nodes in the power system. Then there will be N groups of data following. For the i-th(0 < i ≤ N) group, the first line is an integer ki (ki ≤ 50), which means the node i is connected with ki nodes. The rest of the i-th group data are divided into ki lines. Each line contains an integer ai (0 < ai ≤ N, ai ≠ i) and an integer bi (0 ≤ bi ≤ 100), which represents power can be transmitted from node i to ai and will loss bi% while transmitting. The last line of input data contains three integers separated by single spaces. The first one is s, the second is t (0 < s, t ≤ N), and the third is the total power M (0 < M ≤ 10^6) at node s.
     

    Output
    For each test case, output the minimum of loss power while transmitting from node s to node t. The result should be printed with two digits to the right of the decimal point. If power cannot be transmitted from node s to node t, output “IMPOSSIBLE!” in a line.
     

    Sample Input
    4 2 2 50 3 70 2 1 30 4 20 2 1 10 4 40 0 1 4 100
     

    Sample Output
    60.00
    Hint
    In the sample, the best transmission line is 1 -> 2 -> 4, loss power is 100 * 50% + 100 * (100%-50%)*20% = 60.00
     

    Author
    TJU
     

    Source
     

    Recommend
    zhuyuanchen520
     

    --------------------

    最小消耗即最大剩余。

    选一条路径使(1-b1%)*(1-b2%)....*(1-bm%)最大

    堆优化的dijkstra或spfa皆可

    --------------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    
    using namespace std;
    
    const int maxn=55555;
    const int maxm=111111111;
    const double OO=1e30;
    
    struct HeapNode
    {
        double d;
        int u;
        bool operator<(const HeapNode& rhs) const
        {
            return d<rhs.d;
        }
    };
    
    struct EDGENODE
    {
        int to;
        int w;
        int next;
    }edges[maxm];
    int edge,head[maxn];
    void addedge(int u,int v,int c)
    {
        edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
    }
    void init()
    {
        memset(head,-1,sizeof(head));
        edge=0;
    }
    int n;
    double dist[maxn];
    bool visit[maxn]= {0};
    /*
    void dijkstra(int src)
    {
        int u,v,w;
        double _max;
        memset(visit,0,sizeof(visit));
        memset(dist,0,sizeof(dist));
        dist[src]=1;
        for (int loop=1; loop<=n; loop++)
        {
            //cerr<<loop<<endl;
            u=0;
            _max=0.0;
            for (int i=1; i<=n; i++)
            {
                //printf("%0.2lf ",dist[i]);
                if (!visit[i]&&dist[i]>_max)
                {
                    _max=dist[i];
                    u=i;
                }
            }
            //printf("\n");
            if (u==0) return;
            visit[u]=true;
            for (int i=head[u];i!=-1;i=edges[i].next)
            {
                v=edges[i].to;
                w=edges[i].w;
                if (!visit[v]&&dist[u]*(1.0-(double)w/100.0)>dist[v])
                {
                    dist[v]=dist[u]*(1.0-(double)w/100.0);
                }
            }
        }
    }
    */
    
    void dijkstra(int src)
    {
        int u,v,w;
        priority_queue<HeapNode>que;
        memset(visit,0,sizeof(visit));
        memset(dist,0,sizeof(dist));
        dist[src]=1;
        que.push((HeapNode){0,src});
        while (!que.empty())
        {
            HeapNode x=que.top();
            que.pop();
            u=x.u;
            if (visit[u]) continue;
            visit[u]=true;
            for (int i=head[u]; i!=-1; i=edges[i].next)
            {
                v=edges[i].to;
                w=edges[i].w;
                if (dist[u]*(1.0-(double)w/100.0)>dist[v])
                {
                    dist[v]=dist[u]*(1.0-(double)w/100.0);
                    que.push((HeapNode){dist[v],v});
                }
            }
        }
    }
    
    
    
    /*
    bool spfa(int node,int src,int head[],EDGENODE edges[],double dist[])
    {
        int i,l,r,u,v,w;
        bool visit[maxn];
        int q[maxn],outque[maxn];
        memset(visit,0,sizeof(visit));
        memset(outque,0,sizeof(outque));
        for (int i=0; i<=node; i++) dist[i]=0;
        r=0;
        q[r++]=src;
        dist[src]=1;
        visit[src]=true;
        for (l=0; l!=r; ( (++l>=maxn)?(l=0):(1) ))
        {
            u=q[l];
            visit[u]=false;
            outque[u]++;
            if (outque[u]>node) return false;
            for (i=head[u]; i!=-1; i=edges[i].next)
            {
                v=edges[i].to;
                w=edges[i].w;
                if (dist[u]*(1.0-(double)w/100.0)>dist[v])
                {
                    dist[v]=dist[u]*(1.0-(double)w/100.0);
                    if (visit[v]) continue;
                    q[r++]=v;
                    visit[v]=true;
                    if (r>=maxn) r=0;
                }
            }
        }
        return true;
    }
    */
    
    int main()
    {
        int t;
        int src,dest;
        int M;
        //freopen("abc.in","r",stdin);
        //freopen("abc.out","w",stdout);
        while (~scanf("%d",&n))
        {
            init();
            for (int u=1; u<=n; u++)
            {
                scanf("%d",&t);
                while (t--)
                {
                    int v,c;
                    scanf("%d%d",&v,&c);
                    addedge(u,v,c);
                }
            }
            scanf("%d%d%d",&src,&dest,&M);
            dijkstra(src);
            //spfa(n,src,head,edges,dist);
            if (dist[dest]>0.000001) printf("%0.2lf\n",M-dist[dest]*M);
            else printf("IMPOSSIBLE!\n");
        }
        return 0;
    }
    







  • 相关阅读:
    Ubuntu安装k8s
    SecureCRT连ubuntu
    硬盘安装ubuntu系统
    发布服务
    使用rancher2建k8s集群--个人学习记录
    spring boot 注解
    用STS构建spring boot
    使用js调用麦克风并录音
    全国省市区信息,mysql数据库记录
    ef core 3 migration
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226354.html
Copyright © 2011-2022 走看看