Idea: The problem is too long, and very simple, this will not say.
Idea: Originally think of T, write the suffix array, or add heap optimization What, the result is directly a.
CODE:
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define MAX 50010#define maxe 5000010#define BASE 2333#define INF 0x3f3f3f3fusing namespace std; unsigned long long power[20]; struct complex{char s[12]; int _id,num[10]; unsigned long long hash,_hash,sorting; BOOL operator < (const Complex &a) Const {return sorting < a.sorting; } void Read (int p) {_id = P; scanf ("%s", S + 1); hash = _hash = 0; for (int i = 1; i <=; ++i) {hash = hash * BASE + s[i]; ++num[s[i]-' 0 ']; } for (int i = 0; I <= 9; ++i) _hash = _hash * BASE + num[i]; } int LCP (const Complex &a) const {for (int i = 1;; ++i) if (s[i]! = A.s[i]) return I-1; return 0; }}src[max]; int Cnt;int cost[10]; void Pretreatment () {power[0] = 1; for (int i = 1; i <=; ++i) power[i] = power[i-1] * BASE;} int Head[max],total;int Next[maxe],aim[maxe],length[maxe]; inline void Add (int x,int y,int len) {next[++total] = head[x]; Aim[total] = y; Length[total] = len; HEAD[X] = total;} inline void Addedge (int l,int R) {for (int i = l; I <= R; ++i) for (int j = i + 1; j <= R; ++j) { int LCP = Src[i]. LCP (Src[j]); ADD (SRC[I]._ID,SRC[J]._ID,COST[LCP]); ADD (SRC[J]._ID,SRC[I]._ID,COST[LCP]); }}: inline void Addedge (int l,int r,bool flag) {for (int i = l; I <= R; ++i) for (int j = i + 1; j <= R; ++j) if (Src[i]._hash = = src[j]._hash) {int LCP = Src[i]. LCP (Src[j]); ADD (SRC[I]._ID,SRC[J]._ID,COST[LCP]); ADD (SRC[J]._ID,SRC[I]._ID,COST[LCP]); }} int Spfa () {static int f[max]; static bool V[max]; static queue<int> Q; memset (F,0x3f,sizeof (f)); F[1] = 0; Q.push (1); while (!q.empty ()) {int x = Q.front (); Q.pop (); V[X] = false; for (int i = head[x]; i; i = Next[i]) if (F[aim[i]] > F[x] + length[i]) {F[aim[i]] = f[x] + le Ngth[i]; if (!v[aim[i]]) {V[aim[i]] = true; Q.push (Aim[i]); }}} return f[cnt] = = INF? -1:F[CNT];} int main () {pretreatment (); CIN >> CNT; for (int i = 0; I <= 9; ++i) scanf ("%d", &cost[i]); for (int i = 1; I <= cnt; ++i) Src[i]. Read (i); for (int i = 1; I <=, ++i) {for (int j = 1; j <= cnt; ++j) src[j].sorting = src[j].hash-power[ 10-I] * Src[j].s[i]; Sort (src + 1,src + cnt + 1); unsigned long long now = src[1].sorting; int last = 1; for (int j = 1; j <= cnt; ++j) if (src[j].sorting! = Now) {Addedge (last,j-1); last = j; now = src[j].sorting; } Addedge (last, CNT); } for (int i = 1; I <=, ++i) for (int j = i + 1; J <=, ++j) {for (int k = 1; k <= CNT; + +k) src[k].sorting = src[k].hash-power[10-i] * Src[k].s[i]-power[10-j] * SRC[K].S[J]; Sort (src + 1,src + cnt + 1); unsigned long long now = src[1].sorting; int last = 1; for (int k = 1; k <= cnt; ++k) if (src[k].sorting! = Now) {Addedge (last,k-1,false); last = k; now = src[k].sorting; } addedge (Last,cnt,false); } cout << SPFA () << Endl; return 0;}
Bzoj 3045 Telephone Line violence