zoukankan      html  css  js  c++  java
  • hdu5137 How Many Maos Does the Guanxi Worth(单源最短路径)

    题目链接:点击打开链接

    题目描写叙述:如今有一张关系网。网中有n个结点标号为1~n。有m个关系,每一个关系之间有一个权值。问从2~n-1中随意去掉一个结点之后,从1~n的距离中全部最小值的最大值为多少?


    解题思路:多次调用Dijkstra就可以,每次标记那个结点不通就可以


    代码:

    #include <cstdio>
    #include <queue>
    #include <cstring>
    #define MAXN 31
    #define MAXE 1010
    #define INF 1e9+7
    using namespace std;
    int head[MAXN];
    struct Edge{
        int to,cost,next;
    }edge[MAXE*2];
    struct node{
        int ct;
        int cost;
        node(int _ct,int _cost):ct(_ct),cost(_cost){}
        bool operator<(const node& b)const{///注意优先权队列:<代表大顶堆,>代表小顶堆
            return cost>b.cost;
        }
    };
    int tol;
    void addEdge(int x,int y,int cost){
        edge[tol].to=y;
        edge[tol].cost=cost;
        edge[tol].next=head[x];
        head[x]=tol++;
    
        edge[tol].to=x;
        edge[tol].cost=cost;
        edge[tol].next=head[y];
        head[y]=tol++;
    }
    int n,m;
    int dis[MAXN];
    bool vis[MAXN];
    void bfs(int nt){
        memset(vis,false,sizeof(vis));
        vis[nt]=true;
        for(int i=1;i<=n;++i)
            dis[i]=INF;
        dis[1]=0;
        priority_queue<node> pq;
        while(!pq.empty()) pq.pop();
        pq.push(node(1,0));
        while(!pq.empty()){
            node tmp=pq.top();
            pq.pop();
            int u=tmp.ct;
            if(vis[u]) continue;
            vis[u]=true;
            for(int i=head[u];i!=-1;i=edge[i].next){
                int v=edge[i].to;
                int cost=edge[i].cost;
                if(!vis[v]&&dis[v]>dis[u]+cost){
                    dis[v]=dis[u]+cost;
                    pq.push(node(v,dis[v]));
                }
            }
        }
    }
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF&&(n!=0||m!=0)){
            tol=0;
            memset(head,-1,sizeof(head));
            int x,y,cost;
            for(int i=1;i<=m;++i){
                scanf("%d%d%d",&x,&y,&cost);
                addEdge(x,y,cost);
            }
            int ans=0;
            for(int i=2;i<n;++i){
                bfs(i);
                ans=max(ans,dis[n]);
            }
            if(ans<INF)
                printf("%d
    ",ans);
            else
                printf("Inf
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    Leetcode python 141. 环形链表
    leetcode python 387. 字符串中的第一个唯一字符 383. 赎金信 242. 有效的字母异位词
    leetcode python 566. 重塑矩阵 118. 杨辉三角
    leetcode python 350. 两个数组的交集 121. 买卖股票的最佳时机
    小程序常见的应用场景
    小程序基础入门
    高二数学必修4
    高二数学必修3(概率)
    高中3年数学知识梳理 & 成考 专升本 高数对比;
    高一数学必修1
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6985062.html
Copyright © 2011-2022 走看看