#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define VNUM 20
#define INTMAX 10000
typedef struct{
int vi;
int weight;
}LowCost;
typedef struct{
int vexs[VNUM];
int arcs[VNUM][VNUM];
int vexNUM,arcNUM;
}NetGraph;
void create_NetGraph(NetGraph *G){
int i,j,k,w;
scanf("%d %d",&G->vexNUM,&G->arcNUM);
for(i=0;i<G->vexNUM;i++){
scanf("%d",&G->vexs[i]);
}
for(i=0;i<G->vexNUM;i++){
for(j=0;j<G->vexNUM;j++){
if(i==j)G->arcs[i][j] = 0;
else G->arcs[i][j] = INTMAX;
}
}
for(k=0;k<G->arcNUM;k++){
scanf("%d %d %d",&i,&j,&w);
G->arcs[i][j] = w;
//G->arcs[j][i] = w;
}
}
void prim(NetGraph G){
int mincost,i,j,k;
LowCost *lowcost = (LowCost *)malloc(sizeof(LowCost) * G.vexNUM);
for(i=0;i<G.vexNUM;i++){
lowcost[i].vi = 0;
lowcost[i].weight = G.arcs[0][i];
}
lowcost[0].weight = 0;
for(i=1;i<G.vexNUM;i++){
mincost = INT_MAX;
for(j=0;j<G.vexNUM;j++){
if(lowcost[j].weight<mincost && lowcost[j].weight != 0){
mincost = lowcost[j].weight;
k=j;
}
printf("%d %d %d ",G.vexs[lowcost[k].vi],G.vexs[k],mincost);
lowcost[k].weight = 0;
for(j=0;j<G.vexNUM;j++){
if(G.arcs[k][j]<lowcost[j].weight){
lowcost[j].weight = G.arcs[k][j];
lowcost[j].vi = k;
}
}
}
}
}
typedef char ZFC[20];
int find(ZFC *s, int n, char c){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<strlen(s[i]);j++){
if(s[i][j]==c)return i;
}
}
return -1;
}
void Krusal(NetGraph G){
int i,j,k,t,p,q,x[G.vexNUM][4];
ZFC c[G.vexNUM];
for(i=0;i<G.vexNUM;i++){
c[i][0] = i+48;
c[i][1] = 0;
}
for(i=0,k=0;i<G.vexNUM;i++){
for(j=i;j<G.vexNUM;j++){
if(G.arcs[i][j]!=0 && G.arcs[i][j] != INT_MAX){
x[k][0] = i;
x[k][1] = j;
x[k][2] = G.arcs[i][j];
x[k][3] = 0;
k++;
}
}
}
for(i=0;i<G.arcNUM;i++){
for(k=0;j<G.arcNUM-1-i;k++){
if(x[j][2]>x[j+1][2]){
for(p=0;p<4;p++){
t = x[j][p];
x[j][p] = x[j+1][p];
x[j+1][p] = t;
}
}
}
}
for(i=0,k=0;i<G.arcNUM;i++){
p = find(c,G.vexNUM,x[i][0]+48);
q = find(c,G.vexNUM,x[i][1]+48);
if(p!=q){
strcat(c[p],c[q]);
c[q][0]=0;
k++;
x[i][3] = 1;
}
if(k==G.vexNUM-1)break;
}
printf("最小生成树的边为:\n");
printf("%7s%7s%6s\n","顶点","顶点","权值");
for(i=0;i<G.arcNUM;i++){
if(x[i][3]==1)printf("%d %d %d |",G.vexs[x[i][0]],G.vexs[x[i][1]],x[i][2]);
}
}
void Dijkstra(NetGraph G, int v){
int dist[G.vexNUM];
int path[G.vexNUM][G.vexNUM];
int i,j,k,m,min,n,flag;
for(i=0;i<G.vexNUM;i++)
for(j=0;j<G.vexNUM;j++)path[i][j]=-1;
for(i=0;i<G.vexNUM;i++){
dist[i] = G.arcs[v][i];
if(dist[i]!=0)path[i][0] = v;
if(dist[i]!=0 && dist[i] != INTMAX)path[i][1] = i;
}
n=0;
flag=1;
while(flag){
k=0;
min=INT_MAX;
for(j=0;j<G.vexNUM;j++){
if(dist[j]!=0&&dist[j]<min){
k=j;
min = dist[j];
}
}
printf("k:%d 第%d条最短路径长度为%d--(",k,++n,min);
for(j=0;j<G.vexNUM;j++)
if(path[k][j]!=-1)printf("%d,",G.vexs[path[k][j]]);
printf("\b)\n");
for(j=0;j<G.vexNUM;j++){
if(j!=k && dist[j] !=0){
if(dist[k]+G.arcs[k][j] < dist[j]){
dist[j] = dist[k] + G.arcs[k][j];
for(m=0;m<G.vexNUM;m++)path[j][m] = path[k][m];
for(m=0;m<G.vexNUM && path[j][m] != -1;)m++;
path[j][m] = j;
}
}
}
dist[k] = 0;
flag = 0;
for(j=0;j<G.vexNUM;j++)if(dist[j]!=0 && dist[j]<INTMAX)flag=1;
}
// for(i=0;i<G.vexNUM;i++){
// printf("%d>>",i);
// for(j=0;j<G.vexNUM;j++){
// if(path[i][j]!=-1)printf("%d ",path[i][j]);
// }
// printf("\n");
// }
}
#define N 4
void Floyd(NetGraph G,int Path[N][N]){//生成 Path 矩阵
int i,j,v;//v为结点间中间变量
float A[G.vexNUM][G.vexNUM];
for(i=0;i<G.vexNUM;i++){
for(j=0;j<G.vexNUM;j++){
A[i][j] = G.arcs[i][j];
Path[i][j] = -1;//初始为-1
}
}
for(v=0;v<G.vexNUM;v++){
for(i=0;i<G.vexNUM;i++){
for(j=0;j<G.vexNUM;j++){
if(A[i][j] > A[i][v] + A[j][v]){
A[i][j] = A[i][v] + A[v][j];
Path[i][j] = v;
}
}
}
}
}
void getPath(int S, int E,int path[N][N])
{
if (path[S] [E] == -1) {
return;
}
else {
printf("%d->",path[S][E]);
getPath(S,path[S][E],path);
}
}
void printPath(int u,int v ,int path[N][N]){//u:起点 v:终点
if(path[u][v] == -1){
printf("%d->%d\n ",u,v);
}
else{
int mid = path[u][v];
printPath(u,mid,path);
printPath(mid,v,path);
}
}
typedef struct node{
int vertex;
int weight;
struct node *next;
}AOVnode,*AOVNODE;
typedef struct {
char vertex;
int count;
AOVnode *firstArc;
}VertexNode;
typedef struct {
VertexNode data[20];
int vexNum,arcNum;
}AOVGraph;
typedef struct stack{
int *data;
int top;
int StackSize;
}stack,*Stack;
int initStack(Stack sk,int max){
sk->data = (int *)malloc(max * sizeof(int));
if(sk->data == NULL){
exit(0);
}
sk->top = -1;
sk->StackSize = max;
return 1;
}
int isStackEmpty(Stack sk){
if (sk->top == -1)return 1;
else return 0;
}
int StackLength(Stack sk){
return sk->top +1;
}
int Push(Stack sk,int e){
if(sk->StackSize == sk->top+1)return 0;
sk->top++;
sk->data[sk->top]=e;
return 1;
}
int Pop(Stack sk){
if(sk->top == -1)return -1;
int e = sk->data[sk->top];
sk->top--;
return e;
}
int getTop(Stack sk){
if(sk->top == -1)return -1;
return sk->data[sk->top];
}
int top_sort(AOVGraph G){
stack s;
int i,m=0,n;
int w;
initStack(&s,20);
for(i=0;i<G.vexNum;i++)
if(G.data[i].count==0)Push(&s,i);
while(!isStackEmpty(&s)){
int v = Pop(&s);
printf("%c",G.data[v].vertex);
m = m+1;
AOVnode *p = G.data[v].firstArc;
while(p!=NULL){
w = p->vertex;
G.data[w].count--;
if(G.data[w].count==0)Push(&s,w);
p = p->next;
}
}
if(m<G.vexNum)return 0;
else return 1;
}
void create_AOVGraph(AOVGraph *G){
int v,w,k;
int i;
scanf("%d %d",&G->vexNum,&G->arcNum);
fflush(stdin);
for(i=0;i<G->vexNum;i++){
scanf("%c",&(G->data[i].vertex));
G->data[i].firstArc=NULL;
G->data[i].count=0;
fflush(stdin);
}
printf(">>>\n");
fflush(stdin);
for(i=0;i<G->arcNum;i++){
scanf("%d %d %d",&v,&w,&k);
AOVnode *p = (AOVnode *)malloc(sizeof(AOVnode));
p->next=G->data[v].firstArc;
p->vertex=w;
p->weight = k;
G->data[v].firstArc= p;
G->data[w].count++;
}
}
void critical_path(AOVGraph G){
int i;
int ve[G.vexNum];
int vl[G.vexNum];
stack s1,s2;
for(i=0;i<G.vexNum;i++)ve[i] = 0;
initStack(&s1,G.vexNum);
initStack(&s2,G.vexNum);
for(i=0;i<G.vexNum;i++){
if(G.data[i].count==0)Push(&s1,i);
}
while(!isStackEmpty(&s1)){
int j=Pop(&s1);
Push(&s2,j);
AOVnode *p = G.data[j].firstArc;
while(p!=NULL){
int k = p->vertex;
G.data[k].count--;
if(G.data[k].count==0)Push(&s1,k);
if(ve[j]+p->weight>ve[k])ve[k]=ve[j]+p->weight;
p = p->next;
}
}
for(i=0;i<G.vexNum;i++)vl[i]=ve[G.vexNum-1];
while(!isStackEmpty(&s2)){
int j = Pop(&s2);
AOVnode *p =G.data[j].firstArc;
while(p!=NULL){
int k = p->vertex;
if((vl[k]-p->weight)<vl[j])vl[j]=vl[k]-p->weight;
p = p->next;
}
}
int j;
char tag;
for(j=0;j<G.vexNum;j++){
AOVnode *p =G.data[j].firstArc;
while(p!=NULL){
int k = p->vertex;
int ee=ve[j];
int el=vl[k]-p->weight;
if(ee==el)tag='O';
else tag=' ';
printf("%d %d %d %d %d %c\n",j,k,p->weight,ee,el,tag);
p = p->next;
}
}
}
int main(){
// NetGraph G;
// int i,j;
// create_NetGraph(&G);
// //Dijkstra(G,0);
// int Path[G.vexNUM][G.vexNUM];
// Floyd(G,Path);
//
// printPath(0,2,Path);
// //printPath(0,2,Path);
AOVGraph G;
create_AOVGraph(&G);
top_sort(G);
printf("\n");
critical_path(G);
}
AOE网实现(c语言)
最新推荐文章于 2024-01-24 23:59:30 发布