zoukankan      html  css  js  c++  java
  • [kuangbin带你飞]专题四 最短路练习

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<iomanip>
    #include<cmath>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    using namespace std;
    /*
    单源最短路 路径方向与switch方向同向那么路径权值0,否则为1
    */
    #define MAXN 101
    #define INF 0x3f3f3f3f
    bool g[MAXN][MAXN],TO[MAXN][MAXN],been[MAXN];
    int lowcost[MAXN],A,B,N;
    int Dijkstra(int beg,int to)
    {
        for(int i=1;i<=N;i++)
        {
            lowcost[i] = INF;
            been[i] = false;
        }
        lowcost[beg] = 0;
        for(int i=0;i<N;i++)
        {
            int Minc = INF,k = -1;
            for(int j=1;j<=N;j++)
            {
                if(!been[j]&&lowcost[j]<Minc)
                {
                    Minc = lowcost[j];
                    k = j;
                }
            }
            if(k==-1) break;
            been[k] = true;
            for(int j=1;j<=N;j++)
            {
                int cost;
                if(!g[k][j]) cost = INF;
                else
                {
                    if(TO[k][j]==true) 
                        cost = 0;
                    else cost = 1;
                }
                if(!been[j]&&lowcost[j]>lowcost[k]+cost)
                {
                    lowcost[j] = lowcost[k]+cost;
                }
            }
        }
        return lowcost[to];
    }
    int main()
    {
        cin>>N>>A>>B;
        int k,tmp;
        for(int i=1;i<=N;i++)
        {
            cin>>k;
            for(int j=1;j<=k;j++)
            {
                cin>>tmp;
                if(j==1)
                    TO[i][tmp] = true;
                g[i][tmp] = true;
            }
        }
        int ans = Dijkstra(A,B);
        if(ans!=INF)
            cout<<ans<<endl;
        else
            cout<<-1<<endl;
        return 0;
    }
  • 相关阅读:
    【Loj146】DFS序3-树链剖分
    【BZOJ3083】遥远的国度- 树剖-换根
    树链剖分-树剖换根
    c++ 随机生成树
    【HAOI2015】树上操作-树链剖分
    centos修改dns
    HttpClient
    aiax跨域问题
    伪静态
    springboot父子项目
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6571988.html
Copyright © 2011-2022 走看看