HDU 4725 The Shortest Path in Nya Graph (最短路拆点建图)
来源:互联网 发布:数据库服务器选择 编辑:程序博客网 时间:2024/05/29 13:55
http://acm.hdu.edu.cn/showproblem.php?pid=4725
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.
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.
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.
Sample Input
2
3 3 3
1 3 2
1 2 1
2 3 1
1 3 3
3 3 3
1 3 2
1 2 2
2 3 2
1 3 4
Sample Output
Case #1: 2
Case #2: 3
最短路。
主要是建图。
N个点,然后有N层,要假如2*N个点。
总共是3*N个点。
点1~N就是对应的实际的点1~N. 要求的就是1到N的最短路。
然后点N+1 ~ 3*N 是N层拆出出来的点。
到第i层的入边是点N+2*i-1, 出边从点N+2*i 出来。(1<= i <= N)
N + 2*i 到 N + 2*(i+1)-1 加边长度为C. 表示从第i层到第i+1层。
N + 2*(i+1) 到 N + 2*i - 1 加边长度为C,表示第i+1层到第i层。
如果点i属于第u层,那么加边 N + 2*u -1 -> i i-> N + 2*u 长度都为0
然后用优先队列优化的Dijkstra就可以搞出最短路了
保证边建对。。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace std;const int N = 1e5 + 100;const int MAXN = 3*N;const int INF = 0x3f3f3f3f;struct Edge{ int v,w; Edge(int v,int w):v(v),w(w){}};vector<Edge> E[MAXN]; int dis[MAXN]; bool vis[MAXN];void add(int u,int v,int w){ E[u].push_back(Edge(v,w));};struct Node{ int v,w; Node(){} Node(int v,int w):v(v),w(w){} friend bool operator < (const Node &a,const Node &b) { return a.w > b.w; }};int dijstra(int n){ for(int i=1;i<=3*n;i++) dis[i] = INF; memset(vis,false,sizeof(vis)); priority_queue<Node>que; que.push(Node(1,0)); dis[1] = 0; while(!que.empty()) { Node now = que.top(); que.pop(); int u = now.v; if(vis[u]) continue; vis[u] = true; for(int i=0;i<E[u].size();i++) { int v = E[u][i].v; int c = E[u][i].w; if(!vis[v] && dis[v] > dis[u] + c) { dis[v] = dis[u] + c; que.push(Node(v,dis[v])); } } } if(dis[n] == INF) dis[n] = -1; return dis[n];}int main(){ int T; scanf("%d",&T); for(int cas = 1; cas<=T ;cas++) { int n,m,c; scanf("%d%d%d",&n,&m,&c); for(int i=1;i<=3*n;i++) E[i].clear(); for(int i=1;i<=n;i++) { int x; scanf("%d",&x); add(n+2*x-1,i,0); add(i,n+2*x,0); } for(int i=1;i<n;i++) { add( n+2*(i+1),n+2*i-1,c); add( n+2*i,n+2*(i+1)-1,c); } while(m--) { int u,v,w; scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } printf("Case #%d: %d\n",cas,dijstra(n)); } return 0;}
- 【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 (最短路拆点建图)
- HDU 4725 The Shortest Path in Nya Graph(图论+最短路)
- HDU 4725 The Shortest Path in Nya Graph(最短路 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(拆点+最短路)
- hdu 4725 The Shortest Path in Nya Graph(最短路)
- hdu 4725 The Shortest Path in Nya Graph(堆+dij,最短路,5级)
- HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】
- hdu 4725 The Shortest Path in Nya Graph dijkstra 求最短路
- HDU 4725-J - The Shortest Path in Nya Graph-增点建图-层次网络-最短路
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
- hdu4725(The Shortest Path in Nya Graph)最短路
- IOS开发总结之文件下载(大文件下载)
- HDOJ 3911 Black And White
- mini2440NFS启动,VFS: Unable to mount root fs via NFS, trying floppy.
- <LeetCode OJ> 349 / 350 Intersection of Two Arrays(I / II)
- px4 middleware and architecture
- HDU 4725 The Shortest Path in Nya Graph (最短路拆点建图)
- 239. Sliding Window Maximum
- 数据流重定向
- Java实现简单数据结构之二叉树结构排序 binary tree
- C语言实现的RSA算法程序(使用GMP)
- 态度
- JSP中文乱码问题
- 爬取网页数据 编码转换问题
- 设计模式之观察者模式简单运用