Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n;
- vector<vector<int>>g(0);
- vector<bool>used(0);
- pair<int, string> dfs(int st, int nd){
- used[st] = true;
- if(st == nd){
- used[st] = false;
- return {0 , ""};
- }
- int c = g[st][nd];
- char x = char('A'+st);
- char y = char('A'+nd);
- char z = char('0' + g[st][nd]);
- string s = "";
- s+=y;
- s+=x;
- s+=z;
- for(int i = 0; i < n; i++){
- if(used[i])
- continue;
- auto par = dfs(i, nd);
- if( par.first + g[st][i] < c){
- c = par.first + g[st][i];
- s = par.second + "--" + char('A'+i) + x + char('0' + g[st][i]);
- }
- }
- used[st] = false;
- return {c, s};
- }
- int main() {
- cin >> n;
- g.resize(n, vector<int>(n, 1000000));
- used.resize(n, false);
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n; j++){
- int c;
- cin >> c;
- if(c > -1)
- g[i][j] = c;
- }
- }
- int a,b, c;
- cin >> a >> b >> c;
- auto par1 = dfs(b, a);
- auto par2 = dfs(c,b);
- cout << par1.first + par2.first << endl;
- cout << par1.second + "--" + par2.second << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement