Advertisement
Guest User

Untitled

a guest
May 2nd, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define pii pair<int, int>
  3. #define MAXN 1001
  4. using namespace std;
  5. ifstream in("input.txt");
  6. ofstream out("output.txt");
  7. vector<vector<pii>> mat(MAXN);
  8. vector<vector<bool>> vis(MAXN);
  9. int MIN = INT_MAX, n, m;
  10. void Ddfs(int np, int na, int val, int start){
  11.     //cout << na+1 << " (" << val << ")\n";
  12.     if(val != mat[np][na].second && na == start){
  13.         MIN = min(MIN, val);
  14.         //cout << "Aggiornato min : "<< MIN << "\n";
  15.         return;
  16.     }
  17.     if(MIN <= val) return;
  18.     for(int j = 0; j < mat[na].size(); j++)
  19.         if(!vis[na][j]){
  20.             int dest;
  21.             for(int k = 0; k < mat[mat[na][j].first].size(); k++)
  22.                 if(mat[mat[na][j].first][k].first == na)
  23.                     dest = k;
  24.             vis[na][j] = true;
  25.             vis[mat[na][j].first][dest] = true;
  26.             Ddfs(na, mat[na][j].first, val + mat[na][j].second, start);
  27.             vis[na][j] = false;
  28.             vis[mat[na][j].first][dest] = false;
  29.            
  30.         }
  31. }
  32. int main(){
  33.     in >> n >> m;
  34.     for(int j = 0; j < m; j++){
  35.         int a, b, c;
  36.         in >> a >> b >> c; a--;b--;
  37.         mat[a].push_back({b, c}); vis[a].push_back(false);
  38.         mat[b].push_back({a, c}); vis[b].push_back(false);
  39.     }
  40. //  for(auto &row : mat){
  41. //      for(auto &cell : row) cout << cell.first+1 << " ";
  42. //      cout << endl;
  43. //  }
  44.     for(int i = 0; i < 1 /*n*/; i++)
  45.         for(int j = 0; j < 1/*mat[i].size()*/; j++){
  46.             //cout << i+1 << "-> " << mat[i][j].first+1 << "|\n";
  47.             int dest;
  48.             for(int k = 0; k < mat[mat[i][j].first].size(); k++)
  49.                 if(mat[mat[i][j].first][k].first == i)
  50.                     dest = k;
  51.             vis[i][j] = true;
  52.             vis[mat[i][j].first][dest] = true;
  53.             Ddfs(i, mat[i][j].first, mat[i][j].second, i);
  54.             vis[i][j] = false;
  55.             vis[mat[i][j].first][dest] = false;
  56.            
  57.         }
  58.     //cout << MIN;
  59.     out << MIN;
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement