码迷,mamicode.com
首页 > 其他好文 > 详细

The Shortest Path in Nya Graph HDU - 4725

时间:2018-08-06 16:28:31      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:direct   des   i++   cto   cte   oid   sign   lowbit   end   

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个点,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

标签:direct   des   i++   cto   cte   oid   sign   lowbit   end   

原文地址:https://www.cnblogs.com/qldabiaoge/p/9430777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!