杭电4725 The Shortest Path in Nya Graph(Dijkstra+堆优化)
来源:互联网 发布:王国维戏曲知乎 编辑:程序博客网 时间:2024/05/16 06:31
The Shortest Path in Nya Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2752 Accepted Submission(s): 658
Problem Description
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.
The Nya graph is an undirected graph with "layers". Each node in the graph belongs to a layer, there are N nodes in total.
You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost.
Besides, there are M extra edges, each connecting a pair of node u and v, with cost w.
Help us calculate the shortest path from node 1 to node N.
The Nya graph is an undirected graph with "layers". Each node in the graph belongs to a layer, there are N nodes in total.
You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost.
Besides, there are M extra edges, each connecting a pair of node u and v, with cost w.
Help us calculate the shortest path from node 1 to node N.
Input
The first line has a number T (T <= 20) , indicating the number of test cases.
For each test case, first line has three numbers N, M (0 <= N, M <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), which means there is an extra edge, connecting a pair of node u and v, with cost w.
For each test case, first line has three numbers N, M (0 <= N, M <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), which means there is an extra edge, connecting a pair of node u and v, with cost w.
Output
For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N.
If there are no solutions, output -1.
If there are no solutions, output -1.
Sample Input
23 3 31 3 21 2 12 3 11 3 33 3 31 3 21 2 22 3 21 3 4
Sample Output
Case #1: 2Case #2: 3
Source
2013 ACM/ICPC Asia Regional Online —— Warmup2
/*重在构图,,,注意的是点与层入度相连,到了下边层与层中间相连的时候要用与点相连的那条边与下一层相连,否则会出错。。。。Time:2015-1-8 8:44*/#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;const int MAX=1000000+10;struct qNode{ int v,c; qNode(int _v=0,int _c=0):v(_v),c(_c){}; bool operator<(const qNode &a)const{ return c>a.c; }};struct Edge{ int v,c; Edge(int _v=0,int _c=0):v(_v),c(_c){};};vector<Edge>E[MAX];void addEdge(int u,int v,int w){ E[u].push_back(Edge(v,w));}int dis[MAX];bool vis[MAX];void Dijkstra(int s,int n){ priority_queue<qNode>pq; memset(vis,0,sizeof(vis)); while(!pq.empty())pq.pop(); for(int i=1;i<=n;i++)dis[i]=INF; dis[s]=0;//vis[s]=true;此处不能标记。因为此处表示的v是入队的点s(v) pq.push(qNode(s,0)); while(!pq.empty()){ qNode tmp=pq.top();pq.pop(); int u=tmp.v; if(vis[u])continue; vis[u]=true; for(int i=0;i<E[u].size();i++){ int v=E[u][i].v; int cost=E[u][i].c; if(!vis[v]&&dis[v]>dis[u]+cost){ dis[v]=dis[u]+cost; pq.push(qNode(v,dis[v])); } } }}int main(){ int T; int nCase=1; int n,m,c; scanf("%d",&T); while(T--){ scanf("%d%d%d",&n,&m,&c); for(int i=1;i<=3*n;i++)E[i].clear();//3n int layer,u,v,w; for(int i=1;i<=n;i++){ scanf("%d",&layer); addEdge(i,n+layer*2-1,0); addEdge(n+layer*2,i,0); } for(int i=1;i<n;i++){//i层的入度表示与上边连接点的表示要对应 addEdge(n+i*2-1,n+(i+1)*2,c); addEdge(n+(i+1)*2-1,n+i*2,c); } for(int i=1;i<=m;i++){ scanf("%d%d%d",&u,&v,&w); addEdge(u,v,w); addEdge(v,u,w); } Dijkstra(1,3*n); printf("Case #%d: ",nCase++); if(dis[n]==INF)puts("-1"); else{ printf("%d\n",dis[n]); } }return 0;}
0 0
- 杭电4725 The Shortest Path in Nya Graph(Dijkstra+堆优化)
- HDU 4725 The Shortest Path in Nya Graph(堆优化dijkstra)
- HDU 4725 The Shortest Path in Nya Graph dijkstra优化
- Dijkstra+Heap-HDU-4725-The Shortest Path in Nya Graph
- HDOJ 4725 The Shortest Path in Nya Graph(Dijkstra + 优先队列)
- The Shortest Path in Nya Graph HDU4725 spfa||dijkstra
- HDU 4725 The Shortest Path in Nya Graph(SPFA)
- CUGB图论专场:K - The Shortest Path in Nya Graph(dijkstra优先队列优化+线性构图入边)
- HDOJ 4725 - The Shortest Path in Nya Graph 构图最短路(priority_queue+dijkstra)
- hdu 4725 The Shortest Path in Nya Graph dijkstra 求最短路
- hdu 4725 The Shortest Path in Nya Graph(dijkstra+优先队列)
- HDU 4725 The Shortest Path in Nya Graph——建图+dijkstra
- hdu4725 The Shortest Path in Nya Graph
- hdu4725 The Shortest Path in Nya Graph
- The Shortest Path in Nya Graph
- HDU4725-The Shortest Path in Nya Graph
- The Shortest Path in Nya Graph
- The Shortest Path in Nya Graph HDU
- 222222222222222
- BZOJ 2286 SDOI 2011 消耗战 LCA单调性
- linux的ulimit各种限制之深入分析
- C的库函数
- Failed to sync Gradle project '项目名称(module)' Error:failed to find Build Tools revision 20.0.0
- 杭电4725 The Shortest Path in Nya Graph(Dijkstra+堆优化)
- java学习day13
- 2015:中国经济史上里程碑式的一年
- 用ul、li做横向导航
- Qt Model/View 学习笔记 (一)
- Atmel megaAVR控制器 串行引导Bootloader
- 英语不懂这几句,不如回家去种地!
- windows xp中boot.ini文件修改后电脑异常的解决办法
- Qt Model/View 学习笔记 (二)