【最短路】【负圈可达点】【用while(!Q.empty())】【优先队列优化】【dij】
来源:互联网 发布:ios 数据库开发 编辑:程序博客网 时间:2024/06/10 07:26
Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked in congestion. In order to convince people avoid shortest routes, and hence the crowded roads, to reach destination, the city authority has made a new plan. Each junction of the city is marked with a positive integer (≤ 20) denoting the busyness of the junction. Whenever someone goes from one junction (the source junction) to another (the destination junction), the city authority gets the amount (busyness of destination - busyness of source)3 (that means the cube of the difference) from the traveler. The authority has appointed you to find out the minimum total amount that can be earned when someone intelligent goes from a certain junction (the zero point) to several others.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case contains a blank line and an integer n (1 < n ≤ 200) denoting the number of junctions. The next line contains n integers denoting the busyness of the junctions from 1 to nrespectively. The next line contains an integer m, the number of roads in the city. Each of the next m lines (one for each road) contains two junction-numbers (source, destination) that the corresponding road connects (all roads are unidirectional). The next line contains the integer q, the number of queries. The next q lines each contain a destination junction-number. There can be at most one direct road from a junction to another junction.
Output
For each case, print the case number in a single line. Then print q lines, one for each query, each containing the minimum total earning when one travels from junction 1 (the zero point) to the given junction. However, for the queries that gives total earning less than 3, or if the destination is not reachable from the zero point, then print a '?'.
Sample Input
Output for Sample Input
2
5
6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
5
2
10 10
1
1 2
1
2
Case 1:
3
4
Case 2:
?
lightoj 1074
#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define mp push_backint T,n,m,q;const int maxn = 210;int H[maxn];struct Edge{int v,c,next;}edge[maxn*maxn];struct Qnode{int idx,dis;Qnode(){};Qnode(int _i,int _d) : idx(_i),dis(_d){};bool operator< (const Qnode &x) const{return dis > x.dis;}};int val[maxn];int d[maxn];bool ok[maxn];bool cir[maxn];int top;int dis(int f,int t){return (t-f)*(t-f)*(t-f);}void addedge(int u,int v,int c){edge[top].v = v;edge[top].c = c;edge[top].next = H[u];H[u] = top++;}void dfs(int u){cir[u] = true;for(int ei=H[u];ei!=-1;ei=edge[ei].next){Edge e = edge[ei];if(!cir[e.v])dfs(e.v);}}void dij(int st){for(int i=1;i<=n;i++){d[i] = 0x3f3f3f3f;ok[i] = false;cir[i] = false;} d[st] = 0;priority_queue<Qnode> pq;pq.push(Qnode(st,0));while(!pq.empty()){Qnode cur = pq.top();pq.pop();ok[cur.idx] = true;for(int ei=H[cur.idx];ei!=-1;ei=edge[ei].next){Edge e = edge[ei];if(cir[e.v]) continue;if(d[e.v] > cur.dis + e.c){if(ok[e.v]){dfs(e.v);}else{d[e.v] = cur.dis + e.c;pq.push(Qnode(e.v,d[e.v]));}}}}}int main(){int C = 1; scanf("%d",&T);while(T--){top = 0;memset(H,-1,sizeof(H));scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&val[i]);}scanf("%d",&m);for(int i=0;i<m;i++){int u,v;scanf("%d%d",&u,&v);int c = dis(val[u],val[v]);addedge(u,v,c);}dij(1);scanf("%d",&q);printf("Case %d:\n",C++);while(q--){int v;scanf("%d",&v);if(cir[v] || d[v] < 3 || d[v] == 0x3f3f3f3f){puts("?");}else{printf("%d\n",d[v]);}}} return 0;}
- 【最短路】【负圈可达点】【用while(!Q.empty())】【优先队列优化】【dij】
- poj 3013 Big Christmas Tree (dij+优先队列优化 求最短路)
- HDOJ 2544 最短路(DIJ+优先队列)
- poj --- 1724 最短路变形 【Dij + 优先队列 】
- 最短路&&优先队列优化->HDU2544
- HDU5521 双向最短路+优先队列优化
- 最短路的优先队列的优化
- POJ 3159 最短路dij优先队列模板 差分约束
- hdu 2066最短路 dijkstra 及其优先队列优化
- [最短路]使用优先队列优化的Dijkstra算法
- 【原创】最短路模板 Floyd,优先队列优化dijkstra,SPFA
- POJ1724 ROADS(深搜DFS,最短路,dijkstra,用优先队列优化)
- dij+优先队列POJ_3662
- dij +优先队列
- zoj2797_最短路dij
- Dij二级最短路
- dij最短路 变形
- POJ1062->最短路Dij
- hdoj 1213 How Many Tables【并查集】
- 06-图6. 公路村村通(30)--最小生成树-prime-kruskal
- Dijkstra算法
- CAP定理小议
- 如何判断一个数的二进制有多少个1
- 【最短路】【负圈可达点】【用while(!Q.empty())】【优先队列优化】【dij】
- PAT 1063. Set Similarity (25)
- Caliburn Micro Part 3: More About Events and Parameters
- 线段树的一系列应用
- Think in Java之构造器的真正调用顺序
- webService之hello级别中的注意事项
- andrioid EditText 光标设置为文本的末尾
- Android平台aac谷歌软解框架和流程、解码库学习
- mysql数据库学习笔记 -- Mysql解决