HDOJ 4318 —— dijkstra + 优先队列 求最短路

来源:互联网 发布:几何画图软件 编辑:程序博客网 时间:2024/05/21 21:41

Power transmission

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


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
422 503 7021 304 2021 104 4001 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
 
一道简单的最短路,无trick。据说O(n^2)也能过?
#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>#include <utility>using namespace std;//#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define lson l , mid  , rt << 1#define rson mid + 1 , r , rt << 1 | 1//#define mid ((l + r) >> 1)#define mk make_pair#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)const int MAXN = 50000 + 500;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x3f3f3f3f;const int IMIN = 0x80000000;const double E = 2.718281828;#define eps 1e-8#define DEBUG 1#define mod 1000000007typedef long long LL;const double PI = acos(-1.0);typedef double D;typedef pair<int , int> pi;///#pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];int n , m;int vis[MAXN];double dis[MAXN] , sum;int st , en ;struct Edge{    double w;    int  v;    Edge(int vv , double ww)    {        v = vv;        w = ww;    }};vector <Edge> e[MAXN];struct  Node{    int v ;    double w;    Node(int vv , double ww)    {        v = vv;        w = ww;    }    bool operator < (const  Node &a)const    {        return w < a.w;    }};priority_queue <Node> pq;void dijkstra(){    FORR(i , 1 , n)    {        vis[i] = 0;        dis[i] = -1.0;    }    while(!pq.empty())pq.pop();    pq.push(Node(st , sum));    while(!pq.empty())    {        Node cur = pq.top();        pq.pop();        int u = cur.v;        double w = cur.w;        if(vis[u])continue;        vis[u] = 1;        dis[u] = w;        FOR(i , 0 , e[u].size())        {            int uu = e[u][i].v;            double ww = e[u][i].w;            if(dis[uu] < w * ww)            {                dis[uu] = w * ww;                pq.push(Node(uu , dis[uu]));            }        }    }}int main(){    while(~scanf("%d" , &n))    {        clr(e , 0);        FORR(i , 1,  n)        {            int v ;            double w;            scanf("%d" , &m);            while(m--)            {                scanf("%d%lf" , &v , &w);                e[i].push_back(Edge(v, (100.0 - w) / 100.0));            }        }        scanf("%d%d%lf" , &st , &en , &sum);        dijkstra();        if(dis[en] == -1)puts("IMPOSSIBLE!");        else printf("%.2lf\n" , sum - dis[en]);    }    return 0;}


原创粉丝点击