图toposort

这是一个神奇的错误
ming@localhost codetest]$ gcc main.c -o main  
/usr/lib/gcc/i686-redhat-linux/4.5.1/../../../crt1.o: In function `_start':  
(.text+0x18): undefined reference to `main'  
collect2: ld 返回 1  
没有main
困扰了 我 这个笨蛋 好几天的问题 终于被解决了 突然觉得有点空虚 虽然说这个问题和下面的问题一点关系也没有 但是还是想把我的心情写在这里 因为现在的感觉真的是太空虚了
///有向无环图
///拓扑排序
///图的各种表示法 汇总
///1.邻接矩阵
/*
#include 
#include 
#include
using namespace std;

int deg[100];
bool vis[100];
int map1[100][100];
int a[100];
void toposort(int n)
{
    priority_queue , greater >q;
    while(!q.empty())
        q.pop();
    for(int i=1; i<=n; i++)
    {
        if(deg[i] == 0)
        {
            q.push(i);
            deg[i]--;
            vis[i] = true;
        }
    }
    int j = 0;
    while(!q.empty())
    {
        int u = q.top();
        q.pop();
        a[j++] = u;
        ///vis[u] = false; ///写不写都行
        for(int i=1; i<=n; i++)
        {
            if(map1[u][i] == 1 && !vis[i])
            {
                deg[i]--;
                if(deg[i] == 0)
                {
                    deg[i]--;
                    q.push(i);
                    vis[i] = true;
                }
            }
        }
    }
    if(j!=n)
    {
        cout << "有环\n";
        return;
    }
    for(int i=0; i> n >> m;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                map1[i][j] = 0;
            }
        }
        int u, v;
        for(int i=0; i> u >> v;
            if(map1[u][v] == 0)
                deg[v]++;
            map1[u][v] = 1;
        }
        toposort(n);
    }

    return 0;
}*/
/*
3 3
1 2
2 3
3 1
有环
4 5
2 1
2 4
1 4
4 3
3 1
有环
4 5
2 4
2 1
4 1
1 3
4 3
2
4
1
3

*/
///2.adjacency List (by vector)
/*
#include
#include
#include
using namespace std;
const int maxn = 100;
int q[maxn];
bool vis[maxn][maxn];
bool vis1[maxn];
int head, tail;
struct node
{
    int to;
    int cost;
    node(int t, int c):to(t), cost(c){}
};
vectorG[maxn];
int deg[maxn];
int a[maxn];
void toposort(int n)
{
    head = 0;
    tail = 0;
    int j = 0;
    for(int i=0; i> n >> m;
        int u, v;

        ///cout << G[0][5].cost << endl;///test
        for(int i=0; i> u >> v;
            u--, v--;
            ///for(int j=0; j
#include
#include
using namespace std;
const int maxn = 100;
struct node
{
    int cost;
    int to;
    int next;
};
node edge[maxn];
int first[maxn];
bool vis[maxn];
int deg[maxn];
int a[maxn];
void toposort(int n)
{
    int j = 0;
    priority_queue < int, vector, greater >q;
    while(!q.empty())
        q.pop();

    for(int i=1; i<=n; i++)
    {
        if(deg[i] == 0)
        {
            deg[i]--;
            q.push(i);
        }
    }

    while(!q.empty())
    {
        int u = q.top();
        a[j++] = u;
        q.pop();
        for(int i=first[u]; i!=-1; i=edge[i].next)
        {
            node e = edge[i];
            if(e.cost)
            {
                deg[e.to]--;
                if(deg[e.to] == 0)
                {
                    q.push(e.to);
                    deg[e.to]--;
                }
            }
        }
    }
    if(j!=n)
    {
        cout << "有环" << endl;
        return;
    }
    else
    {
        for(int i=0; i> n >> m;
        int u, v;
        int cnt = 0;
        int i, j;
        for( i=0; i> u >> v;
            for( j=first[u]; j!=-1; j = edge[j].next)
            {
                if(v == edge[j].to)
                {
                    break;
                }
            }
            if(j == -1)
            {
                edge[cnt].to = v;
                edge[cnt].cost = 1;
                edge[cnt].next = first[u];
                first[u] = cnt++;
                deg[v]++;
            }

        }
        toposort(n);
    }
    return 0;
}
*/
///adjacency List (normal)
#include
#include
#include
#include
using namespace std;
const int maxn = 100;
struct arcnode
{
    int weight;
    int adjvex;
    arcnode *nextarc;
};
typedef struct vnode
{
    int data;
    arcnode *firstarc;
}Adjvex[20];
struct Graph
{
    Adjvex vertices;
    int vexnum, arcnum;
};
int deg[maxn];
int a[maxn];
int LocateVex(Graph G, int u)
{
    for(int i=0; i> G.vexnum >> G.arcnum;
    for(int i=0; i> u >> v;
        p = new arcnode;
        if(p == NULL)
            return;
        int k = LocateVex(G, u);
        int kk = LocateVex(G, v);
        for(q = G.vertices[k].firstarc; q!=NULL; q=q->nextarc)
        {
            if(kk == q->adjvex)
            {
                break;
            }
        }
        if(q == NULL)
        {
            deg[v]++;
            p->weight = 1;
            p->adjvex = kk;
            p->nextarc = G.vertices[k].firstarc;
            G.vertices[k].firstarc = p;
        }

    }
}
/*
void toposort(Graph G,int n)
{
    priority_queue < int, vector, greater >q;
    int j = 0;
    arcnode *p;
    while(!q.empty())
        q.pop();
    for(int i=1; i<=n;i++)
    {
        if(deg[i] == 0)
        {
            deg[i]--;
            q.push(i);
        }
    }
    while(!q.empty())
    {
        int u = q.top();
        a[j++] = u;
        q.pop();
        int kk = LocateVex(G, u);
        for(p=G.vertices[kk].firstarc; p!=NULL; p=p->nextarc)
        {
            int k = p->adjvex;
            int d = G.vertices[k].data;
            if(p->weight == 1)
            {
                deg[d]--;
                if(deg[d] == 0)
                {
                    q.push(d);
                    deg[d]--;
                }
            }
        }

    }
    if(j!=n)
        cout << "有环" << endl;
    else
    {
        for(int i=0; is;
    while(!s.empty())
        s.pop();
    for(int i=1; i<=n; i++)
    {
        if(deg[i] == 0)
        {
            deg[i]--;
            s.push(i);
        }
    }
    while(!s.empty())
    {
        int u = s.top();
        a[j++] = u;
        s.pop();
        arcnode *p;
        int k = LocateVex(G, u);
        for(p=G.vertices[k].firstarc; p!=NULL; p=p->nextarc)
        {
            int kk = p->adjvex;
            int d = G.vertices[kk].data;
            if(p->weight == 1)
            {
                deg[d]--;
                if(deg[d] == 0)
                {
                    s.push(d);
                    deg[d]--;
                }
            }
        }
    }
    if(j!=n)
        cout << "有环" << endl;
    else
    {
        for(int i=0; i

你可能感兴趣的:(数据结构)