http://acm.hdu.edu.cn/showproblem.php?pid=4318
题意:
给出运输路线,每条路线运输时都会损失一定百分比的量,给定起点、终点和初始运输量,问最后到达终点时最少损失多少量。
思路:
d[u]表示到达该点时剩余量的最大值。
将松弛条件修改为大时更新即可。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 const int maxn = 50000+5; 7 8 int n,m,tot,s,t,M; 9 int head[maxn]; 10 double d[maxn]; 11 bool done[maxn]; 12 13 struct node 14 { 15 int v,next; 16 double w; 17 }e[50*maxn]; 18 19 struct HeapNode 20 { 21 double d; 22 int u; 23 HeapNode(int u, double d):u(u),d(d){} 24 bool operator< (const HeapNode& rhs) const 25 { 26 return d < rhs.d; 27 } 28 }; 29 30 void addEdge(int u, int v, double w) 31 { 32 e[tot].v = v; 33 e[tot].w = w; 34 e[tot].next = head[u]; 35 head[u] = tot++; 36 } 37 38 void dijkstra(int s) 39 { 40 priority_queue<HeapNode> Q; 41 for(int i=0;i<=n;i++) d[i] = 0; 42 d[s] = M; 43 memset(done,0,sizeof(done)); 44 Q.push(HeapNode(s,M)); 45 while(!Q.empty()) 46 { 47 HeapNode x = Q.top(); Q.pop(); 48 int u = x.u; 49 if(done[u]) continue; 50 done[u] = true; 51 for(int i=head[u];i!=-1;i=e[i].next) 52 { 53 int v = e[i].v; 54 if(d[v] < d[u] - d[u]*e[i].w/100.0) 55 { 56 d[v] = d[u] - d[u]*e[i].w/100.0; 57 Q.push(HeapNode(v,d[v])); 58 } 59 } 60 } 61 } 62 63 int main() 64 { 65 //freopen("in.txt","r",stdin); 66 while(~scanf("%d",&n)) 67 { 68 tot = 0; 69 memset(head,-1,sizeof(head)); 70 for(int i=1;i<=n;i++) 71 { 72 scanf("%d",&m); 73 while(m--) 74 { 75 int u;double w; 76 scanf("%d%lf",&u,&w); 77 addEdge(i,u,w); 78 } 79 } 80 scanf("%d%d%d",&s,&t,&M); 81 dijkstra(s); 82 printf("%.2f ",M-d[t]); 83 } 84 return 0; 85 }