The Shortest Path in Nya Graph HDU - 4725
阿新 • • 發佈:2018-08-06
move *** comment class tle pre nod %d side 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.
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
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.
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
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 Output Case #1: 2 Case #2: 3 n個點,m條邊,以及相鄰層之間移動的代價c,給出每個點所在的層數,以及m條邊, 每條邊有u,v,c,表示從節點u到v(無向),並且移動的代價 c , 問說從 1 到 n 的代價最小是多少。 將層抽象出來成為n個點(對應編號依次為n+1 ~ n+n), 然後層與層建邊,點與點建邊 ,層與在該層上的點建邊 (邊長為0),點與相鄰層建邊 (邊長為c)。
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <cmath> 5 #include <algorithm> 6 #include <set> 7 #include <iostream> 8 #include <map> 9 #include <stack> 10 #include <string> 11 #include <vector> 12 #define pi acos(-1.0) 13 #define eps 1e-6 14 #define fi first 15 #define se second 16 #define lson l,m,rt<<1 17 #define rson m+1,r,rt<<1|1 18 #define bug printf("******\n") 19 #define mem(a,b) memset(a,b,sizeof(a)) 20 #define fuck(x) cout<<"["<<x<<"]"<<endl 21 #define sf(n) scanf("%d", &n) 22 #define sff(a,b) scanf("%d %d", &a, &b) 23 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) 24 #define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d) 25 #define pf printf 26 #define FRE(i,a,b) for(i = a; i <= b; i++) 27 #define FREE(i,a,b) for(i = a; i >= b; i--) 28 #define FRL(i,a,b) for(i = a; i < b; i++) 29 #define FRLL(i,a,b) for(i = a; i > b; i--) 30 #define FIN freopen("DATA.txt","r",stdin) 31 #define gcd(a,b) __gcd(a,b) 32 #define lowbit(x) x&-x 33 #pragma comment (linker,"/STACK:102400000,102400000") 34 using namespace std; 35 typedef long long LL; 36 typedef unsigned long long ULL; 37 const int INF = 0x7fffffff; 38 const int maxn = 2e5 + 10; 39 int cas = 1, t, n, m, c, lv[maxn], have[maxn]; 40 int tot, head[maxn], d[maxn], vis[maxn]; 41 struct Edge { 42 int v, w, nxt; 43 } edge[maxn*10]; 44 struct node { 45 int v, d; 46 node(int v, int d) : v(v), d(d) {} 47 bool operator < (const node & a) const { 48 return d > a.d; 49 } 50 }; 51 void init() { 52 tot = 0; 53 mem(head, -1); 54 } 55 void add(int u, int v, int w) { 56 edge[tot].v = v; 57 edge[tot].w = w; 58 edge[tot].nxt = head[u]; 59 head[u] = tot++; 60 } 61 int dijkstra(int st, int ed) { 62 mem(vis, 0); 63 for (int i = 0 ; i < maxn ; i++) d[i] = INF; 64 priority_queue<node>q; 65 d[st] = 0; 66 q.push(node(st, d[st])); 67 while(!q.empty()) { 68 node temp = q.top(); 69 q.pop(); 70 int u = temp.v; 71 if (vis[u]) continue; 72 vis[u] = 1; 73 for (int i = head[u] ; ~i ; i = edge[i].nxt) { 74 int v = edge[i].v, w = edge[i].w; 75 if (d[v] > d[u] + w && !vis[v]) { 76 d[v] = d[u] + w; 77 q.push(node(v, d[v])); 78 } 79 } 80 } 81 return d[ed]; 82 } 83 int main() { 84 sf(t); 85 while(t--) { 86 sfff(n, m, c); 87 init(); 88 mem(have, 0); 89 mem(lv,0); 90 for (int i = 1 ; i <= n ; i++) { 91 sf(lv[i]); 92 have[lv[i]] = 1; 93 } 94 for (int i = 1 ; i <= m ; i++) { 95 int u, v, w; 96 sfff(u, v, w); 97 add(u, v, w); 98 add(v, u, w); 99 } 100 for (int i = 1 ; i < n ; i++) 101 if (have[i] && have[i + 1]) { 102 add(n + i, n + i + 1, c); 103 add(n + i + 1, n + i, c); 104 } 105 for (int i = 1 ; i <= n ; i++) { 106 add(lv[i] + n, i, 0); 107 if (lv[i] > 1) add(i, lv[i] + n - 1, c); 108 if (lv[i] < n) add(i, lv[i] + n + 1, c); 109 } 110 int ans = dijkstra(1, n); 111 if (ans == INF) printf("Case #%d: -1\n", cas++); 112 else printf("Case #%d: %d\n", cas++, ans); 113 } 114 return 0; 115 }
The Shortest Path in Nya Graph HDU - 4725