# Difference constraint system C ++ implementation

Source: Internet
Author: User

Difference constraint: each row of Linear Programming matrix A contains A 1 and A-1, and other elements are 0. therefore, the constraint given by Ax <= B is m sets of difference constraints, including n unknown elements. Each constraint is an inequality:

Xj-xi <= bk

Where 1 <= I, j <= n, I <= k <= m

Solution: regard n unknown elements as the vertices of n Directed Graphs. xj-xi <= bk indicates a directed line segment from vertex j to vertex I with a length of bk. Add another v0 vertex, which is a directed line segment from v0 to other vertices with a length of 0. (For example)

It can be proved that xi = β (v0, vi) (β (v0, vi) is the shortest path length from vertex 0 to vertex I ). Therefore, you can use Bellman_Ford to calculate the single-source shortest path (the Dijkstra algorithm cannot be used because the length of a directed line segment can be negative)

// Differential constraint system. cpp: Defines the entry point for the console application.

//

# Include "stdafx. h"

# Include <iostream>

# Deprecision MAX 100

# Define Infinity 65535

Using namespace std;

// Tail node Structure of the edge

Struct edgeNode

{

Int no; // The number of the end of the edge

Int weight; // Edge weight

Struct edgeNode * next; // next

};

// Node Structure

Struct vexNode

{

Char info; // node name

Struct edgeNode * link; // endpoint connected to it

};

// Store node Information

// Precursor Node

Int parent [MAX];

//// The cost of the shortest path from the source point to the node j

Int lowcost [MAX];

// Difference matrix

Int a [MAX] [MAX];

// Shard set

Int w [MAX];

// Create an adjacent table store based on the difference matrix

// Adjlist is the node set, and parent [j] is the precursor node of the shortest path from node 0 to node j.

// Lowcost [j] is the cost of the shortest path from 0 nodes to node j.

// W is the input difference Constraint

// M and n are the number of rows and columns of the difference matrix respectively.

Void createGraph (vexNode * adjlist, int * parent, int * lowcost, int * w, int m, int n)

{

Int I, j;

// Initialization, node vi name is char (a + I)

For (I = 0; I <= n; I ++)

{

Adjlist [I]. info = (char) ('A' + I );

Lowcost [I] = Infinity;

Parent [I] = I;

}

Int col1, col2;

Col1 = col2 = 0;

EdgeNode * p1;

For (I = 1; I <= m; I ++)

{

For (j = 1; j <= n; j ++)

{

If (a [I] [j] =-1)

Col1 = j;

Else if (a [I] [j] = 1)

Col2 = j;

}

P1 = (edgeNode *) malloc (sizeof (edgeNode ));

P1-> no = col2;

P1-> weight = w [I];

}

For (I = 1; I <= n; I ++)

{

P1 = (edgeNode *) malloc (sizeof (edgeNode ));

P1-> no = I;

P1-> weight = 0;

}

Lowcost [0] = 0;

}

// Find the shortest path from v0 to each node

Bool Bellman_Ford (vexNode * adjlist, int * lowcost, int * parent, const int n)

{

Int I, j;

For (j = 0; j <n; j ++)

{

For (I = 0; I <= n; I ++)

{

While (p1! = NULL)

{

If (lowcost [I] + p1-> weight <lowcost [p1-> no])

{

Lowcost [p1-> no] = lowcost [I] + p1-> weight;

Parent [p1-> no] = I;

}

P1 = p1-> next;

}

}

}

// Check whether there is a negative Loop

For (I = 1; I <= n; I ++)

{

While (p2! = NULL)

{

If (lowcost [p2-> no]> lowcost [I] + p2-> weight)

Return false;

P2 = p2-> next;

}

}

Return true;

}

Int _ tmain (int argc, _ TCHAR * argv [])

{

Int cases;

Cout <"Enter the number of cases :";

Cin> cases;

While (cases --)

{

Int I, j;

Int n, m;

Cout <"Enter the number of rows (m) and number of columns (n) of the difference matrix ):";

Cin> m> n;

Cout <"Enter the difference matrix:" <endl;

For (I = 1; I <= m; I ++)

For (j = 1; j <= n; j ++)

Cin> a [I] [j];

Cout <"Enter the consumer set:" <endl;

For (I = 1; I <= m; I ++)

Cin> w [I];

CreateGraph (adjlist, parent, lowcost, w, m, n );

/*

For (I = 0; I <= n; I ++)

{

Cout <I <":";

While (p! = NULL)

{

Cout <"(" <p-> no <"," <p-> weight <")";

P = p-> next;

}

Cout <endl;

}

*/

// Call the Bellman-Ford Algorithm

Bool flag = Bellman_Ford (adjlist, lowcost, parent, n );

If (! Flag)

Cout <"no solution" <endl;

Else

{

// Output solution set

Cout <"one of the solutions sets is (this solution set is added with an arbitrary constant d, which is also its solution set):" <endl;

For (I = 1; I <= n; I ++)

Cout <"x" <I <"=" <lowcost [I] <endl;

}

}

System ("pause ");

Return 0;

}

--------------------------------------------------- Program test ---------------------------------------------------

Enter the number of cases: 1

Enter the number of rows (m) and number of columns (n) of the difference matrix: 8 5

Enter the difference matrix:

1-1 0 0 0

1 0 0 0-1

0 1 0 0-1

-1 0 1 0 0

-1 0 0 1 0

0 0-1 1 0

0 0-1 0 1

0 0 0-1 1

Enter the sequence set:

0-1 1 5 4-1-3-3

One of the solutions sets is (this solution set is added with an arbitrary constant d, which is also its solution set ):

X1 =-5

X2 =-3

X3 = 0

X4 =-1

X5 =-4

Press any key to continue ..

Author heyongluoyao8

Related Keywords:
Related Article

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.