背包问题
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
const int volume = 100 ;
const int N = 5 ;
struct OBJECTS{
int value;
int weight;
float rate;
} ;
OBJECTS objects[ N] ;
void swap ( OBJECTS & a, OBJECTS & b)
{
OBJECTS X;
X = a;
a = b;
b = X;
}
void init_1 ( )
{
objects[ 0 ] . value= 20 ; objects[ 0 ] . weight= 10 ; objects[ 0 ] . rate= 2 ;
objects[ 1 ] . value= 30 ; objects[ 1 ] . weight= 20 ; objects[ 1 ] . rate= 1.5 ;
objects[ 2 ] . value= 65 ; objects[ 2 ] . weight= 30 ; objects[ 2 ] . rate= 2.1 ;
objects[ 3 ] . value= 40 ; objects[ 3 ] . weight= 40 ; objects[ 3 ] . rate= 1 ;
objects[ 4 ] . value= 60 ; objects[ 4 ] . weight= 50 ; objects[ 4 ] . rate= 1.2 ;
}
int partition ( int p, int r)
{
float x= objects[ r] . rate;
int i= p- 1 ;
for ( int j= p; j< r; j++ )
{
if ( objects[ j] . rate<= x)
{
i+ = 1 ;
OBJECTS X;
X = objects[ i] ;
objects[ i] = objects[ j] ;
objects[ j] = X;
}
}
OBJECTS Y;
Y = objects[ i+ 1 ] ;
objects[ i+ 1 ] = objects[ r] ;
objects[ r] = Y;
return i+ 1 ;
}
int random_partition ( int p, int r)
{
int i= p+ rand ( ) % ( r- p+ 1 ) ;
OBJECTS X;
X = objects[ i] ;
objects[ i] = objects[ r] ;
objects[ r] = X;
int n;
n= partition ( p, r) ;
return n;
}
void random_qk_sort ( int p, int r)
{
int q;
if ( p< r)
{
q= random_partition ( p, r) ;
random_qk_sort ( p, q- 1 ) ;
random_qk_sort ( q+ 1 , r) ;
}
}
void print ( )
{
for ( int i= 0 ; i< N; i++ )
{
cout<< objects[ i] . rate<< " " ;
}
cout<< endl;
}
void frac_knapsack ( )
{
random_qk_sort ( 0 , N- 1 ) ;
float sum= 0 ;
float count= 0 ;
for ( int i= N- 1 ; i>= 0 ; i-- )
{
if ( sum< volume)
{
if ( objects[ i] . weight<= ( volume- sum) )
{
sum+ = objects[ i] . weight;
count+ = objects[ i] . value;
cout<< i<< " " << objects[ i] . value<< " " << objects[ i] . weight<< endl;
}
else
{
count+ = ( volume- sum) * objects[ i] . rate;
cout<< i<< " " << ( volume- sum) * objects[ i] . rate<< " " << ( volume- sum) << endl;
sum= volume;
}
}
else break ;
}
cout<< count<< endl;
}
int zero_one_knapsack ( )
{
int vec[ N+ 1 ] [ volume+ 1 ] ;
for ( int i= 0 ; i<= N; i++ )
{
vec[ i] [ 0 ] = 0 ;
}
for ( int j = 0 ; j<= volume; j++ )
{
vec[ 0 ] [ j] = 0 ;
}
for ( int i= 1 ; i<= N; i++ )
{
for ( int j= 1 ; j<= volume; j++ )
{
if ( j< objects[ i- 1 ] . weight)
{
vec[ i] [ j] = vec[ i- 1 ] [ j] ;
}
else {
vec[ i] [ j] = max ( vec[ i- 1 ] [ j] , vec[ i- 1 ] [ j- objects[ i- 1 ] . weight] + objects[ i- 1 ] . value) ;
}
}
}
int x[ N+ 1 ] ;
int j= volume;
for ( int i= N; i> 0 ; i-- )
{
if ( vec[ i] [ j] > vec[ i- 1 ] [ j] )
{
x[ i] = 1 ;
j= j- objects[ i- 1 ] . weight;
}
else
{
x[ i] = 0 ;
}
}
for ( int i= 1 ; i<= N; i++ )
{
cout<< x[ i] << " " ;
}
cout<< endl;
return vec[ N] [ volume] ;
}
int main ( )
{
init_1 ( ) ;
print ( ) ;
random_qk_sort ( 0 , N- 1 ) ;
print ( ) ;
frac_knapsack ( ) ;
return 0 ;
}
调度问题
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main ( )
{
vector< int > jobs;
int N;
cout<< "please input the number of jobs:" << endl;
cin>> N;
cout<< "please input time of jobs: " << endl;
for ( int i, j= 0 ; j< N; j++ )
{
cin>> i;
jobs. push_back ( i) ;
}
sort ( jobs. begin ( ) , jobs. end ( ) ) ;
int consume= 0 ;
int sum= 0 ;
int n= jobs. size ( ) ;
for ( int i= 0 ; i< n; i++ )
{
consume+ = jobs[ i] ;
sum+ = consume;
}
float average;
average= float ( sum) / float ( n) ;
cout<< average<< endl;
return 0 ;
}
单源节点
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int n= 5 ;
const int s = 0 ;
int matrix[ n] [ n] ;
struct VEX{
int d;
int pi;
} ;
VEX vex[ n] ;
void init ( )
{
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< n; j++ )
{
matrix[ i] [ j] = INT_MAX;
}
}
for ( int i= 0 ; i< n; i++ )
{
matrix[ i] [ i] = 0 ;
}
matrix[ 0 ] [ 1 ] = - 1 ; matrix[ 0 ] [ 2 ] = 3 ;
matrix[ 1 ] [ 2 ] = 3 ; matrix[ 1 ] [ 3 ] = 2 ; matrix[ 1 ] [ 4 ] = 2 ;
matrix[ 3 ] [ 1 ] = 1 ; matrix[ 3 ] [ 2 ] = 5 ;
matrix[ 4 ] [ 3 ] = - 3 ;
}
void print ( )
{
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< n; j++ )
{
cout<< matrix[ i] [ j] << " " ;
}
cout<< endl;
}
}
void init_single_source ( int s)
{
for ( int i= 0 ; i< n; i++ )
{
vex[ i] . d= INT_MAX;
}
vex[ s] . d= 0 ;
}
void relax ( int u, int v, int w )
{
if ( vex[ v] . d> ( vex[ u] . d+ w) )
{
vex[ v] . d= vex[ u] . d+ w;
vex[ v] . pi= u;
}
}
bool Bellman_ford ( int s)
{
init_single_source ( s) ;
for ( int i= 1 ; i< n; i++ )
{
for ( int j= 0 ; j< n; j++ )
{
for ( int k= 0 ; k< n; k++ )
{
if ( matrix[ j] [ k] != INT_MAX)
{
relax ( j, k, matrix[ j] [ k] ) ;
}
}
}
}
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< n; j++ )
{
if ( matrix[ i] [ j] != INT_MAX)
{
if ( vex[ j] . d> ( vex[ i] . d+ matrix[ i] [ j] ) ) return false ;
}
}
}
return true ;
}
void out ( int target)
{
if ( target> 0 )
{
target= vex[ target] . pi;
out ( target) ;
cout<< target<< "->" ;
}
}
int main ( )
{
init ( ) ;
int target;
cout<< "please input the vertex you wana to get:" << endl;
cin>> target;
if ( Bellman_ford ( s) )
{
cout<< "最短路径:" << endl;
out ( target) ;
cout<< target<< endl;
cout<< "最短路径权重值之和" << endl;
cout<< vex[ target] . d<< endl;
}
return 0 ;
}
多源点路径
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
const int n= 5 ;
int matrix[ n] [ n] ;
int P[ n] [ n] ;
int D[ n] [ n] ;
void init ( )
{
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< n; j++ )
{
matrix[ i] [ j] = INT_MAX;
}
}
for ( int i= 0 ; i< n; i++ )
{
matrix[ i] [ i] = 0 ;
}
matrix[ 0 ] [ 1 ] = - 1 ; matrix[ 0 ] [ 2 ] = 3 ;
matrix[ 1 ] [ 2 ] = 3 ; matrix[ 1 ] [ 3 ] = 2 ; matrix[ 1 ] [ 4 ] = 2 ;
matrix[ 3 ] [ 1 ] = 1 ; matrix[ 3 ] [ 2 ] = 5 ;
matrix[ 4 ] [ 3 ] = - 3 ;
}
void print ( int m[ n] [ n] )
{
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< n; j++ )
{
cout<< m[ i] [ j] << " " ;
}
cout<< endl;
}
}
void floyd_warshall ( )
{
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< n; j++ )
{
D[ i] [ j] = matrix[ i] [ j] ;
}
}
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< n; j++ )
{
if ( matrix[ i] [ j] != 0 && matrix[ i] [ j] != INT_MAX)
{
P[ i] [ j] = i;
}
else {
P[ i] [ j] = INT_MAX;
}
}
}
for ( int k= 1 ; k<= n; k++ )
{
for ( int i= 1 ; i<= n; i++ )
{
for ( int j= 1 ; j<= n; j++ )
{
if ( D[ i- 1 ] [ j- 1 ] > ( D[ i- 1 ] [ k- 1 ] + D[ k- 1 ] [ j- 1 ] ) && D[ i- 1 ] [ k- 1 ] != INT_MAX&& D[ k- 1 ] [ j- 1 ] != INT_MAX)
{
P[ i- 1 ] [ j- 1 ] = P[ k- 1 ] [ j- 1 ] ;
D[ i- 1 ] [ j- 1 ] = D[ i- 1 ] [ k- 1 ] + D[ k- 1 ] [ j- 1 ] ;
}
}
}
}
print ( D) ;
}
void out ( int s, int e)
{
if ( P[ s] [ e] != s)
{
out ( s, P[ s] [ e] ) ;
cout<< P[ s] [ e] << "->" ;
}
}
int main ( )
{
int start, end;
init ( ) ;
floyd_warshall ( ) ;
cout<< "please input start and end pots:" << endl;
cin>> start>> end;
if ( D[ start] [ end] != INT_MAX|| D[ start] [ end] != 0 )
{
cout<< "最短路径代价:" ;
cout<< D[ start] [ end] << endl;
cout<< "最短路径:" ;
cout<< start<< "->" ;
out ( start, end) ;
cout<< end<< endl;
}
else {
cout<< "不存在最短路径" << endl;
}
return 0 ;
}