hdu 4318 (最短路)

Power transmission

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


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
2012 Multi-University Training Contest 2
 

Recommend
zhuyuanchen520
 
每条边经过后有一个百分比的损耗,求从起点到终点最多剩余能量。开始想用dp的记忆化搜索写,爆栈了。因为最后的结果是整条路径上所有百分比的乘积,对于这种连乘的最优化问题可以通过取对数转化为为累加的最优化问题,这样就把问题转化成最短路了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include<queue>
#include<set>
#include<cmath>
using namespace std;
typedef long long LL;
typedef pair<double,int> P;
const int maxn = 50000 + 5;
const int INF = 1000000000;

int n;
struct Edge{
    int to;
    double dis;
    Edge(int to,double dis){
        this -> to = to;
        this -> dis = dis;
    }
};

double d[maxn];
int vis[maxn];
vector<Edge> G[maxn];

void Dij(int x){
    priority_queue<P,vector<P>,greater<P> > Q;
    memset(vis,0,sizeof(vis));
    for(int i = 0;i <= n;i++) d[i] = 10000.0;
    Q.push(P(0,x));
    while(!Q.empty()){
        P p = Q.top();Q.pop();
        int id = p.second;
        double dis = p.first;
        if(vis[id] == 1) continue;
        vis[id] = 1;
        d[id] = dis;
        for(int i = 0;i < G[id].size();i++){
            Edge edgs = G[id][i];
            int to = edgs.to;
            double der = edgs.dis;
            if(d[to] > d[id] + der){
                d[to] = d[id] + der;
                Q.push(P(d[to],to));
            }
        }
    }
}

int main(){
    while(scanf("%d",&n) != EOF){
        for(int i = 0;i < maxn;i++) G[i].clear();
        for(int i = 1;i <= n;i++){
            int x;scanf("%d",&x);
            while(x--){
                int to;
                double dis;
                scanf("%d%lf",&to,&dis);
                G[i].push_back(Edge(to,-log(1.0-dis/100)));
            }
        }
        int s,t,total;
        scanf("%d%d%d",&s,&t,&total);
        Dij(s);
        double ans = d[t];
        printf("%.2lf\n",total*(1-exp(-ans)));
    }
    return 0;
}


你可能感兴趣的:(hdu 4318 (最短路))