Advertisement
Dynonychus

Untitled

Jan 15th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3.  
  4. using namespace std;
  5.  
  6. signed main() {
  7. ios_base::sync_with_stdio(false);
  8. cin.tie(0);
  9.  
  10. int l=0;
  11. int n=0;
  12. int a=0;
  13. string s;
  14. int i=0;
  15. int j=0;
  16. char b;
  17. char c;
  18. bool check = false;
  19. int p=0;
  20. int curr=0;
  21. int curry=0;
  22. int curryy=0;
  23. int ans = 0;
  24.  
  25. cin>>l>>n;
  26. vector<vector<int>> vec(26, vector<int>(26, 1e16));
  27. vector<vector<bool>> veco(26, vector<bool>(26, false));
  28.  
  29. cin>>s;
  30. while (n--) {
  31. cin>>b>>c>>a;
  32.  
  33. vec.at(b - 'a').at(c - 'a') = min(vec.at(b - 'a').at(c - 'a'), a);
  34. veco.at(b - 'a').at(c - 'a') = true;
  35. }
  36.  
  37. for (i=0;i<26;i++) {
  38. vec.at(i).at(i) = 0;
  39. veco.at(i).at(i) = true;
  40. }
  41.  
  42. for (a=0;a<26;a++) {
  43. for (i=0;i<26;i++) {
  44. for (j=0;j<26;j++)
  45. vec.at(i).at(j) = min(vec.at(i).at(j), (vec.at(i).at(a) + vec.at(a).at(j)));
  46. }
  47. }
  48.  
  49. for (i=0;i<26;i++) {
  50. for(j=0;j<26;j++) {
  51. if (vec.at(i).at(j) < (int)1e16)
  52. veco.at(i).at(j) = true;
  53. }
  54. }
  55.  
  56. p=(l/2);
  57. for (i=0;i<p;i++) {
  58. curr = LLONG_MAX;
  59.  
  60. curry = s[i] - 'a';
  61. curryy = s[l - i - 1] - 'a';
  62.  
  63. for (j=0;j<26;j++) {
  64. if (veco.at(curry).at(j) && veco.at(curryy).at(j)) {
  65. check = true;
  66. curr = min(curr, vec.at(curry).at(j) + vec.at(curryy).at(j));
  67. }
  68. }
  69.  
  70. if(!check) {
  71. cout<<"-1"<<endl;
  72. return 0;
  73. }
  74.  
  75. ans += curr;
  76. }
  77.  
  78. cout<<ans<<endl;
  79.  
  80. return 0;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement