图,最短路径算法,拓扑排序

//T2
#include <stdio.h>
#include <stdlib.h>
typedef struct list{
    int data;
    struct list *next;
    
}*List;
List creatlist(void){
    List A=(List)malloc(sizeof(struct list));
    A->next=NULL;
    return A;
}
List insertlist(int a,List A){
    List B=(List)malloc(sizeof(struct list));
    B->data=a;
    B->next=A->next;
    A->next=B;
    return A;
    
}
void Print(int A){
    if(A==0)printf("S,");
    if(A==1)printf("A,");
    if(A==2)printf("B,");
    if(A==3)printf("C,");
    if(A==4)printf("D,");
    if(A==5)printf("E,");
    if(A==6)printf("F,");
    if(A==7)printf("G,");
    if(A==8)printf("H,");
    if(A==9)printf("I,");
    if(A==10)printf("T,");
    
}
int change(char a){
    if(a=='S')return 0;
    if(a=='A')return 1;
    if(a=='B')return 2;
    if(a=='C')return 3;
    if(a=='D')return 4;
    if(a=='E')return 5;
    if(a=='F')return 6;
    if(a=='G')return 7;
    if(a=='H')return 8;
    if(a=='I')return 9;
    if(a=='T')return 10;
}
int Isempty(List Head){
    if(Head->next==NULL)return 1;
    else return 0;
}
int deletelist(List A){
    if(A->next==NULL)return -1;
    else if(A->next->next==NULL){
    	int a=A->next->data;
		A->next=NULL;
		return a; 
	}
    else{
        List C=A->next;
        while(C->next->next!=NULL)C=C->next;
        int a=C->next->data;
        C->next=NULL;
        return a;
         }
}
int main(){
    
    int Map[11][11]={
       //S A B C D E F G H I T
        0,1,0,0,4,0,0,6,0,0,0,
        0,0,2,0,0,2,0,0,0,0,0,
        0,0,0,2,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,4,
        0,3,0,0,0,3,0,0,0,0,0,
        0,0,0,2,0,0,3,0,0,3,0,
        0,0,0,1,0,0,0,0,0,0,3,
        0,0,0,0,2,1,0,0,6,0,0,
        0,0,0,0,0,2,0,0,0,6,0,
        0,0,0,0,0,0,1,0,0,0,4,
    };
    char n,m;
    int N,M;
    scanf("%c,%c",&n,&m);
    N=change(n);
    M=change(m);
    Map[N][M]=0;
    List Head=creatlist();
    int Indgree[11];
    for(int i=0;i<11;i++){
        Indgree[i]=0;
    }
    for(int i=0;i<11;i++){
        for(int j=0;j<11;j++){
            if(Map[j][i]){
                Indgree[i]++;
            }
            }
    }
    
    for(int i=0;i<11;i++){
        if(Indgree[i]==0){
            insertlist(i,Head);
        }
    }
    
    while(!Isempty(Head)){
        int S=deletelist(Head);
        Print(S);
        for(int i=0;i<11;i++){
            if(Map[S][i]){
                //Map[S][i]=0;
                Indgree[i]--;
                if(Indgree[i]==0){
                    insertlist(i,Head);
                }
            }
        }
        
    }
    
}
//T3
#include <stdio.h>
#include <stdlib.h>
struct Table{
	int Know;
	int Dist;
	int Path;
};
void Print(int a,struct Table T[]){
	if(T[a].Dist>=999){
		printf("-1");
		return;
	}
	
	
	if(T[a].Path!=-1){
		Print(T[a].Path,T);
		
	}
	printf("%d,",a+1);
}
int main(){
	int Map[7][7]={
	{999,2,999,1,999,999,999},
	{999,999,999,3,10,999,999},
	{4,999,999,999,999,5,999},
	{999,999,2,999,2,8,4},
	{999,999,999,999,999,999,6},
	{999,999,999,999,999,999,999},
	{999,999,999,999,999,1,999},
	};
	int N,M;
	scanf("%d,%d",&N,&M); 
	N--;
	M--;
	struct Table T[7];
	for(int i=0;i<7;i++){
		if(i==N)T[i].Dist=0;
		else T[i].Dist=999;
		T[i].Know=0;
		T[i].Path=-1;
	}
	int S=1;
	while(S){
		S=0;
		int v,dist=999;
		for(int i=0;i<7;i++){
			if(T[i].Know==0&&T[i].Dist<dist){
				dist=T[i].Dist;
				v=i;
				S=1;
			}
		}
		T[v].Know=1;
		for(int i=0;i<7;i++){
			if(Map[v][i]){
				if(T[i].Know==0){
					if(T[v].Dist+Map[v][i]<T[i].Dist){
						T[i].Dist=T[v].Dist+Map[v][i];
						T[i].Path=v;
					}
				}
			}
		}
	}
	
	Print(M,T);
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值