afterward

导航

 
View Code
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<queue>
using namespace std;

struct node{
    int dest;
    double value;
    node* next;
    node()
    {
        next=NULL;
    }
}edge[50010];//构造点
int n,s,t;
double power;
double len[50010];
node temp[2500010];
queue<int> ans;

void bfs()
{
    int index;
    node* p;
    ans.push(s);
    while(!ans.empty())
    {
        index=ans.front();
        ans.pop();
        p=edge[index].next;//取出第一个相邻的点
        while(p!=NULL)
        {
            if(len[p->dest]<len[index]*(p->value))//这个点的能量比从上一个点传过来的能量要小
            {
               // cout<<p->dest<<" "<<len[p->dest]<<" "<<index<<" "<<len[index]<<" "<<p->value<<endl;
                ans.push(p->dest);cout<<p->dest<<endl;
                len[p->dest]=len[index]*(p->value);//换成能量大的
            }
            p=p->next;//找下一个相邻的点h
        }
    }
               //cout<<len[1]<<" "<<len[2]<<" "<<len[3]<<" "<<len[4]<<endl;
}
int main()
{
    int i,j,k;
    int x;
    double y;
    node* p;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1,j=0;i<=n;i++)
        {
            len[i]=0;
            scanf("%d",&k);

            while(k--)
            {
                scanf("%d%lf",&x,&y);
                p=&temp[j++];
                p->dest=x;
                p->value=(100-y)/100;
                p->next=edge[i].next;
                edge[i].next=p;
            }
        }
        scanf("%d%d%lf",&s,&t,&power);//1  4  100
        len[s]=power;//在点的剩余能量
        bfs();
        if(len[t]==0)
            cout<<"IMPOSSIBLE!"<<endl;
        else
            cout<<setiosflags(ios::fixed)<<setprecision(2)<<(len[s]-len[t])<<endl;
        for(i=0;i<j;i++)
        {
            temp[i].next=NULL;
        }
        for(i=0;i<n;i++)
        {
            edge[i].next=NULL;
        }
    }
    return 0;
}

 

Power transmission 有向图问题

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

posted on 2012-08-03 19:02  afterward  阅读(265)  评论(0编辑  收藏  举报