データ構造カリキュラム設計(病院の場所選択)コード
4107 ワード
#include
#include
using namespace std;
#define MAXV 50
#define INF 1000000000
typedef int InfoType;
//
typedef struct
{
int n;
int edges[MAXV][MAXV];
} MGraph;
FILE *f_in;//
FILE *f_out;//
int n;//n
//
//
void Ppath(int path[],int i,int v)
{
int k;
k = path[i];
if(k == v) //
return;
Ppath(path,k,v);
fprintf(f_out,"%d->",k);
}
//
int biaoji1=1,biaoji2=0;
void Dispath(int dist[],int path[],bool s[],int n,int v)
{
int i;
for(i = 0;i < n;i ++)
{
if(i == v)
continue;
if(s[i] == 1)// v i
{
fprintf(f_out," %d %d :%d ",v,i,dist[i]);
fprintf(f_out," : ");
fprintf(f_out,"%d->",v);
//
Ppath(path,i,v);
fprintf(f_out,"%d
",i);
if(biaoji1 + 1 != n)
{
biaoji2+=dist[i];biaoji1++;
}
else
{
fprintf(f_out," :%d
",biaoji2);
biaoji1=1;biaoji2=0;
}
}
else
fprintf(f_out," %d %d
",v,i);
}
}
//path
//dist v
//s
void Dijkstra(MGraph g,int v)
{
int dist[MAXV],path[MAXV];
bool s[MAXV];
int mindis,i,j,u;
for(i = 0;i < g.n;i ++)
{
dist[i] = g.edges[v][i];
//
s[i] = 0;
//path
if(g.edges[v][i] < INF) path[i] = v;
else path[i] = -1;
}
//
s[v] = 1;dist[v] = 0;
//Dijkstra
for(i = 0;i < g.n;i ++)
{
mindis = INF;
for(j = 0;j < g.n;j ++)
{
if(!s[j] && dist[j] < mindis)
{
u = j;
mindis = dist[j];
}
}
s[u] = 1;
for(j = 0;j < g.n;j ++)
{
if(!s[j])
{
if(g.edges[u][j] < INF && dist[u] + g.edges[u][j] < dist[j])
{
dist[j] = dist[u] + g.edges[u][j];
path[j] = u;
}
}
}
}
Dispath(dist,path,s,g.n,v);
}
//
//
void Ppath1(int path[][MAXV],int i,int j)
{
int k;
k = path[i][j];
if(k == -1) return;//
Ppath1(path,i,k);
fprintf(f_out,"%d->",k);
Ppath1(path,k,j);
}
//
void Dispath1(int A[][MAXV],int path[][MAXV],int n)
{
int i,j;
for(i = 0;i < n;i ++)
{
for(j = 0;j < n;j ++)
{
if(i == j) continue;
if(A[i][j] == INF)
{
if(i != j)
fprintf(f_out," %d %d ",i,j);
}
else
{
fprintf(f_out," %d %d :%d ",i,j,A[i][j]);
fprintf(f_out," : ");
fprintf(f_out,"%d->",i);
Ppath1(path,i,j);//
fprintf(f_out,"%d
",j);
}
}
}
}
//
void Floyd(MGraph g)
{
int A[MAXV][MAXV],path[MAXV][MAXV];
int i,j,k;
//
for(i = 0;i < g.n;i ++)
{
for(j =0;j < g.n;j ++)
{
A[i][j] = g.edges[i][j];
path[i][j] = -1;
}
}
//Folyd
for(k = 0;k < g.n;k ++)
{
for(i = 0;i < g.n;i ++)
{
for(j = 0;j < g.n;j ++)
{
if(A[i][j] > A[i][k] + A[k][j])
{
A[i][j] = A[i][k] + A[k][j];
path[i][j] = k;
}
}
}
}
Dispath1(A,path,g.n);
}
//
int main()
{
f_in = fopen("F:\\ \\ \\in.txt","r");
f_out = fopen("F:\\ \\ \\out.txt","w");
int i,j;
char s[1010];//
fscanf(f_in,"%s",s);//
while(fscanf(f_in,"%d",&n)!=EOF/*cin>>n,n!=EOF*/)
{
MGraph g;//
fscanf(f_in,"%s",s);//
// , ,
for(i = 0;i < n;i ++)
for(j = 0;j < n;j ++)
fscanf(f_in,"%d",&g.edges[i][j]);
g.n = n;//
fprintf(f_out," ,
");//
for(i=0;i
ファイル内のデータを入力(in.txt)
:
6
:
0 5 1000000000 7 1000000000 1000000000
1000000000 0 4 1000000000 1000000000 1000000000
8 1000000000 0 1000000000 1000000000 9
1000000000 1000000000 5 0 1000000000 6
1000000000 1000000000 1000000000 5 0 1000000000
3 1000000000 1000000000 1000000000 1 0
覚えてる?txtとcppファイルと出力ファイルを1つのフォルダに置いて、ファイルのパスを修正することに注意します