⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mintreedlg.cpp

📁 最小生成树的程序
💻 CPP
字号:
// MintreeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Mintree.h"
#include "MintreeDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMintreeDlg dialog

CMintreeDlg::CMintreeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMintreeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMintreeDlg)
	m_InputNum = _T("");
	m_TopNum = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMintreeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMintreeDlg)
	DDX_Text(pDX, IDC_EDIT1, m_InputNum);
	DDX_Text(pDX, IDC_TOPNUM, m_TopNum);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMintreeDlg, CDialog)
	//{{AFX_MSG_MAP(CMintreeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_MAP, OnMap)
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	ON_EN_CHANGE(IDC_TOPNUM, OnChangeTopnum)
	ON_BN_CLICKED(IDC_MINTREE, OnMintree)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMintreeDlg message handlers

BOOL CMintreeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMintreeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

#include "stdio.h"
#define MAX_VERTEX_NUM 20 //最大顶点数
#define INFINITY       32767
typedef int VRType;
typedef int VertexType;
typedef   int AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
                                                              
                                                                //邻接矩阵类型
typedef struct  {
	VertexType vexs[MAX_VERTEX_NUM];      //顶点表
	AdjMatrix arcs;                                             //邻接矩阵
	int vexnum,arcnum;                       //图的顶点数和弧数
}  MGraph;

struct{
	VertexType adjvex;
	VRType lowcost;
}closedge[MAX_VERTEX_NUM];

int e[100];
int i=0,temp;
int u;
CString input;
Node N[11];
BOOL Paint=FALSE;
int n1=0;
int n[50];

MGraph G;

void StringToInt(CString source);

int LocateVex(MGraph G,int u)
   {int i;

	for (i=0;i<G.vexnum;i++)
              {	if (u==G.vexs[i]) return i;}
	
	if (i==G.vexnum) {  
		printf("Error u!\n");
        return 0;                 //注意!
       }
	return 0;
   }

void CreateMGraph(MGraph &G)
    {	int i,j;	
        
	    G.vexnum =10;
		G.arcnum =11;
		for(i=0;i<G.vexnum ;i++)
		{   G.vexs[i]=i+1;
		    
		}
	   
	    for (i=0;i<G.vexnum;i++)
		 for (j=0;j<G.vexnum;j++)
			G.arcs[i][j]=INFINITY;
		G.arcs[0][2]=G.arcs[2][0]=e[0];
        G.arcs[0][1]=G.arcs[1][0]=e[1];
        G.arcs[0][4]=G.arcs[4][0]=e[2];
        G.arcs[1][6]=G.arcs[6][1]=e[3];
        G.arcs[2][3]=G.arcs[3][2]=e[4];
        G.arcs[3][5]=G.arcs[5][3]=e[5];
        G.arcs[5][6]=G.arcs[6][5]=e[6];
        G.arcs[5][9]=G.arcs[9][5]=e[7];
        G.arcs[6][8]=G.arcs[8][6]=e[8];
        G.arcs[7][9]=G.arcs[9][7]=e[9];
        G.arcs[8][9]=G.arcs[9][8]=e[10];
}
     
	  
	

//转化成int

void StringToInt(CString source)
{   int j=0;
    char *buff = source.GetBuffer(source.GetLength());
    for(j;j<11;j++,i++)
    {
	 e[i]=atoi(buff);
	 if(e[i]<10)buff+=2;
	 else if(e[i]<100)buff+=3;
	 else if(e[i]<1000)buff+=4;
	 else if(e[i]<10000)buff+=5;
	 else if(e[i]<100000)buff+=6;
	 else buff+=7;
	}
	temp=i;
    source.ReleaseBuffer();
}

//画边
void CMintreeDlg::Drawline(Node N1,Node N2)
{
		UpdateWindow();
	//Invalidate();
	CreateMGraph(G);
	CDC	*pDC = GetDC();
	CClientDC dc(this);
    
	
	CString s;
	

	CPen	pen;
	if(!Paint)
        pen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
	else
	{
		pen.CreatePen (PS_SOLID,3,RGB(255,0,0));
		Paint=FALSE;
	}
	   
	dc.SelectObject(pen);
	dc.MoveTo(N1.x,N1.y);
	dc.LineTo(N2.x,N2.y);

	ReleaseDC(pDC);
}



//画边

void CMintreeDlg::Drawline (Node N1,Node N2,int w)
{
	UpdateWindow();
	//Invalidate();
	CreateMGraph(G);
	CDC	*pDC = GetDC();
	CClientDC dc(this);
    
	
	CString s;
	int x,y;
	x=(N1.x+N2.x)/2;
	y=(N1.y+N2.y)/2;
    s.Format("%d",w);
	pDC->SetBkColor(RGB(0,255,255));
    pDC->TextOut(x,y,s);

	CPen	pen;
	if(!Paint)
        pen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
	else
	{
		pen.CreatePen (PS_SOLID,3,RGB(255,0,0));
		Paint=FALSE;
	}
	   
	dc.SelectObject(pen);
	dc.MoveTo(N1.x,N1.y);
	dc.LineTo(N2.x,N2.y);
	ReleaseDC(pDC);
}


void CMintreeDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMintreeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//原始图
void CMintreeDlg::OnMap() 
{
	// TODO: Add your control notification handler code here
	 CMintreeDlg::Invalidate();
	 CMintreeDlg::UpdateWindow();
	 	
	UpdateData();
	
	
	
	CDC *pDC=GetDC();
	CClientDC dc(this);
	StringToInt(input);
	CreateMGraph(G);

	
	int x,y;int i;int k;
	x=100;y=80;i=-1;
	for(k=1;k<=10;k+=2)
	{  N[k].x=x;
	   N[k].y=y;
	 x=x+i*40;i=-i;
	 y+=65;
	}
	x=400;y=100;i=1;
	for(k=2;k<=10;k+=2)
	{
	 N[k].x=x;N[k].y=y;
	 x=x+i*45;i=-i;
	 y+=70;
	}                             //顶点坐标
	CString s;
	for( i=0;i<=10;i++)
	{   pDC->SetBkColor(RGB(255,255,0));
		s.Format("%d",G.vexs[i]);
	    pDC->TextOut(N[i+1].x,N[i+1].y,s);
		
	}                            //输出顶点
	 
	Drawline(N[1],N[2],e[0]);
	Drawline(N[1],N[3],e[1]);
	Drawline(N[1],N[5],e[2]);
	Drawline(N[2],N[7],e[3]);
	Drawline(N[3],N[4],e[4]);
	Drawline(N[4],N[6],e[5]);
	Drawline(N[6],N[7],e[6]);
	Drawline(N[6],N[10],e[7]);
	Drawline(N[7],N[9],e[8]);
	Drawline(N[8],N[10],e[9]);
	Drawline(N[9],N[10],e[10]);
	ReleaseDC(pDC);
	
	
}

//输入权值
void CMintreeDlg::OnChangeEdit1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData();
	input=m_InputNum;            
	
}

//输入初始顶点
void CMintreeDlg::OnChangeTopnum() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData();
	u=m_TopNum;
	
}

//生成最小生成树
void CMintreeDlg::OnMintree() 
{
	// TODO: Add your control notification handler code here
	int k,j,i,minCost,p=0;
	UpdateData(FALSE);
	UpdateWindow();
	Invalidate();
	OnMap();
	
	k=LocateVex(G,u);
	n[n1++]=k+1;
	
	for (j=0;j<G.vexnum;++j)
		if (j!=k) {
			closedge[j].adjvex=u;
			closedge[j].lowcost=G.arcs[k][j]; }
		closedge[k].lowcost=0;
    for (i=1;i<G.vexnum;++i)
    {  
        minCost=INFINITY;
       for (j=0;j<G.vexnum;++j)
        { if (closedge[j].lowcost <minCost &&
               closedge[j].lowcost!=0)
                 { minCost=closedge[j].lowcost;k=j;}
		}
	   Paint=TRUE;
	   n[n1++]=k+1;
	   p=LocateVex(G,closedge[k].adjvex);
	   Drawline(N[p+1],N[k+1]);
	   
	   closedge[k].lowcost=0;
       for (j=0;j<G.vexnum;++j)
          if (G.arcs[k][j]<closedge[j].lowcost)
             { closedge[j].adjvex=G.vexs[k];
        	    closedge[j].lowcost=G.arcs[k][j];
		  }
    }
	

  
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -