Guest User

Untitled

a guest
Jul 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. // Paste me into the FileEdit configuration dialog
  2.  
  3. #include <cmath>
  4. #include <ctime>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cstdlib>
  12. using namespace std;
  13.  
  14. class SafeReturn {
  15. public:
  16.  
  17.  
  18.     #define trav(it, v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
  19.     #define rep(i, a, b) for(int i = (a); i < int(b); ++i)
  20.  
  21.     vector<int> match;
  22.     vector<bool> seen;
  23.     template<class G>
  24.     bool find(int j, G &g) {
  25.         if (match[j] == -1) return true;
  26.         seen[j] = true; int di = match[j];
  27.         trav(e, g[di])
  28.         if (!seen[*e] && find(*e, g)) {
  29.             match[*e] = di;
  30.             match[j] = -1;
  31.             return true;
  32.         }
  33.         return false;
  34.     }
  35.     template<class G>
  36.     int dfs_matching(G &g, int n, int m) {
  37.         match.assign(m, -1);
  38.         rep(i,0,n) {
  39.             seen.assign(m, false);
  40.             trav(j,g[i])
  41.             if (find(*j, g)) {
  42.                 match[*j] = i;
  43.                 break;
  44.             }
  45.         }
  46.         return m - count(match.begin(), match.end(), -1);
  47.     }
  48.  
  49.     int minRisk( int N, vector <string> streets ) {
  50.         int T = streets.size();
  51.         int dist[T][T];
  52.         for(int i = 0; i<T; ++i)
  53.             for(int j = 0; j<T; ++j)
  54.                 dist[i][j] = 1000000;
  55.         for(int i = 0; i<T; ++i){
  56.             for(int j = 0; j<T; ++j){
  57.                 if(streets[i][j] != '-'){
  58.                     dist[i][j] = streets[i][j]-'0';
  59.                 }
  60.             }
  61.         }
  62.         for(int k = 0; k<T; ++k)
  63.             for(int i = 0; i<T; ++i)
  64.                 for(int j = 0; j<T; ++j){
  65.                     dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);
  66.                 }
  67.                 vector<vector<int> > leftPartition(N);
  68.             for(int i = 1; i<=N; ++i)
  69.                 for(int j = 1; j<=N; ++j){
  70.                     if(i != j && dist[0][i]+dist[i][j] == dist[0][j]){
  71.                        
  72.                         //j kan följa i utan konsekvenser!
  73.                         leftPartition[j-1].push_back(i-1);
  74.                     }
  75.                 }
  76.                 return N-dfs_matching(leftPartition, N, N);
  77.     }
  78. };
Add Comment
Please, Sign In to add comment