C language using backtracking method to solve the problem of traveling salesman and the problem of M coloring of graphs _c language

Source: Internet
Author: User
Tags first row

Travel salesman problem
1. Question Description:

The problem of traveling salesman is also called TSP. The problem is as follows: A salesman to a number of cities to sell goods, known to the distance between the city (or travel), he would like to select a departure from the station, through each city the final return to the station route, so that the overall route (or total travel) the smallest. The mathematical model for given a graph, to traverse each vertex once and only once a loop, finally back to the minimum cost of starting point.

2. Input requirements:

The first behavior of the input test sample number T (T < 120), followed by a test sample of T. The first row of each test sample is the number of vertices n, the number of edges m (N < 12,m < 100) of the graph without direction, and then the M row, three integers u, V, and W in each row, indicating that a side of the vertex U and V has a weighted value of W. (1 <= u < v <= n,w <= 1000). Assume the starting point (the station) is the vertex of number 1th.

3. Output Requirements:

For each test sample output line, the format is "Case #: W", where ' # ' indicates the first few test samples (starting from 1), W is the optimal solution of TSP problem, if the feasible solution can not find the output-1.

4. Sample input:

2
5 8
1 2 5
1
4
7 1 5 9 2 3 2 4 3 2 5 6 3 4 8 4 5 4-3 1 1 2

5. Sample output:

Case 1:36 Case
2:-1

6. Solution:

Travel salesman problem (backtracking) #include <iostream> #define N-namespace Std; int n,m,w,//graph vertex number and edge number graph[n][n],//graph weighted adjacency matrix c=0,//Current cost bestc=-1,//Current optimal value x[n],//current solution be    Stx[n]; 
The current optimal solution void backtrack (int k); 
void swap (int &a,int &b); 
  void swap (int &a,int &b) {int temp=a; 
  A=b; 
B=temp; } void Backtrack (int k) {if (k==n) {if (c+graph[x[n-1]][x[n]]+graph[x[n]][1]<bestc| | Bestc==-1) && graph[x[n-1]][x[n]]!=-1 && graph[x[n]][1]!=-1) {bestc=c+graph[x[n-1]][x[n]]+gr 
      APH[X[N]][1]; 
      for (int i=1;i<=n;i++) {bestx[i]=x[i]; 
  } return; else {for (int i=k;i<=n;i++) {if graph[x[k-1]][x[i]]!=-1 && C+GRAPH[X[K-1]][X[I]]&L T;BESTC | | 
        Bestc==-1)) {swap (x[i],x[k]); 
        C+=graph[x[k-1]][x[k]]; 
        Backtrack (k+1); 
        C-=graph[x[k-1]][x[k]]; 
      Swap (x[i],x[k]); 
} 
    }  int main (void) {int i,j,tmp=1,testnum;
  cin>>testnum;
    while (Tmp<=testnum) {cin>>n>>m;
    for (i=1;i<=n;i++) for (j=1;j<=n;j++) graph[i][j]=-1;
      for (int k=1;k<=m;k++) {cin>>i>>j>>w;
      Graph[i][j]=w;
    Graph[j][i]=w;
      for (i=1;i<=n;i++) {x[i]=i;
    Bestx[i]=i;
    } backtrack (2);
    cout<< "Case" <<tmp<< ":" <<bestc<<endl;
    Bestc=-1;
    
    c=0;
  tmp++;
return 0;

 }

The problem of M coloring of graphs
1. Description of the problem
given undirected connected graphs G and m are different colors. These colors are colored for each vertex of fig g, each vertex a color. Whether there is a coloring method of g in each side of the 2 vertices of different colors, ask how many methods for the figure can be m coloring.

2. Input requirements:
the first input is the number T (T < 120) of the test sample, followed by a test sample of T. The first line of each test sample is the number of vertices n, the number of edges m and the number of available colors m (n <= 10,m < 100,m <= 7), followed by m rows, two integers u and V in each row, representing a side connection between the vertices U and v. (1 <= u < v <= N).

3. Output Requirements:
for each test sample output two lines, the first line is "Case #: W", where ' # ' indicates the first few test samples (starting from 1), and W is the number of M-shaded schemes.

4. Sample input:

1
5 8 5
1 2
1
3
1 4 2 3 2 4 2 5 3 4 4-5

5. Sample output:

Case 1:360

6. Solution:

#include <iostream> using namespace std;
#define N-M,n,m,a[n][n],x[n],textnum int;

int static sum=0;
  bool OK (int k) {for (int j=1;j<=n;j++) if (a[k][j]&& (x[j]==x[k)) is return false;
return true;
    } void Backtrack (int t) {if (t>n) {sum++;
    for (int i=1;i<=n;i++)//cout<<x[i]<< "";
  cout<<endl;
    else for (int i=1;i<=m;i++) {x[t]=i;
    if (OK (t)) backtrack (t+1);
  x[t]=0;
  int main () {int i,j,z=1;         cin>>textnum;          Enter the number of tests while (textnum>0) {cin>>n;
    The number of input vertices for (i=1;i<=n;i++) for (j=1;j<=n;j++) a[i][j]=0;         cin>>m>>m;
      Number of input edges, number of available colors for (int k=1;k<=m;k++)//graph adjacency Matrix {cin>>i>>j;
      A[i][j]=1;
    A[j][i]=1;
    }/* for (i=1;i<=n;i++) {for (j=1;j<=n;j++) cout<<a[i][j]<< "; Cout<<endl;}
    * * for (i=0;i<=n;i++) x[i]=0; BaCktrack (1);
    cout<< "Case" <<z<< ":" <<sum<<endl;
        
    sum=0;
    textnum--;
  z++;
return 0;
 }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.