示例#1
0
bool gaussian_elimination(vvd &matrix, vd &output) {
  for (int i = 0; i < matrix.size(); i++) {
    int pivot = find_pivot(matrix, i);
    if (fabs(matrix[pivot][i]) < EPS) {
      return false;
    }
    swap_row(matrix, pivot, i);
    double factor = matrix[i][i];
    for (int j = 0; j < matrix[i].size(); j++) {
      matrix[i][j] /= factor;
    }
    for (int j = 0; j < matrix.size(); j++) {
      if (j == i) {
        continue;
      }
      factor = matrix[j][i];
      for (int k = 0; k < matrix[j].size(); k++) {
        matrix[j][k] -= factor*matrix[i][k];
      }
    }
  }
  output.clear();
  for (int i = 0; i < matrix.size(); i++) {
    output.push_back(matrix[i][matrix[i].size() - 1]);
  }
  return true;
}
void getDualProblem(const vvd& a, vd& b, vd& c, vvd& dual_a, vd& dual_b, vd& dual_c)
{
    for (auto x : b)
    {
        dual_c.push_back(-x);
    }
    for (auto x : c)
    {
        dual_b.push_back(-x);
    }
    dual_a.assign(a[0].size(), vd());
    for (size_t i = 0; i < a.size(); ++i)
    {
        for (size_t j = 0; j < a[i].size(); ++j)
        {
            dual_a[j].push_back(-a[i][j]);
        }
    }
}
示例#3
0
int main(){
    ios::sync_with_stdio(0);
    int count=0;
    while(cin >> n && n){
        cout << "**********************************************************\n";
        cout << "Network #"<<++count << endl;
        dist.clear();
        dist.resize(n,vd(n));
        memset(memo,-1,sizeof memo);
        X.clear();X.resize(n);
        Y.clear();Y.resize(n);
        for (int i = 0; i < n; ++i) {
            cin >> X[i] >> Y[i];
        }
        for (int j = 0; j < n; ++j) {
            for (int i = j+1; i < n; ++i) {
                dist[j][i]= dist[i][j] = dis(j,i);
                //cerr << "dist " << j << "  " << i <<"  ->   "<< dist[j][i] << endl;
            }
        }

        double ans = inf; int id =-1;
        for (int k = 0; k < n; ++k) {
            double val = tsp(k,(1<<k));
            //cerr << val << endl;
            if(val < ans) ans = val,id=k;
        }
        //id=0;
        //cerr << id << endl;
        int msk=0;
        while(--n) {
                msk |= (1 << id);
            if(dis(id,par[id][msk])!=16) {
                printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2lf feet.\n", X[id], Y[id], X[par[id][msk]], Y[par[id][msk]], dis(id, par[id][msk]) );
                id = par[id][msk];
            }
        }
        printf("Number of feet of cable required is %.2lf.\n",ans);
    }

    return 0;
}
示例#4
0
int find_pivot(const vvd &matrix, int index) {
  double max_value = fabs(matrix[index][index]);
  int max_index = index;
  for (int i = index + 1; i < matrix.size(); i++) {
    if (fabs(matrix[i][index]) > max_value) {
      max_value = fabs(matrix[i][index]);
      max_index = i;
    }
  }
  return max_index;
}
void printTask(const vvd& a, const vd& b, const vd& c, const std::string& filename = "task.txt")
{
    size_t n = c.size();
    size_t m = a.size();
    std::ofstream out(filename);
    for (size_t i = 0; i < n; ++i)
    {
        auto& t = c[i];
        if (std::abs(t) < EPS)
        {
            continue;
        }
        if (t > 0)
        {
            out << "+\t" << t;
        }
        if (t < 0)
        {
            out << "-\t" << -t;
        }
        out << " * x" << i + 1 << "\t";
    }
    out << "-----> MAX\n";
    for (size_t i = 0; i < m; ++i)
    {
        out << "x" << n + i + 1 << "\t=\t" << b[i] << "\t";
        for (size_t j = 0; j < n; ++j)
        {
            auto t = -a[i][j];
            if (std::abs(t) < EPS)
            {
                continue;
            }
            if (t > 0)
            {
                out << "+\t" << t;
            }
            if (t < 0)
            {
                out << "-\t" << -t;
            }
            out << " * x" << j + 1 << "\t";
        }
        out << endl;
    }
}
示例#6
0
int main() {
    inp(t);
    A.resize(26);
    string s,s1;
    for(int i=0;i<26;i++)
        A[i].resize(26);

    while(t--) {
        inp(n);
        inp(k);
        cin>>s;
        for(int i=0;i<26;i++)
            for(int j=0;j<26;j++)
                cin>>A[i][j];

        A=power(A,k);

        double ans=0;
        map<string,int> m;
        for(int i=0;i<n;i++) {
            cin>>s1;
            if(s.length()!=s1.length())
                continue;
            if(m.find(s1)!=m.end())
                continue;
            m[s1]=1;
            // cout<<s<<" "<<s1<<endl;
            double val=1;
            for(int i=0;i<s.length();i++) {
                val*=A[s[i]-'a'][s1[i]-'a'];
            }
            ans+=val;
        }
        printf("%.6f\n", ans);
    }
     
    return 0;
} 
示例#7
0
文件: B.cpp 项目: gzgreg/ACM
bool gaussian(vvd& A) { int m=A.size(), n=A[0].size()-1; vector<bool> used(m,0);
  for (int c = 0; c < n; ++c) for (int r = 0; r < m; ++r)
    if (!used[r] && !ISZERO(A[r][c]) ) { pivot(A, r, c); used[r] = true; }
  for (int r = 0; r < m; ++r) if(!used[r] && !ISZERO(A[r].back())) return false;
  return true; }
示例#8
0
文件: B.cpp 项目: gzgreg/ACM
void pivot(vvd& A, int r, int c) { // A is A|b
  int m = A.size(), n = A[0].size(); ld tmp = A[r][c];
  for (int i = 0; i < n; ++i) A[r][i]/=tmp;
  for (int i = 0; i < m; ++i) if (i != r) { ld k = A[i][c];
    for(int j = 0; j < n; ++j) A[i][j] -= A[r][j]*k; } }