标签:push min blog 朋友 规则 小明 insert priority clear
题意:
Description
Input
Output
1 #include <bits/stdc++.h> 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 #define ll long long 6 #define lson l, m, rt<<1 7 #define rson m+1, r, rt<<1|1 8 #define st first 9 #define nd second 10 #define mp make_pair 11 #define pii pair<int, int> 12 #define pdi pair<double, int> 13 #define gg puts("gg"); 14 //#define local 15 //#define out1 16 using namespace std; 17 const int N = 6e4+10; 18 const int inf = 0x3f3f3f3f; 19 int id(int c){ 20 return c-1; 21 } 22 struct Tire{ 23 int nex[N][50], fail[N],end[N], ch[N]; 24 int root, L, tot; 25 int newnode(){ 26 memset(nex[L], -1, sizeof(nex[L])); 27 end[L] = 0; 28 ch[L] = 0; 29 return L++; 30 } 31 void init(){ 32 L = tot = 0; 33 root = newnode(); 34 } 35 void insert(int* s, int len, int tag){ 36 int now = root; 37 for(int i = 0; i < len; i++){ 38 int p = id(s[i]); 39 if(nex[now][p] == -1) 40 nex[now][p] = newnode(); 41 now = nex[now][p]; 42 ch[now] = p; 43 } 44 end[now] = tag; 45 } 46 void build(){ 47 queue<int> Q; 48 fail[root] = root; 49 for(int i = 0; i < 50; i++){ 50 int& u = nex[root][i]; 51 if(u == -1) 52 u = root; 53 else{ 54 fail[u] = root; 55 Q.push(u); 56 } 57 } 58 while(!Q.empty()){ 59 int now = Q.front(); 60 Q.pop(); 61 for(int i = 0; i < 50; i++){ 62 int& u = nex[now][i]; 63 if(u == -1) 64 u = nex[ fail[now] ][i]; 65 else{ 66 fail[u] = nex[ fail[now] ][i]; 67 end[u] |= end[ fail[u] ]; 68 Q.push(u); 69 } 70 } 71 } 72 } 73 }; 74 Tire ac; 75 ll x[55], y[55]; 76 double w[55][55]; 77 int s[10]; 78 vector< pdi > ve[505]; 79 priority_queue< pdi, vector< pdi >, greater< pdi > >Q; 80 double d[505]; 81 double dijkstra(int S){ 82 int i, x; 83 for(i = 0; i < 505; i++) d[i] = 1e30; 84 Q.push(mp(d[S]=0,S)); 85 while(!Q.empty()){ 86 pdi t = Q.top(); Q.pop(); 87 if(d[x = t.nd] < t.st) continue; 88 for(i = 0; i < ve[x].size(); i++) 89 if(d[x]+ve[x][i].st < d[ve[x][i].nd]){ 90 d[ve[x][i].nd] = d[x]+ve[x][i].st; 91 Q.push( mp(d[ve[x][i].nd], ve[x][i].nd) ); 92 } 93 } 94 } 95 int main(){ 96 int n, m, ca = 1; 97 while(~scanf("%d%d", &n, &m), n+m){ 98 for(int i = 1; i <= n; i++) 99 scanf("%lld%lld", x+i, y+i); 100 for(int i = 0; i <= n; i++) 101 for(int j = 0; j <= n; j++) w[i][j] = 1e30; 102 for(int i = 1; i <= n; i++) 103 for(int j = i+1; j <= n; j++) 104 w[i][j] = sqrt( 1.0*(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]) ); 105 106 ac.init(); 107 for(int i = 1; i <= n; i++){ 108 s[0] = i; 109 ac.insert(s, 1, 0); 110 } 111 112 for(int i = 1; i <= m; i++){ 113 int k; scanf("%d", &k); 114 for(int j = 0; j < k; j++) 115 scanf("%d", s+j); 116 ac.insert(s, k, 1); 117 } 118 ac.build(); 119 for(int i = 0; i < ac.L; i++) ve[i].clear(); 120 for(int i = 1; i < ac.L; i++){ 121 int u = ac.ch[i]+1; 122 for(int j = u+1; j <= n; j++) { 123 int to = ac.nex[i][id(j)]; 124 if(!ac.end[to]) ve[i].push_back( mp(w[u][j], to) ); 125 } 126 } 127 128 dijkstra(ac.nex[0][id(1)]); 129 double ans = 1e30; 130 for(int i = 1; i < ac.L; i++){ 131 if(ac.ch[i] == id(n)) 132 ans = min(ans, d[i]); 133 } 134 if(ans < 1e25) 135 printf("%.2f\n", ans); 136 else 137 puts("Can not be reached!"); 138 } 139 return 0; 140 }
后记:发现debug能力实在太弱了...调半天没看出来哪错了...代码能力不够啊!!!
标签:push min blog 朋友 规则 小明 insert priority clear
原文地址:http://www.cnblogs.com/dirge/p/6012301.html