拓扑排序 C++ 代码


拓扑排序 C++ 代码

/*
*Program:TopologicalSort
*Author:Yee-fan Zhu
*/

using namespace std;
ifstream fin("topo.in");
ofstream fout("topo.out");
bool TopologicalSort(int a[][101],int *ans) //可以完成拓扑排序则返回True   
{
        int n = a[0][0], i, j;
        int into[101];
        memset(into, 0, sizeof(into));
        memset(ans, 0, sizeof(ans));
        //计算每个顶点的入度

        {

                {
                        if (a[i][j]>0)
                                into[j]++;
                }
        }
        into[0]=1;

        {
                j=0;
                while (into[j]!= 0)
                {
                        j++;
                        if(j>n)
                        return false;
                }
                ans[i]=j;
                into[j]=-1;
                //删除这个顶点

                {
                        if (a[j][k]>0)
                                into[k]--;
                }
        }
        return true;
}
int main()
{
        int n;
        int mat[101][101];
        int ans[101];
        fin>>n;
        mat[0][0]=n;


                        mat[i][j]=0;
        int m;
        fin>>m;
        int x,y,z;

        {
                fin>>x>>y>>z;
                mat[x][y]=z;
        }
        if (TopologicalSort(mat,ans))
        {


                        {

                        }

        }

        return 0;
}

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《拓扑排序 C++ 代码
本文地址:https://www.zhiletu.com/archives-7853.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微