标签:direct des i++ cto cte oid sign lowbit end
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