HDOJ 4318 —— dijkstra + 优先队列 求最短路
2013-10-03 17:10
429 查看
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
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
一道简单的最短路,无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 1000000007 typedef 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; }
相关文章推荐
- hdu-2544-最短路(Dijkstra + Dijkstra优先队列 + Bellman-ford + SPFA +Floyd) 纯模板题
- Codeforces 128B. String(优先队列,模仿dijkstra求最短路)
- Codeforces Round #287 (Div. 2) E. Breaking Good [Dijkstra 最短路 优先队列]
- usaco2.4 comehome 最短路 Floyd, Dijkstra, Dijkstra优先队列优化
- 最短路--dijkstra+优先队列优化模板
- dijkstra 优先队列最短路模板
- hdu2544 最短路 dijkstra的使用优先队列优化的比较
- HDU ~ 2544 ~ 最短路 (Dijkstra模板,常规版 and 优先队列优化版)
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
- ACM:最短路,dijkstra,邻接表的建立,使用邻接表跟优先队列的dijkstra,Bellman-Ford,Floyd。。
- 最短路(Dijkstra) HDOJ 4318 Power transmission
- 蓝桥杯 算法训练 最短路 【SPFA队列优化 + dijkstra优先队列优化】
- uva 11374 最短路+记录路径 好题 dijkstra优先队列优化算法 邻接表法 可做模板 G++提交
- 最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)
- POJ1724 ROADS(深搜DFS,最短路,dijkstra,用优先队列优化)
- hdu 2066最短路 dijkstra 及其优先队列优化
- hdoj 5521 Meeting 【优先队列 dijkstra】
- 【原创】最短路模板 Floyd,优先队列优化dijkstra,SPFA
- poj3463 hdu1688 次短路+优先队列 如果让我说:我只能说,实力决定一切
- hdoj 4857 逃生【拓扑排序&&优先队列】