HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
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 <= 10 5) and C(1 <= C <= 10 3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers. The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to. Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 10 4), 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.
题目大意:有n个点m条无向边,然后每个点有一个层次,相邻层次的点可以移动(同层次不可以),花费为C,问1~n的最小花费。
思路:我试过类似于每层之间的点都连一条边(不是$O(n^2)$那种很挫的方法,不断TLE……好吧换思路……每层新建两个点a、b,i层的点到ai连一条费用为0的边,bi到i层的点连一条边,然后相邻的层分别从ai到bj连一条边,费用为C。跑Dijkstra+heap可AC。
PS:至于最初的思路为啥会TLE我就不想说了……有兴趣自己动脑吧……
代码(343MS):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 typedef pair<int, int> PII; 8 9 const int MAXN = 300010; 10 const int MAXE = MAXN * 2; 11 12 int head[MAXN]; 13 int to[MAXE], next[MAXE], cost[MAXE]; 14 int n, m, ecnt, lcnt, c; 15 16 void init() { 17 memset(head, 0, sizeof(head)); 18 ecnt = lcnt = 1; 19 } 20 21 inline void add_edge(int u, int v, int c) { 22 to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++; 23 } 24 25 int dis[MAXN]; 26 int lay[MAXN]; 27 bool vis[MAXN]; 28 29 void Dijkstra(int st, int ed) { 30 memset(dis, 0x7f, sizeof(dis)); 31 memset(vis, 0, sizeof(vis)); 32 priority_queue<PII> que; que.push(make_pair(0, st)); 33 dis[st] = 0; 34 while(!que.empty()) { 35 int u = que.top().second; que.pop(); 36 if(vis[u]) continue; 37 if(u == ed) return ; 38 vis[u] = true; 39 for(int p = head[u]; p; p = next[p]) { 40 int &v = to[p]; 41 if(dis[v] > dis[u] + cost[p]) { 42 dis[v] = dis[u] + cost[p]; 43 que.push(make_pair(-dis[v], v)); 44 } 45 } 46 } 47 } 48 49 int T; 50 51 inline int readint() { 52 char c = getchar(); 53 while(!isdigit(c)) c = getchar(); 54 int ret = 0; 55 while(isdigit(c)) ret = ret * 10 + c - '0', c = getchar(); 56 return ret; 57 } 58 59 int main() { 60 T = readint(); 61 for(int t = 1; t <= T; ++t) { 62 n = readint(), m = readint(), c = readint(); 63 init(); 64 for(int i = 1; i <= n; ++i) { 65 lay[i] = readint(); 66 add_edge(i, n + 2 * lay[i] - 1, 0); 67 add_edge(n + 2 * lay[i], i, 0); 68 } 69 for(int i = 1; i < n; ++i) { 70 add_edge(n + 2 * i - 1, n + 2 * (i + 1), c); 71 add_edge(n + 2 * (i + 1) - 1, n + 2 * i, c); 72 } 73 int u, v, w; 74 while(m--) { 75 //scanf("%d%d%d", &u, &v, &w); 76 u = readint(), v = readint(), w = readint(); 77 add_edge(u, v, w); 78 add_edge(v, u, w); 79 } 80 Dijkstra(1, n); 81 if(dis[n] == 0x7f7f7f7f) dis[n] = -1; 82 printf("Case #%d: %d\n", t, dis[n]); 83 } 84 }