机试基本算法笔记--图论

邻接表表示图

struct Edge{
    int nextNode;//下一个结点编号
    int cost;//该边的权重
};

int main()
{
    vector<Edge> edge[N];
    for(int i=0;i<=N;i++)//初始化
        edge[i].clear();
    for(int i=0;i<N;i++)
    {
        int tempa,tempb,cost;
        Edge tempe;
        cin>>tempa>>tempb>>cost;

        tempe.nextNode=tempb;
        tempe.cost=cost;
        edge[tempa].push_back(tempe);
        
        for(int i=0;i<N;i++)//遍历
        {
            for(int j=0;j<edge[i].size();j++)
            {
                int nextNode = edge[i][j].nextNode;
                int cost = edge[i][j].cost;
            }
        }

        edge[1].erase(edge[1].begin()+5,edge[1].begin()+6);//删除结点1与结点5的路径
    }
}

并查集

int findRoot(int x)
{
    if(Tree[x]!=-1)
    {
        int tmp = findRoot(Tree[x]);//路径压缩
        Tree[x]=tmp;
        return tmp;
    }
    return x;
}

int findRoot2(int x)
{
    int tmp=x;
    while(Tree[x]!=-1)
        x=Tree[x];
    int root=x;
    while(Tree[x]!=-1)//路径压缩
    {
        int t=Tree[x];
        Tree[x]=root;
        x=t;
    }
    return root;
}

最小生成树(Kruskal算法)

struct Edge{
    int a,b;//顶点
    int cost;//权值
}edge[100];
int Tree[N];
bool cmp(Edge v1,Edge v2)//按照路径的权重排序
{
    return v1.cost<v2.cost;
}

int findRoot(int x)
{
    if(Tree[x]!=-1)
    {
        int tmp = findRoot(Tree[x]);//路径压缩
        Tree[x]=tmp;
        return tmp;
    }
    return x;
}

int main()
{
    int n,tempa,tempb,cost;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>tempa>>tempb>>cost;
        edge[i].a=tempa;
        edge[i].b=tempb;
        edge[i].cost=cost;
    }
    sort(edge,edge+n,cmp);
    for(int i=0;i<n;i++)
        Tree[i]=-1;
    int ans=0;
    for(int i=0;i<n;i++)
    {
        int a=findRoot(edge[i].a);
        int b=findRoot(edge[i].b);
        if(a!=b)
        {
            Tree[a]=b;
            ans+=edge[i].cost;
        }
    }
    cout<<ans<<endl;
}

最短路径(dijstra)

	int n,m;
    cin>>n>>m;
    int dst[N] = {MAXDST};
    int flag[N] = {0};
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=n;j++)
            edge[i][j] = MAXDST;
        edge[i][i] = 0;
    }
    dst[1] = 0;
    flag[1] = 1;
    for(int i=1;i<=m;i++)
    {
        int tempi,tempj,tempw;
        cin>>tempi>>tempj>>tempw;
        edge[tempi][tempj] = tempw;
    }
    for(int i=1;i<=n;i++)
        dst[i] = edge[1][i];
    for(int i=1;i<=n;i++)
    {
        int mindst = MAXDST;
        int u = 1;
        for(int j=1;j<=n;j++)
        {
            if(flag[j]==0&&dst[j]<mindst)
            {
                u = j;
                mindst = dst[j];
            }
        }
        flag[u] = 1;
        for(int j=1;j<=n;j++)
        {
            if(flag[j]==0&&edge[u][j]<MAXDST)
            {
                if(dst[j]>dst[u]+edge[u][j])
                {
                    dst[j] = dst[u] + edge[u][j];
                }
            }
        }
    }
    cout<<dst[n]<<endl;

拓扑排序

	vector<int> edge[N];
	queue<int> Q;//入度为0的结点队列
    int inDegree[N];//所有结点的入度
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        inDegree[i]=0;
        edge[i].clear();
    }
    while(m--)
    {
        int a,b;
        cin>>a>>b;
        inDegree[b]++;
        edge[a].push_back(b);
    }
    while(!Q.empty())//清空队列
        Q.pop();
    int cnt=0;
    while(!Q.empty())
    {
        int nowP=Q.front();
        Q.pop();
        cnt++;
        for(int i=0;i<edge[nowP].size();i++)
        {
            inDegree[edge[nowP][i]]--;
            if(inDegree[edge[nowP][i]]==0)
                Q.push(edge[nowP][i]);
        }
    }
    if(cnt==n) cout<<"Yes\n";
    else cout<<"No\n";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值