标签:ems memory 腾讯 sam 2.0 ble memset key php
Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2024 Accepted Submission(s): 556
1 #include<iostream> 2 #include<cstring> 3 #include<queue> 4 #include<cstdio> 5 #include<stack> 6 #include<set> 7 #include<map> 8 #include<cmath> 9 #include<ctime> 10 #include<time.h> 11 #include<algorithm> 12 using namespace std; 13 #define mp make_pair 14 #define pb push_back 15 #define debug puts("debug") 16 #define LL long long 17 #define pii pair<int,int> 18 #define eps 1e-10 19 double inf=1e20; 20 int N; 21 struct Point{ 22 double x,y; 23 }P[55]; 24 double dis(Point A,Point B){ 25 return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)); 26 } 27 double e[55][55]; 28 struct ach{ 29 int ch[1020][55],fail[1020],cnt[1020],tot; 30 double f[55][1020]; 31 void init(){ 32 tot=0; 33 newnode(); 34 } 35 36 int newnode(){ 37 memset(ch[tot],-1,sizeof(ch[tot])); 38 fail[tot]=-1; 39 cnt[tot]=0; 40 return tot++; 41 } 42 43 void insert(int a[],int n){ 44 int u=0; 45 for(int i=1;i<=n;++i){ 46 if(ch[u][a[i]]==-1) ch[u][a[i]]=newnode(); 47 u=ch[u][a[i]]; 48 } 49 cnt[u]=1; 50 } 51 void getfail(){ 52 queue<int>q; 53 fail[0]=0; 54 for(int i=1;i<=N;++i){ 55 if(ch[0][i]==-1){ 56 ch[0][i]=0; 57 } 58 else{ 59 fail[ch[0][i]]=0; 60 q.push(ch[0][i]); 61 } 62 } 63 while(!q.empty()){ 64 int u=q.front(); 65 q.pop(); 66 cnt[u]|=cnt[fail[u]]; 67 for(int i=1;i<=N;++i){ 68 if(ch[u][i]==-1){ 69 ch[u][i]=ch[fail[u]][i]; 70 } 71 else{ 72 fail[ch[u][i]]=ch[fail[u]][i]; 73 q.push(ch[u][i]); 74 } 75 } 76 } 77 } 78 79 void solve(){ 80 for(int i=1;i<=N;++i){ 81 for(int j=0;j<tot;++j){ 82 f[i][j]=inf; 83 } 84 } 85 f[1][ch[0][1]]=0; 86 for(int i=1;i<=N;++i){ 87 for(int j=0;j<tot;++j){ 88 if(f[i][j]<inf){ 89 for(int k=i+1;k<=N;++k){ 90 if(cnt[ch[j][k]]==0){ 91 f[k][ch[j][k]]=min(f[k][ch[j][k]],f[i][j]+e[i][k]); 92 } 93 } 94 } 95 } 96 } 97 98 double ans=inf; 99 for(int j=0;j<tot;++j) 100 ans=min(ans,f[N][j]); 101 if(ans==inf) puts("Can not be reached!"); 102 else printf("%.2f\n",ans); 103 } 104 }a; 105 int main() 106 { 107 int n,m,i,j,k; 108 int c[15]; 109 while(cin>>n>>m){N=n; 110 a.init(); 111 if(n==0&&m==0) break; 112 for(i=1;i<=n;++i) {e[i][i]=0; 113 scanf("%lf%lf",&P[i].x,&P[i].y); 114 for(j=1;j<i;++j){ 115 e[i][j]=e[j][i]=dis(P[i],P[j]); 116 } 117 } 118 for(i=1;i<=m;++i){ 119 scanf("%d",&k); 120 for(j=1;j<=k;++j){ 121 scanf("%d",c+j); 122 } 123 a.insert(c,k); 124 } 125 126 a.getfail(); 127 a.solve(); 128 } 129 return 0; 130 }
标签:ems memory 腾讯 sam 2.0 ble memset key php
原文地址:https://www.cnblogs.com/zzqc/p/8986500.html