SPFA: Power Transmission

Power transmission

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


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
 
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define INF 1e9

int beg, end, m;

struct node                 //静态邻接表
{
    int to,next;          //next为下一条边的编号
    double w;
} e[3000000];
int lastshow[100010];
double dis[100010];
bool inqueue[100010];
int t,n,cnt;
queue<int>q;

void insert(int a,int b,double w)          //插入a指向b的边,权值为w
{
    e[cnt].to=b;
    e[cnt].w=w;
    e[cnt].next=lastshow[a];
    lastshow[a]=cnt++;
}
bool spfa()
{
    q.push(beg);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        inqueue[x]=false;                               //清除在队列中的标志
        int id=lastshow[x];
        while(id!=-1)
        {
            if(dis[e[id].to] < dis[x]*e[id].w)
            {
                dis[e[id].to] = dis[x] * e[id].w;
                if(!inqueue[e[id].to])                  //如果已经在队列中就不要重复加了
                {
                    inqueue[e[id].to]=true;
                    q.push(e[id].to);
                }
            }
            id=e[id].next;
        }
    }
    return true;
}

void ini()
{
    for(int i=0; i<=n; ++i){
        inqueue[i] = false;
        dis[i] = 0;
    }
    dis[beg] = 1.0;
    cnt=0;
    while(!q.empty())
    {
        q.pop();
    }
}
int main()
{
    int i, j, k;
    double ww;
    int a, b;
    int ki;
    double ans;
    while(cin >> n){
        int a,b;
        double w;
        for(i = 0 ; i <=n ; i ++)
            lastshow[i] = -1;
        for(i=1; i<= n; ++i){
            scanf("%d", &ki);
            for(j = 1; j <= ki; j ++){
                scanf("%d", &b);
                scanf("%lf", &ww);
                w = 1.0 - ww / 100.0;
                insert(i,b,w);
            }
        }
        scanf("%d %d %d", &beg, &end, &m);
        ini();
        spfa();
        if(0 == dis[end])
            printf("IMPOSSIBLE!\n");
        else{
            ans = (double)m * (1.0 - dis[end]);
            printf("%.2lf\n", ans);
        }
    }
    return 0;
}


你可能感兴趣的:(SPFA: Power Transmission)