Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

#include<stdio.

h>
#include<stdlib.h>
#include<iostream>
#include<iomanip>
#define max 20
using namespace std;
//Khai bao CTDL dang ma tran
struct DoThiMaTran
{
int n;
float C[max][max];
};

//--------------------
struct Canh
{
int dd,dc;
float ts;
};
struct DoThiCanh
{
int m;
Canh ds[max];
};

void nhapDoThi(DoThiCanh &G)


{
int d,c;
int i;
float ts;
cout<<"Nhap so canh cua do thi:";
cin>>G.m;
for(i = 1; i<=G.m; i++)
{
cout<<"Nhap dinh dau:"; cin>>d;
cout<<"Nhap dinh cuoi:"; cin>>c;
cout<<"Nhap trong so:"; cin>>ts;
G.ds[i].dd = d;
G.ds[i].dc = c;
G.ds[i].ts = ts;
cout<<endl;
}
}

//In do thi duoi dang danch canh


void inDoThi(DoThiCanh G)
{
cout<<"\n Danh sach cac canh cua do thi la: \n";
for(int i = 1; i<=G.m; i++)
cout<<"\n "<<G.ds[i].dd<<" -> "<<G.ds[i].dc<<": "<<G.ds[i].ts;
}

void inDoThi_MaTran(DoThiMaTran G)
{
cout<<"\n\n Ma tran trong so la:\n";
for(int i = 1; i<=G.n; i++)
{
for(int j=1;j<=G.n;j++)
cout<<setw(8)<<setprecision(1)<<fixed<<G.C[i][j];
cout<<"\n";
}
}

//Liet ke cac dinh ke cua dinh k trong do thi G


void lietKeDinhKe_MaTran(DoThiMaTran G,int k)
{
int i,j;
cout<<"\n Cac dinh ke cua dinh "<<k<<" la:";
for(i=1;i<=G.n;i++)
if(G.C[k][i]>0)
cout<<setw(8)<<i;
}

void DSCanh_MaTran(DoThiCanh G, DoThiMaTran &GG)


{
int i,j;
//Tinh so dinh cua do thi
GG.n = 0;
for(i=1; i<=G.m; i++)
{
if(G.ds[i].dd > GG.n)
GG.n = G.ds[i].dd;
if(G.ds[i].dc > GG.n)
GG.n = G.ds[i].dc;
}
//printf("\n Dinh cua do thi la:%d", GG.n);
for(i=1; i<=GG.n; i++)
for(j=1; j<=GG.n; j++)
GG.C[i][j] = 0;

for(i=1; i<=G.m; i++)


{
GG.C[ G.ds[i].dd ][ G.ds[i].dc ] = G.ds[i].ts;
GG.C[ G.ds[i].dc ][ G.ds[i].dd ] = G.ds[i].ts;
}
}

void lietKeDinhKe_DSCanh(DoThiCanh G,int k)


{
int i;
cout<<"\n Cac dinh ke cua dinh "<<k<<" la:";
for(i=1;i<=G.m;i++)
{
if(G.ds[i].dd == k)
cout<<setw(8)<<G.ds[i].dc;
if(G.ds[i].dc == k)
cout<<setw(8)<<G.ds[i].dd;
}
}
int main()
{
DoThiCanh G;
DoThiMaTran GG;
int k;
nhapDoThi(G);
inDoThi(G);

DSCanh_MaTran(G, GG);
inDoThi_MaTran(GG);
}

You might also like