HDU 4725 The Shortest Path in Nya Graph dijkstra优化
来源:互联网 发布:mac虚拟机蓝屏 编辑:程序博客网 时间:2024/06/05 01:57
The Shortest Path in Nya Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2554 Accepted Submission(s): 624
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
设虚点建图,然后优化下dijkstra就可以了
#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,n) for(int i=0;i<(n);++i)#define FOR(i,l,h) for(int i=(l);i<=(h);++i)#define DWN(i,h,l) for(int i=(h);i>=(l);--i)#define CLR(vis) memset(vis,0,sizeof(vis))#define MST(vis,pos) memset(vis,pos,sizeof(vis))#define MAX3(a,b,c) max(a,max(b,c))#define MAX4(a,b,c,d) max(max(a,b),max(c,d))#define MIN3(a,b,c) min(a,min(b,c))#define MIN4(a,b,c,d) min(min(a,b),min(c,d))#define PI acos(-1.0)#define INF 0x7FFFFFFF#define LINF 1000000000000000000LL#define eps 1e-8typedef long long ll;const int maxn=1e5+100;struct Edge{ int to,w; int next;}e[maxn*6];struct HeapNode{ int w,u; bool operator < (const HeapNode &a) const { return w>a.w; }};int head[maxn*3],dis[maxn*3],edge;int vis[maxn*3];int n,m,c;vector<int> pos[maxn];void init(){ MST(head,-1); edge=0; REP(i,n+1) pos[i].clear();}void addedge(int u,int v,int w){ e[edge].to=v,e[edge].w=w,e[edge].next=head[u],head[u]=edge++;}void dijkstra(int s){ priority_queue<HeapNode> q; CLR(vis); for(int i=0;i<=3*n;i++) dis[i]=INF; dis[s]=0; HeapNode hp; hp.u=1,hp.w=0; q.push(hp); while(!q.empty()) { hp=q.top();q.pop(); int u=hp.u; if(vis[u]) continue; vis[u]=true; for(int k=head[u];k!=-1;k=e[k].next) { int v=e[k].to; if(dis[v]>dis[u]+e[k].w) { dis[v]=dis[u]+e[k].w; hp.w=dis[v]; hp.u=v; q.push(hp); } } }}int check(int x){ if(x==INF) return -1; return x;}int main(){ int T; int cas=1; cin>>T; while(T--) { scanf("%d%d%d",&n,&m,&c); init(); int x; int max_layer=-1; FOR(i,1,n) { scanf("%d",&x); max_layer=max(max_layer,x); pos[x].PB(i); } REP(i,max_layer) { REP(j,pos[i].size()) { addedge(pos[i][j],i+n,c); addedge(i+n+n,pos[i][j],0); } REP(j,pos[i+1].size()) { addedge(i+n,pos[i+1][j],0); addedge(pos[i+1][j],i+n+n,c); } } int u,v,w; REP(i,m) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } dijkstra(1); printf("Case #%d: %d\n",cas++,check(dis[n])); } return 0;}
#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,n) for(int i=0;i<(n);++i)#define FOR(i,l,h) for(int i=(l);i<=(h);++i)#define DWN(i,h,l) for(int i=(h);i>=(l);--i)#define CLR(vis) memset(vis,0,sizeof(vis))#define MST(vis,pos) memset(vis,pos,sizeof(vis))#define MAX3(a,b,c) max(a,max(b,c))#define MAX4(a,b,c,d) max(max(a,b),max(c,d))#define MIN3(a,b,c) min(a,min(b,c))#define MIN4(a,b,c,d) min(min(a,b),min(c,d))#define PI acos(-1.0)#define INF 0x7FFFFFFF#define LINF 1000000000000000000LL#define eps 1e-8typedef long long ll;const int maxn=1e5+100;struct node{ int to,w; node(){} node(int v,int c){to=v;w=c;} bool operator < (const node & a) const { if(w==a.w) return to<a.to; else return w>a.w; }};vector<node> e[maxn*6];int dis[maxn*3];int n,m,c;vector<int> p[maxn*3];void dijkstra(int s){ int i; for(i=0;i<=3*n;i++) dis[i]=INF; dis[s]=0; priority_queue<node> q; q.push(node(s,dis[s])); while(!q.empty()) { node u=q.top(); q.pop(); int v=u.to; REP(i,e[v].size()) { node temp=e[v][i]; if(dis[temp.to]>dis[v]+temp.w) { dis[temp.to]=dis[v]+temp.w; q.push(node(temp.to,dis[temp.to])); } } }}int check(int x){ if(x==INF) return -1; return x;}int main(){ int T; int cas=1; cin>>T; while(T--) { scanf("%d%d%d",&n,&m,&c); REP(i,n*3+10) { p[i].clear(); e[i].clear(); } int x; int max_layer=-1; FOR(i,1,n) { scanf("%d",&x); max_layer=max(x,max_layer); p[x].PB(i); } REP(i,max_layer) { REP(j,p[i].size()) { e[p[i][j]].PB(node(i+n,c)); e[i+n+n].PB(node(p[i][j],0)); } REP(j,p[i+1].size()) { e[i+n].PB(node(p[i+1][j],0)); e[p[i+1][j]].PB(node(i+n+n,c)); } } int u,v,w; REP(i,m) { scanf("%d%d%d",&u,&v,&w); e[u].PB(node(v,w)); e[v].PB(node(u,w)); } dijkstra(1); printf("Case #%d: %d\n",cas++,check(dis[n])); } return 0;}
0 0
- HDU 4725 The Shortest Path in Nya Graph dijkstra优化
- Dijkstra+Heap-HDU-4725-The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph(堆优化dijkstra)
- The Shortest Path in Nya Graph HDU
- The Shortest Path in Nya Graph 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+优先队列)
- HDU 4725 The Shortest Path in Nya Graph——建图+dijkstra
- hdu 4725 The Shortest Path in Nya Graph
- HDU-4725-The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph
- hdu 4725 The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph priority_queue + spfa
- HDU 4725 The Shortest Path in Nya Graph
- hdu-4725-The Shortest Path in Nya Graph-层次网络
- 【HDU】4725 The Shortest Path in Nya Graph 最短路
- oracle 管道化表函数(Pipelined Table)
- ArcGIS教程:栅格表面的分辨率
- C源程序的 编译 链接 main()的调用粗程
- 继承总结
- poj 2965 水 = =
- HDU 4725 The Shortest Path in Nya Graph dijkstra优化
- 标准正态分布面积(面积=比例=概率,不仅限于正态分布)
- 提高tomcat的并发能力
- java--四则运算
- jQuery.fn的作用是什么
- Preference中比较重要的监听点击事件方法
- hadoop学习工作总结(六)之生成hive的一个存储过程
- Java中的equals()和==
- 设置一个层同时水平垂直居中