//算法分析与设计 第四章 贪心算法
//Made by syx Time:2010年7月20日 14:46:02
//
//活动安排问题
//背包问题
//
//
//
#include
#define MAX_VALUE 9999
void dijkstra(int v,int a[][10],int dist[],int prev[])
{
int n = 5;
if(v<1||v>5) return ;
bool s[6];
int i = 0;
for(i=1; i<=n; ++i)
{
dist[i] = a[v][i];
s[i] = false;
if(dist[i] == MAX_VALUE)
prev[i] = 0;
else
prev[i] = v;
}
dist[v] = 0;
s[v] = true;
for(i=1; i<=n;++i)
{
int temp = MAX_VALUE;
int u = v;
//int j = 0;
for(int j=1; j<=n; ++j)
{
if( (!s[j])&&(dist[j]
u = j;
temp = dist[j];
}
s[u] = true;
for(int j=1; j<=n; ++j)
{
if( (!s[j])&&(a[u][j]
int newdist = dist[u] + a[u][j];
if(newdist
dist[j] = newdist;
prev[j] = u;
}
}
}
}
int i = 0;
for(i=1; i<=5; ++i)
{
printf("%3d",prev[i]);
}
printf("\n");
}
}
int main()
{
int a[10][10] = {MAX_VALUE};
a[1][2] = 10;
a[1][4] = 30;
a[1][5] = 100;
a[2][3] = 50;
a[3][5] = 10;
a[4][3] = 20;
a[4][5] = 60;
int dist[10] = {MAX_VALUE};
int prev[10] = {0};
dijkstra(1,a,dist,prev);
int i = 0;
for(i=1; i<=5; ++i)
{
printf("%3d",prev[i]);
}
printf("\n");
return 0;
}
/*
//背包问题
#include
using namespace std;
#define MAX_NUM 10
typedef struct
{
float wv[MAX_NUM];
int num[MAX_NUM+1];
}WV;
WV wv;
inline void swap(float &x,int &left,float &y,int &j)
{
float temp = x;
x = y;
y = temp;
int iTemp = wv.num[left];
wv.num[left] = wv.num[j];
wv.num[j] = iTemp;
}
int partition(float a[],int left,int right)
{
float q = a[left];
int i = left ,
j = right + 1;
while(true)
{
while(a[++i] < q) ;
while(a[--j] > q) ;
if(i>j)
break;
swap(a[i],i,a[j],j);
}
swap(a[left],left,a[j],j);
return j;
}
void qSort(float a[],int left,int right)
{
if(left
int q = partition(a,left,right);
// int i = 0;//调试partition函数是否执行正确
// for( i=0; i<=9; ++i)
// printf("%d ",a[i]);
// printf("\n");
// getchar();
qSort(a,left,q-1);
qSort(a,q+1,right);
}
}
class CBeibao
{
private:
float c ;
float w[MAX_NUM];
float v[MAX_NUM];
float result[MAX_NUM];
int length;
float opt ;
public:
CBeibao()
{
c = 50;
w[0] = 10;
v[0] = 60;
w[1] = 20;
v[1] = 100;
w[2] = 30;
v[2] = 120;
length = 3;
opt = 0.0;
wv.wv[0] = 6;
wv.num[0] = 0;
wv.wv[1] = 5;
wv.num[1] = 1;
wv.wv[2] = 4;
wv.num[2] = 2;
}
float knapsack()
{
qSort(wv.wv,0,length-1);
int i = 0;
for(i=0; i
printf("%f ",wv.wv[i]);
printf(" %3d ",wv.num[i]);
printf("\n");
}
for(i=length-1; i>=0; --i)
{
if(w[wv.num[i]] > c)
break;
result[wv.num[i]] = 1;
opt += v[wv.num[i]];
c -= w[wv.num[i]];
printf("%d %d %f %f %f \n",i,wv.num[i],w[wv.num[i]],v[wv.num[i]],c);
}
if(i
{
float fTemp = c / w[wv.num[i]];
result[wv.num[i]] = fTemp;
opt += v[wv.num[i]]*fTemp;
}
return 0.0;
}
void printResult()
{
int i = 0;
for(i=0; i
if(result[i]==1.0)
{
printf("%d %f %f 被全部放入!\n",i+1,w[i],v[i]);
}
else if(result[i]>0.0&&result[i]<1.0)
{
printf("%d %f %f 被部分放入!\n",i+1,w[i]*result[i],v[i]*result[i]);
}
}
printf("总价值:%f\n",opt);
printf("\n");
}
};
int main()
{
CBeibao bb;
bb.knapsack();
bb.printResult();
return 0;
}
*/
/*
//活动安排问题
#include
int greedySelector(int s[],int f[],bool result[],int length)
{
int n = length - 1;
result[0] = 1;
int count = 1;
int i = 0,j = 0;
for(i=1,j=0; i<=n; ++i)
{
if(s[i]>=f[j])
{
result[i] = 1;
count++;
j = i;
}
else
result[i] = 0;
}
return count;
}
int main()
{
int s[11] = {1,3,0,5,3,5,6,8,8,2,12};
int f[11] = {4,5,6,7,8,9,10,11,12,13,14};
bool result[11] = {0};
greedySelector(s,f,result,11);
int i = 0;
for(i=0; i<11;++i)
{
printf("%3d",s[i]);
}
printf("\n");
for(i=0; i<11;++i)
{
printf("%3d",f[i]);
}
printf("\n");
for(i=0; i<11;++i)
{
printf("%3d",result[i]);
}
printf("\n");
return 0;
}
*/