Source Code
	#include <iostream>
	#include <string>
	#include <math.h>
	#include <set>
	#include <map>
	#include <queue>
	#include <utility>
	#include <algorithm>
	#include <stack>
	#include <bitset>
	#define pb push_back
	using namespace std;
 

	int main(){
	    int t = 1;
	    //cin >> t;

	    while(t--){
	    	int n, m;
	    	cin >> n >> m;

	    	vector<pair<pair<int,int>, int>>v;
	    	map<int,map<char,set<int>>>mp;



	    	for(int i = 0 ; i < m ; i++){
	    		int u, vv, w;
	    		scanf("%d%d%d",&u, &vv, &w);

	    		v.pb(make_pair(make_pair(u, vv), w));
	    	}

	    	string s;
	    	cin >> s;

	    	bool f = 0;
	    	long long ans = INT_MAX;

	    	for(int i = 0 ; i < m ; i++){
	    		mp[v[i].first.first][s[v[i].first.second - 1] - 'a'].insert(v[i].second);
	    		mp[v[i].first.second][s[v[i].first.first - 1] - 'a'].insert(v[i].second);
	    	}

	    	for(int i = 1 ; i <= n ; i++){
	    		if(mp[i][s[i - 1] - 'a'].size() > 0){
	    			ans = min(ans, 1ll * (*mp[i][s[i - 1] - 'a'].begin()));
	    			f = 1;
	    		}
	    		for(int j = 0 ; j < 26 ; j++){
	    			long long sum = 0;
	    			int k = 0;
	    			for(auto it = mp[i][j].begin() ; it != mp[i][j].end() && k < 2 ; it++, k++){
	    				sum += (*it);
	    			}
	    			if(mp[i][j].size() > 1){
	    				ans = min(ans, sum);
	    				f = 1;
	    			}
	    		}
	    	}

	    	if(f)
	    	cout<<ans<<endl;
	    	else puts("-1");
	    }
	}
 


Copy
Selen With a C Abdullahbitar
GNU G++17
0 ms
0 KB
Compilation Error